在网上调查了图片压缩的方法并实装后,大致上可以认为有两类压缩:质量压缩(不改变图片的尺寸)和尺寸压缩(相当于是像素上的压缩);质量压缩一般可用于上传大图前的处理,这样就可以节省一定的流量,毕竟现在的手机拍照都能达到3M左右了,尺寸压缩一般可
在网上调查了图片压缩的方法并实装后,大致上可以认为有两类压缩:质量压缩(不改变图片的尺寸)和尺寸压缩(相当于是像素上的压缩);质量压缩一般可用于上传大图前的处理,这样就可以节省一定的流量,毕竟现在的手机拍照都能达到3M左右了,尺寸压缩一般可用于生成缩略图。
两种方法都实装在了我的项目中,结果却发现在质量压缩的模块中,本来1.9M的图片压缩后反而变成3M多了,很是奇怪,再做了进一步调查终于知道原因了。下面这个博客说的比较清晰:
android图片压缩总结
总 结来看,图片有三种存在形式:硬盘上时是file,网络传输时是stream,内存中是stream或bitmap,所谓的质量压缩,它其实只能实现对 file的影响,你可以把一个file转成bitmap再转成file,或者直接将一个bitmap转成file时,这个最终的file是被压缩过的,但 是中间的bitmap并没有被压缩(或者说几乎没有被压缩,我不确定),因为bigmap在内存中的大小是按像素计算的,也就是width * height,对于质量压缩,并不会改变图片的像素,所以就算质量被压缩了,但是bitmap在内存的占有率还是没变小,但你做成file时,它确实变小 了;
而尺寸压缩由于是减小了图片的像素,所以它直接对bitmap产生了影响,当然最终的file也是相对的变小了;
import javaioByteArrayInputStream; import javaioByteArrayOutputStream; import javaioFile; import javaioFileNotFoundException; import javaioFileOutputStream; import javaioIOException; import AndroidgraphicsBitmap; import androidgraphicsBitmapConfig; import androidgraphicsBitmapFactory; public class ImageFactory { public Bitmap getBitmap(String imgPath) { // Get bitmap through image path BitmapFactoryOptions newOpts = new BitmapFactoryOptions(); newOptsinJustDecodeBounds = false; newOptsinPurgeable = true; newOptsinInputShareable = true; // Do not compress newOptsinSampleSize = 1; newOptsinPreferredConfig = ConfigRGB_565; return BitmapFactorydecodeFile(imgPath, newOpts); } public void storeImage(Bitmap bitmap, String outPath) throws FileNotFoundException { FileOutputStream os = new FileOutputStream(outPath); bitmapcompress(BitmapCompressFORMatJPEG, 100, os); } public Bitmap ratio(String imgPath, float pixelW, float pixelH) { BitmapFactoryOptions newOpts = new BitmapFactoryOptions(); // 开始读入图片,此时把optionsinJustDecodeBounds 设回true,即只读边不读内容 newOptsinJustDecodeBounds = true; newOptsinPreferredConfig = ConfigRGB_565; // Get bitmap info, but notice that bitmap is null now Bitmap bitmap = BitmapFactorydecodeFile(imgPath,newOpts); newOptsinJustDecodeBounds = false; int w = newOptsoutWidth; int h = newOptsoutHeight; // 想要缩放的目标尺寸 float hh = pixelH;// 设置高度为240f时,可以明显看到图片缩小了 float ww = pixelW;// 设置宽度为120f,可以明显看到图片缩小了 // 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可 int be = 1;//be=1表示不缩放 if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放 be = (int) (newOptsoutWidth / ww); } else if (w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放 be = (int) (newOptsoutHeight / hh); } if (be <= 0) be = 1; newOptsinSampleSize = be;//设置缩放比例 // 开始压缩图片,注意此时已经把optionsinJustDecodeBounds 设回false了 bitmap = BitmapFactorydecodeFile(imgPath, newOpts); // 压缩好比例大小后再进行质量压缩 // return compress(bitmap, maxSize); // 这里再进行质量压缩的意义不大,反而耗资源,删除 return bitmap; } public Bitmap ratio(Bitmap image, float pixelW, float pixelH) { ByteArrayOutputStream os = new ByteArrayOutputStream(); imagecompress(BitmapCompressFormatJPEG, 100, os); if( ostoByteArray()length / 1024>1024) {//判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactorydecodeStream)时溢出 osreset();//重置baos即清空baos imagecompress(BitmapCompressFormatJPEG, 50, os);//这里压缩50%,把压缩后的数据存放到baos中 } ByteArrayInputStream is = new ByteArrayInputStream(ostoByteArray()); BitmapFactoryOptions newOpts = new BitmapFactoryOptions(); //开始读入图片,此时把optionsinJustDecodeBounds 设回true了 newOptsinJustDecodeBounds = true; newOptsinPreferredConfig = ConfigRGB_565; Bitmap bitmap = BitmapFactorydecodeStream(is, null, newOpts); newOptsinJustDecodeBounds = false; int w = newOptsoutWidth; int h = newOptsoutHeight; float hh = pixelH;// 设置高度为240f时,可以明显看到图片缩小了 float ww = pixelW;// 设置宽度为120f,可以明显看到图片缩小了 //缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可 int be = 1;//be=1表示不缩放 if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放 be = (int) (newOptsoutWidth / ww); } else if (w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放 be = (int) (newOptsoutHeight / hh); } if (be <= 0) be = 1; newOptsinSampleSize = be;//设置缩放比例 //重新读入图片,注意此时已经把optionsinJustDecodeBounds 设回false了 is = new ByteArrayInputStream(ostoByteArray()); bitmap = BitmapFactorydecodeStream(is, null, newOpts); //压缩好比例大小后再进行质量压缩 // return compress(bitmap, maxSize); // 这里再进行质量压缩的意义不大,反而耗资源,删除 return bitmap; } public void compressAndGenImage(Bitmap image, String outPath, int maxSize) throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(); // scale int options = 100; // Store the bitmap into output stream(no compress) imagecompress(BitmapCompressFormatJPEG, options, os); // Compress by loop while ( ostoByteArray()length / 1024 > maxSize) { // Clean up os osreset(); // interval 10 options -= 10; imagecompress(BitmapCompressFormatJPEG, options, os); } // Generate compressed image file FileOutputStream fos = new FileOutputStream(outPath); foswrite(ostoByteArray()); fosflush(); fosclose(); } public void compressAndGenImage(String imgPath, String outPath, int maxSize, boolean needsDelete) throws IOException { compressAndGenImage(getBitmap(imgPath), outPath, maxSize); // Delete original file if (needsDelete) { File file = new File (imgPath); if (fileexists()) { filedelete(); } } } public void ratioAndGenThumb(Bitmap image, String outPath, float pixelW, float pixelH) throws FileNotFoundException { Bitmap bitmap = ratio(image, pixelW, pixelH); storeImage( bitmap, outPath); } public void ratioAndGenThumb(String imgPath, String outPath, float pixelW, float pixelH, boolean needsDelete) throws FileNotFoundException { Bitmap bitmap = ratio(imgPath, pixelW, pixelH); storeImage( bitmap, outPath); // Delete original file if (needsDelete) { File file = new File (imgPath); if (fileexists()) { filedelete(); } } } }
--结束END--
本文标题: Android图片压缩(质量压缩和尺寸压缩)
本文链接: https://www.lsjlt.com/news/226203.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-04-30
2024-04-30
2024-04-30
2024-04-30
2024-04-30
2024-04-30
2024-04-30
2024-04-30
2024-04-30
2024-04-30
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0