android 图片上传前压缩


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

详解:

  • 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 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();    }

更多相关文章

  1. android as gradle 使用过称中 错误处理
  2. android轮播图封装 (网络图片glide解析,手势触摸,三种viewpager
  3. 新浪下载图片的ProgressBar进度样式源码
  4. Hello Android(安卓)- Bitmap转换为黑白的灰度图和加圆角效果
  5. 【Android(安卓)开发教程】Gallery和ImageView
  6. Android(安卓)Bitmap 缩放 旋转 水印 裁剪操作
  7. android  打开多个Activity,返回到第一个Activity的问题
  8. Android(安卓)批量上传sd卡图片
  9. android 根据图片路径获取图片缩略图

随机推荐

  1. listview android:cacheColorHint,androi
  2. [置顶] Android按返回键退出程序但不销毁
  3. 如何关联androidSDK源代码
  4. 编写android jni代码时遇到的问题
  5. Android设定屏幕只竖屏或只横屏的两种方
  6. Android(安卓)中文 API (21) ―― DigitalC
  7. Android(安卓)全局Activity动画设置
  8. 浅谈Android(安卓)Surface机制
  9. Android(安卓)Activity四种加载方式
  10. 9.RatingBar