整理文档,搜刮出一个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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小牛知识库。
本文向大家介绍Android图片压缩的实例详解,包括了Android图片压缩的实例详解的使用技巧和注意事项,需要的朋友参考一下 Android图片压缩的实例详解 在做微信分享的时候,由于分享的缩略图要求不得大于32K,否则不能调起微信,所以总结了一下Android图片的压缩问题,大部分资料都是来自网上各位的分享,自己只是完善或修改了一下,本着继续分享的精神,也方便自己记忆,于是总结如下。 andr
本文向大家介绍PHP实现图片压缩的两则实例,包括了PHP实现图片压缩的两则实例的使用技巧和注意事项,需要的朋友参考一下 本文介绍了PHP实现图片压缩的两种方法,读者可以根据具体应用参考或加以改进,以适应自身应用需求!废话不多说,主要代码部分如下: 实例1: 实例2:
本文向大家介绍php实现等比例压缩图片,包括了php实现等比例压缩图片的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了php实现等比例压缩图片的具体代码,供大家参考,具体内容如下 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。
本文向大家介绍python 实现图片批量压缩的示例,包括了python 实现图片批量压缩的示例的使用技巧和注意事项,需要的朋友参考一下 项目中大量用到图片加载,由于图片太大,加载速度很慢,因此需要对文件进行统一压缩 一:导入包 二:获取图片文件的大小 三:拼接输出文件地址 四:压缩文件到指定大小,我期望的是150KB,step和quality可以修改到最合适的数值 五:修改图片尺寸,如果同时有修改
本文向大家介绍python如何实现图片压缩,包括了python如何实现图片压缩的使用技巧和注意事项,需要的朋友参考一下 本工具是通过将图片上传到第三方网站tinypng,进行压缩后下载,覆盖本地图片,tinypng是一个强大的图片处理网站,目前最可靠的无损压缩网站。 代码如下: 改进版 优化点: 1.遍历完成本地文件夹再去上传网站 2.所有图片压缩完成再去下载 3.启动多线程下载 4.设定时间为加
本文向大家介绍c# 如何实现图片压缩,包括了c# 如何实现图片压缩的使用技巧和注意事项,需要的朋友参考一下 一般在web应用中,对客户端提交上来的图片肯定需要进行压缩的。尤其是比较大的图片,如果不经过压缩会导致页面变的很大,打开速度比较慢,当然了如果是需要高质量的图片也得需要生产缩略图。 一般在web应用中,对客户端提交上来的图片肯定需要进行压缩的。尤其是比较大的图片,如果不经过压缩会导致页面变的