广告
返回顶部
首页 > 资讯 > 移动开发 >android 将图片压缩到指定的大小的示例
  • 607
分享到

android 将图片压缩到指定的大小的示例

压缩示例图片图片压缩Android 2022-06-06 04:06:12 607人浏览 泡泡鱼
摘要

从网上收集后自己写的一个方法; 1.首先是一个根据分辨率压缩的类,首先对图片进行一次压缩 private static Bitmap compressByResolu

从网上收集后自己写的一个方法;

1.首先是一个根据分辨率压缩的类,首先对图片进行一次压缩


  
 private static Bitmap compressByResolution(String imgPath, int w, int h) {
  BitmapFactory.Options opts = new BitmapFactory.Options();
  opts.inJustDecodeBounds = true;
  BitmapFactory.decodeFile(imgPath, opts);
  int width = opts.outWidth;
  int height = opts.outHeight;
  int widthScale = width / w;
  int heightScale = height / h;
  int scale;
  if (widthScale < heightScale) { //保留压缩比例小的
   scale = widthScale;
  } else {
   scale = heightScale;
  }
  if (scale < 1) {
   scale = 1;
  }
  Log.i(TAG,"图片分辨率压缩比例:" + scale);
  opts.inSampleSize = scale;
  opts.inJustDecodeBounds = false;
  Bitmap bitmap = BitmapFactory.decodeFile(imgPath, opts);
  return bitmap;
 }

2.第二就是循环对图片的压缩,直到压缩到指定的大小以下为止(重要!)


 
 public static boolean compressBitmap(String srcPath, int ImageSize, String savePath) {
  int subtract;
  Log.i(TAG, "图片处理开始..");
  Bitmap bitmap = compressByResolution(srcPath, 1024, 720); //分辨率压缩
  if (bitmap == null) {
   Log.i(TAG, "bitmap 为空");
   return false;
  }
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  int options = 100;
  bitmap.compress(Bitmap.CompressFORMat.JPEG, options, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
  Log.i(TAG, "图片分辨率压缩后:" + baos.toByteArray().length / 1024 + "KB");
  while (baos.toByteArray().length > ImageSize * 1024) { //循环判断如果压缩后图片是否大于ImageSize kb,大于继续压缩
   subtract = setSubstractSize(baos.toByteArray().length / 1024);
   baos.reset();//重置baos即清空baos
   options -= subtract;//每次都减少10
   bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中
   Log.i(TAG, "图片压缩后:" + baos.toByteArray().length / 1024 + "KB");
  }
  Log.i(TAG, "图片处理完成!" + baos.toByteArray().length / 1024 + "KB");
  try {
   FileOutputStream fos = new FileOutputStream(new File(savePath));//将压缩后的图片保存的本地上指定路径中
   fos.write(baos.toByteArray());
   fos.flush();
   fos.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
  if (bitmap != null) {
   bitmap.recycle();
  }
  return true; //压缩成功返回ture
 }

在这其中 


 
 private static int setSubstractSize(int imageMB) {
  if (imageMB > 1000) {
   return 60;
  } else if (imageMB > 750) {
   return 40;
  } else if (imageMB > 500) {
   return 20;
  } else {
   return 10;
  }
 }

这个方法用来动态设置每次压缩的比例,主要用于提升压缩的时间,这其中的数值是我大概测试出来的可以修改成你认为比较合适的

3.最后

压缩图片费时又费内存,很明显执行的时候需要在子线程中完成,如果需要的话可以加一个压缩完成的监听

下载地址:CommonUtils_jb51.rar

您可能感兴趣的文章:Android小知识之图片的3种压缩方式小结Android LuBan与Compressor图片压缩方式Android开发之图片压缩工具类完整实例详解android 通过uri获取bitmap图片并压缩android递归压缩上传多张图片到七牛的实例代码浅析Android 快速实现图片压缩与上传功能Android图片压缩方法并压缩到指定大小Android图片压缩以及优化实例Android实现图片压缩(bitmap的六种压缩方式)Android整理好的图片压缩工具类


--结束END--

本文标题: android 将图片压缩到指定的大小的示例

本文链接: https://www.lsjlt.com/news/22337.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作