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

Android图片实现压缩处理的实例代码

程序员文章站 2023-11-21 18:14:04
整理文档,搜刮出一个android图片实现压缩处理的实例代码,稍微整理精简一下做下分享。 详解: 1.获取本地图片file文件 获取bitmapfa...

整理文档,搜刮出一个android图片实现压缩处理的实例代码,稍微整理精简一下做下分享。

详解:

1.获取本地图片file文件 获取bitmapfactory.options对象 计算原始图片 目标图片宽高比 计算输出的图片宽高

2.根据宽高比计算options.insamplesize值(缩放比例 if set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory.)得到bitmap位图 根据位图对象获取新的输出位图对象 bitmap.createscaledbitmap(bitmap src, int dstwidth, int dstheight, boolean filter)creates a new bitmap, scaled from an existing bitmap, whenpossible.

3.获取图片方向调整、失量压缩图片保持在1024kb以下

 //进行大小缩放来达到压缩的目的
 bitmapfactory.options options = new bitmapfactory.options();
 options.injustdecodebounds = true;
 bitmapfactory.decodefile(srcimagepath, options);
 //根据原始图片的宽高比和期望的输出图片的宽高比计算最终输出的图片的宽和高

 float srcwidth = options.outwidth;
 float srcheight = options.outheight;
 float maxwidth = outwidth;
 float maxheight = outheight;
 float srcratio = srcwidth / srcheight; //原始图片宽高比
 float outratio = maxwidth / maxheight; //目标图片宽高比
 float actualoutwidth = srcwidth;
 float actualoutheight = srcheight;
  if (srcwidth > maxwidth || srcheight > maxheight) {
   if(srcratio>outratio){ //原始宽高比大于目标宽高比
     actualoutwidth = maxwidth;
     actualoutheight = actualoutwidth / srcratio;
   }else if(srcratio<outratio){ //原始宽高比小于目标宽高比
     actualoutheight = maxheight;
     actualoutwidth = actualoutheight * srcratio;
   }
 }else{
   actualoutwidth = maxwidth;
   actualoutheight = maxheight;
 }

 options.insamplesize = computsamplesize(options, actualoutwidth, actualoutheight);
 options.injustdecodebounds = false;
 bitmap scaledbitmap = null;
 try {
   scaledbitmap = bitmapfactory.decodefile(srcimagepath, options);
 } catch (outofmemoryerror e) {
   e.printstacktrace();
 }
 if (scaledbitmap == null) {
   return null;
 }

  //生成最终输出的bitmap
 bitmap actualoutbitmap = bitmap.createscaledbitmap(scaledbitmap, (int) actualoutwidth, (int) actualoutheight, true);

 //释放原始位图资源
 if(scaledbitmap!=actualoutbitmap){ //判断目标位图是否和原始位图指向栈目标相同
   scaledbitmap.recycle();
   scaledbitmap = null;
 }

  //处理图片旋转问题
 exifinterface exif = null;
 try {
   exif = new exifinterface(srcimagepath);
   int orientation = exif.getattributeint(
       exifinterface.tag_orientation, 0);
   matrix matrix = new matrix();
   if (orientation == exifinterface.orientation_rotate_90) {
     matrix.postrotate(90);
   } else if (orientation == exifinterface.orientation_rotate_180) {
     matrix.postrotate(180);
   } else if (orientation == exifinterface.orientation_rotate_270) {
     matrix.postrotate(270);
   }
   actualoutbitmap = bitmap.createbitmap(actualoutbitmap, 0, 0,
       actualoutbitmap.getwidth(), actualoutbitmap.getheight(), matrix, true);
 } catch (ioexception e) {
   e.printstacktrace();
   return null;
 }

  //进行有损压缩
 bytearrayoutputstream baos = new bytearrayoutputstream();
 int options_ = 100;
 actualoutbitmap.compress(bitmap.compressformat.jpeg, options_, baos);//质量压缩方法,把压缩后的数据存放到baos中 (100表示不压缩,0表示压缩到最小)

 int baoslength = baos.tobytearray().length;

 while (baoslength / 1024 > maxfilesize) {//循环判断如果压缩后图片是否大于maxmemmorrysize,大于继续压缩
   baos.reset();//重置baos即让下一次的写入覆盖之前的内容
   options_ = math.max(0, options_ - 10);//图片质量每次减少10
   actualoutbitmap.compress(bitmap.compressformat.jpeg, options_, baos);//将压缩后的图片保存到baos中
   baoslength = baos.tobytearray().length;
   if (options_ == 0)//如果图片的质量已降到最低则,不再进行压缩
     break;
 }
 actualoutbitmap.recycle();

 //将bitmap保存到指定路径
 fileoutputstream fos = null;
 string filepath = getoutputfilename(srcimagepath);
 try {
   fos = new fileoutputstream(filepath);
   //包装缓冲流,提高写入速度
   bufferedoutputstream bufferedoutputstream = new bufferedoutputstream(fos);
   bufferedoutputstream.write(baos.tobytearray());
   bufferedoutputstream.flush();
 } catch (filenotfoundexception e) {
   return null;
 } catch (ioexception e) {
   return null;
 } finally {
   if (baos != null) {
     try {
       baos.close();
     } catch (ioexception e) {
       e.printstacktrace();
     }
   }
   if (fos != null) {
     try {
       fos.close();
     } catch (ioexception e) {
       e.printstacktrace();
     }
   }
 }

 //获取位图缩放比例
 private int computsamplesize(bitmapfactory.options options, float reqwidth, float reqheight) {
   float srcwidth = options.outwidth;//20
   float srcheight = options.outheight;//10
   int samplesize = 1;
   if (srcwidth > reqwidth || srcheight > reqheight) {
     int withratio = math.round(srcwidth / reqwidth);
     int heightratio = math.round(srcheight / reqheight);
     samplesize = math.min(withratio, heightratio);
   }
   return samplesize;
 }

压缩比例换算:

float srcwidth = options.outwidth;
float srcheight = options.outheight;
float widthscale = outwidth / srcwidth;//目标/原始 宽比例
float heightscale = outheight / srcheight; //目标原始 高比
//对比宽高比选择较大的一种比例
float scale = widthscale > heightscale ? widthscale : heightscale;
float actualoutwidth = srcwidth;
float actualoutheight = srcheight;
if (scale < 1) {
  actualoutwidth = srcwidth * scale;
  actualoutheight = srcheight * scale;
}

设置缩放比例--生成新的位图

  matrix matrix1 = new matrix();
  matrix1.postscale(scale, scale);// 放大缩小比例
  //生成最终输出的bitmap
  bitmap actualoutbitmap = bitmap.createbitmap(scaledbitmap, 0, 0, scaledbitmap.getwidth(), scaledbitmap.getheight(), matrix1, true);

  if (actualoutbitmap != scaledbitmap) {
    scaledbitmap.recycle();
    scaledbitmap = null;
    system.gc();
  }

参考:https://github.com/guizhigang/lgimagecompressor

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。