/** 压缩质量:发送前要压缩的图片质量(0~100值) */ public static final int COMPRESS_QUALITY = 75; /** * 此项将用于计算BitmapFactory.Opts的inSimpleSize值,目的是保 * 证加载到内存中的图片不至于过大,此值将会与requestHeight一同计算出最终的inSimpleSize * ,从而使得加载到内存中的Bitmap不至于过大而导致OOM. */ public static final int mRequestWidth = 648; /** * 此项将用于计算BitmapFactory.Opts的inSimpleSize值,目的是保 * 证加载到内存中的图片不至于过大,此值将会与requestWidth一同计算出最终的inSimpleSize * ,从而使得加载到内存中的Bitmap不至于过大而导致OOM. */ public static final int mRequestHeight = 864; /** * 图片裁剪、压缩实现方法。 * * @param imageFilePath 原始(未裁剪尺寸、未压缩质量前)图片的保存路径 * @param savedPath 压缩处理完成后的图片将要保存的路径 * @param savedPath 获取质量压缩后将要保存到的路径(目前的实现即是覆盖原始图片) * @exception Exception 处理过程中发生任何问题都将抛出异常 */ public static void doCompress(String imageFilePath, File savedPath) throws Exception { Bitmap decreasedBm = null; // 【【第一步:压缩尺寸,防止超高分辨率相机拍出的大尺寸图片导致APP出现OOM而崩溃】】 decreasedBm = loadLocalBitmap(imageFilePath // 调整inSimpleSize值,确保在用户载入巨大尺寸时不致于OOM! , computeSampleSize2(imageFilePath, mRequestWidth, mRequestHeight)); // 【【第二步:降低图片质量(从而减小文件大小以便节省网络传输数据量)】】 try { if(savedPath != null) { boolean compressOk = BitmapHelper.saveBitmapToFile(decreasedBm, COMPRESS_QUALITY, savedPath); if(compressOk) Log.d(TAG, "【SendPic】质量压缩完成,压缩质量为:"+COMPRESS_QUALITY+", 临时文件保存路径是:"+savedPath); else Log.w(TAG, "【SendPic】质量压缩失败!!!压缩质量为:"+COMPRESS_QUALITY+", 将要保存路径是:"+savedPath); } else Log.e(TAG, "【SendPic】质量压缩时,压缩完成后将要保存的路径居然是null ?!savedPath="+savedPath); } catch (Exception e) { Log.e(TAG, "【SendPic】降低图片质量的过程中出错了!", e); } } private static BitmapFactory.Options computeSampleSize2(String filePath, int reqWidth, int reqHeight) { BitmapFactory.Options opts = new BitmapFactory.Options(); try { opts.inJustDecodeBounds = true; BitmapFactory.decodeFile(filePath, opts); opts.inSampleSize = computeSampleSize2(opts, reqWidth, reqHeight); } catch (Exception e) { Log.e("computeSampleSize", "计算图片1的inSampleSize时出错.", e.getCause()); } finally { opts.inJustDecodeBounds = false; } Log.d("computeSampleSize", ">> inSampleSize算法[2]计算完成,计算结果是【"+opts.inSampleSize+"】,reqWidth="+ reqWidth+", reqHeight="+reqHeight+", filePath="+filePath); return opts; } private static int computeSampleSize2(BitmapFactory.Options options, int reqWidth, int reqHeight) { // 计算原始图像的高度和宽度 final int height = options.outHeight; final int width = options.outWidth; Log.d("computeSampleSize", ">> inSampleSize算法[2]计算中,[原始options.outWidth="+options.outWidth +", o原始ptions.outHeight="+options.outHeight +"],目标reqWidth="+reqWidth+", 目标reqHeight="+reqHeight+", options="+options); int inSampleSize = 1; //判定,当原始图像的高和宽大于所需高度和宽度时 if (height > reqHeight || width > reqWidth) { final int heightRatio = Math.round((float) height / (float) reqHeight); final int widthRatio = Math.round((float) width / (float) reqWidth); //算出长宽比后去比例小的作为inSamplesize,保障最后imageview的dimension比request的大 inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; //计算原始图片总像素 final float totalPixels = width * height; // Anything more than 2x the requested pixels we'll sample down further //所需总像素*2,长和宽的根号2倍 final float totalReqPixelsCap = reqWidth * reqHeight * 2; //如果遇到很长,或者是很宽的图片时,这个算法比较有用 while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) { inSampleSize++; } } return inSampleSize; }
来源:即时通讯网 - 即时通讯开发者社区!
轻量级开源移动端即时通讯框架。
快速入门 / 性能 / 指南 / 提问
轻量级Web端即时通讯框架。
详细介绍 / 精编源码 / 手册教程
移动端实时音视频框架。
详细介绍 / 性能测试 / 安装体验
基于MobileIMSDK的移动IM系统。
详细介绍 / 产品截图 / 安装体验
一套产品级Web端IM系统。
详细介绍 / 产品截图 / 演示视频
引用此评论
引用:kezhaoyuan 发表于 2017-02-10 14:44 厉害,学习了
引用:jackning 发表于 2017-02-12 14:30 花金币下载了,却发现没有BitmapHelper类
引用:淡云流痕 发表于 2017-08-15 17:08 效果感觉比Luban好 学习了
精华主题数超过100个。
连续任职达2年以上的合格正式版主
为论区做出突出贡献的开发者、版主等。
Copyright © 2014-2024 即时通讯网 - 即时通讯开发者社区 / 版本 V4.4
苏州网际时代信息科技有限公司 (苏ICP备16005070号-1)
Processed in 0.140624 second(s), 38 queries , Gzip On.