欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android bitmap compress(图片压缩)代码

程序员文章站 2023-11-12 10:08:40
android的照相功能随着手机硬件的发展,变得越来越强大,能够找出很高分辨率的图片。有些场景中,需要照相并且上传到服务,但是由于图片的大小太大,那么就上传就会很慢(在有些...

android的照相功能随着手机硬件的发展,变得越来越强大,能够找出很高分辨率的图片。
有些场景中,需要照相并且上传到服务,但是由于图片的大小太大,那么就上传就会很慢(在有些网络情况下),而且很耗流量,要想速度快,那么就需要减小图片的大小。减少图片的大小有两种方法,1. 照小图片; 2. 压缩大图片。 照相时获取小图片一般不太符合要求,因为,图片的清晰度会很差,但是这种情况有个好处就是应用速度会快些; 压缩图片,就是把大图片压缩小,降低图片的质量,在一定范围内,降低图片的大小,并且满足需求(图片仍就清晰)。下面组要是介绍图片的压缩:


1. 照相请查看 ->想要保存图片到制定目录,启动camera应用时,需要指定文件
2. 压缩过程:
    2.1 从图片路径中读取图片(图片很大,不能全部加在到内存中处理,要是全部加载到内存中会内存溢出)
[java]

复制代码 代码如下:

final bitmapfactory.options options = new bitmapfactory.options();
    options.injustdecodebounds = true;
    bitmapfactory.decodefile(filepath, options);

    // calculate insamplesize 
    options.insamplesize = calculateinsamplesize(options, 480, 800);

    // decode bitmap with insamplesize set 
    options.injustdecodebounds = false;

    bitmap bm = bitmapfactory.decodefile(filepath, options);

 final bitmapfactory.options options = new bitmapfactory.options();
  options.injustdecodebounds = true;
  bitmapfactory.decodefile(filepath, options);

  // calculate insamplesize
  options.insamplesize = calculateinsamplesize(options, 480, 800);

  // decode bitmap with insamplesize set
  options.injustdecodebounds = false;

  bitmap bm = bitmapfactory.decodefile(filepath, options);


2.2 处理图片旋转 
[java]
复制代码 代码如下:

int degree = readpicturedegree(filepath);
        bm = rotatebitmap(bm,degree) ;

int degree = readpicturedegree(filepath);
  bm = rotatebitmap(bm,degree) ;[java] view plaincopyprint?private static int readpicturedegree(string path) {  
           int degree  = 0;  
           try {  
                   exifinterface exifinterface = new exifinterface(path);  
                   int orientation = exifinterface.getattributeint(exifinterface.tag_orientation, exifinterface.orientation_normal);  
                   switch (orientation) {  
                   case exifinterface.orientation_rotate_90:  
                           degree = 90;  
                           break;  
                   case exifinterface.orientation_rotate_180:  
                           degree = 180;  
                           break;  
                   case exifinterface.orientation_rotate_270:  
                           degree = 270;  
                           break;  
                   }  
           } catch (ioexception e) {  
                   e.printstacktrace();  
           }  
           return degree;  
       } 

private static int readpicturedegree(string path) {
        int degree  = 0;
        try {
                exifinterface exifinterface = new exifinterface(path);
                int orientation = exifinterface.getattributeint(exifinterface.tag_orientation, exifinterface.orientation_normal);
                switch (orientation) {
                case exifinterface.orientation_rotate_90:
                        degree = 90;
                        break;
                case exifinterface.orientation_rotate_180:
                        degree = 180;
                        break;
                case exifinterface.orientation_rotate_270:
                        degree = 270;
                        break;
                }
        } catch (ioexception e) {
                e.printstacktrace();
        }
        return degree;
    }


[java]
复制代码 代码如下:

view plaincopyprint?private static bitmap rotatebitmap(bitmap bitmap, int rotate){
        if(bitmap == null)
            return null ;

        int w = bitmap.getwidth();
        int h = bitmap.getheight();

        // setting post rotate to 90 
        matrix mtx = new matrix();
        mtx.postrotate(rotate);
        return bitmap.createbitmap(bitmap, 0, 0, w, h, mtx, true);
    }

private static bitmap rotatebitmap(bitmap bitmap, int rotate){
  if(bitmap == null)
   return null ;

  int w = bitmap.getwidth();
  int h = bitmap.getheight();

  // setting post rotate to 90
  matrix mtx = new matrix();
  mtx.postrotate(rotate);
  return bitmap.createbitmap(bitmap, 0, 0, w, h, mtx, true);
 }


2.3压缩图片      
[java]
复制代码 代码如下:

bm.compress(bitmap.compressformat.jpeg, 30, baos);//30 是压缩率,表示压缩70%; 如果不压缩是100,表示压缩率为0

bm.compress(bitmap.compressformat.jpeg, 30, baos);//30 是压缩率,表示压缩70%; 如果不压缩是100,表示压缩率为0


完整的方法代码:
[java]
复制代码 代码如下:

public static bitmap getsmallbitmap(string filepath) {

        final bitmapfactory.options options = new bitmapfactory.options();
        options.injustdecodebounds = true;
        bitmapfactory.decodefile(filepath, options);

        // calculate insamplesize 
        options.insamplesize = calculateinsamplesize(options, 480, 800);

        // decode bitmap with insamplesize set 
        options.injustdecodebounds = false;

        bitmap bm = bitmapfactory.decodefile(filepath, options);
        if(bm == null){
            return  null;
        }
        int degree = readpicturedegree(filepath);
        bm = rotatebitmap(bm,degree) ;
        bytearrayoutputstream baos = null ;
        try{
            baos = new bytearrayoutputstream();
            bm.compress(bitmap.compressformat.jpeg, 30, baos);

        }finally{
            try {
                if(baos != null)
                    baos.close() ;
            } catch (ioexception e) {
                e.printstacktrace();
            }
        }
        return bm ;

    }

public static bitmap getsmallbitmap(string filepath) {

  final bitmapfactory.options options = new bitmapfactory.options();
  options.injustdecodebounds = true;
  bitmapfactory.decodefile(filepath, options);

  // calculate insamplesize
  options.insamplesize = calculateinsamplesize(options, 480, 800);

  // decode bitmap with insamplesize set
  options.injustdecodebounds = false;

  bitmap bm = bitmapfactory.decodefile(filepath, options);
  if(bm == null){
   return  null;
  }
  int degree = readpicturedegree(filepath);
  bm = rotatebitmap(bm,degree) ;
  bytearrayoutputstream baos = null ;
  try{
   baos = new bytearrayoutputstream();
   bm.compress(bitmap.compressformat.jpeg, 30, baos);

  }finally{
   try {
    if(baos != null)
     baos.close() ;
   } catch (ioexception e) {
    e.printstacktrace();
   }
  }
  return bm ;

 }


[java]
复制代码 代码如下:

view plaincopyprint?private static int calculateinsamplesize(bitmapfactory.options options,
            int reqwidth, int reqheight) {
        // raw height and width of image 
        final int height = options.outheight;
        final int width = options.outwidth;
        int insamplesize = 1;

        if (height > reqheight || width > reqwidth) {

            // calculate ratios of height and width to requested height and 
            // width 
            final int heightratio = math.round((float) height
                    / (float) reqheight);
            final int widthratio = math.round((float) width / (float) reqwidth);

            // choose the smallest ratio as insamplesize value, this will 
            // guarantee 
            // a final image with both dimensions larger than or equal to the 
            // requested height and width. 
            insamplesize = heightratio < widthratio ? widthratio : heightratio;
        }

        return insamplesize;
    }

private static int calculateinsamplesize(bitmapfactory.options options,
   int reqwidth, int reqheight) {
  // raw height and width of image
  final int height = options.outheight;
  final int width = options.outwidth;
  int insamplesize = 1;

  if (height > reqheight || width > reqwidth) {

   // calculate ratios of height and width to requested height and
   // width
   final int heightratio = math.round((float) height
     / (float) reqheight);
   final int widthratio = math.round((float) width / (float) reqwidth);

   // choose the smallest ratio as insamplesize value, this will
   // guarantee
   // a final image with both dimensions larger than or equal to the
   // requested height and width.
   insamplesize = heightratio < widthratio ? widthratio : heightratio;
  }

  return insamplesize;
 }