这篇文章将为大家详细讲解有关Android应用中实现图片压缩的方法有哪些,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。Android图片压缩几种方式总结图片压缩在Android开发中很常见也
这篇文章将为大家详细讲解有关Android应用中实现图片压缩的方法有哪些,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
Android图片压缩几种方式总结
图片压缩在Android开发中很常见也很重要,防止图片的OOM也是压缩的重要原因。
首先看下Bitmap图片文件的大小的决定因素:
Bitmap所占用的内存 = 图片长度 x 图片宽度 x 一个像素点占用的字节数。3个参数,任意减少一个的值,就达到了压缩的效果。
接下来看下Bitmap图片的几种格式的特点:
ALPHA_8
表示8位Alpha位图,即A=8,一个像素点占用1个字节,它没有颜色,只有透明度
ARGB_4444
表示16位ARGB位图,即A=4,R=4,G=4,B=4,一个像素点占4+4+4+4=16位,2个字节
ARGB_8888
表示32位ARGB位图,即A=8,R=8,G=8,B=8,一个像素点占8+8+8+8=32位,4个字节
RGB_565
表示16位RGB位图,即R=5,G=6,B=5,它没有透明度,一个像素点占5+6+5=16位,2个字节
如果进行图片格式的压缩的话,一般情况下都是ARGB_8888转为RGB565进行压缩。
写了一个工具类,基本上列举了android上图片的几种基本压缩方式:
质量压缩
采样率压缩
尺寸压缩
Matrix压缩
图片格式的压缩,例如PNG和JPG保存后的图片大小是不同的
public class Utils { public static Bitmap getBitmap(Bitmap bitmap, int sampleSize) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = sampleSize; ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFORMat.JPEG, 100, baos); byte[] bytes = baos.toByteArray(); Bitmap bit = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options); Log.i("info", "图片大小:" + bit.getByteCount());//2665296 10661184 return bit; } public static Bitmap compressByQuality(Bitmap bitmap, int quality) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos); byte[] bytes = baos.toByteArray(); Bitmap bit = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); Log.i("info", "图片大小:" + bit.getByteCount());//10661184 return bit; } public static Bitmap compressByQuality(Bitmap src, long maxByteSize) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); int quality = 100; src.compress(Bitmap.CompressFormat.JPEG, quality, baos); while (baos.toByteArray().length > maxByteSize && quality > 0) { baos.reset(); src.compress(Bitmap.CompressFormat.JPEG, quality -= 5, baos); } if (quality < 0) return null; byte[] bytes = baos.toByteArray(); Bitmap bit = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); return bit; } public static Bitmap compressByFormat(Bitmap bitmap, int format) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); byte[] bytes = baos.toByteArray(); Bitmap bit = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); Log.i("info", "图片大小:" + bit.getByteCount());//10661184 return bit; } public static Bitmap getBitmapBySize(Bitmap bitmap, float scaleWidth, float scaleHeight) { Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); Bitmap bit = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false); Log.i("info", "图片大小:" + bit.getByteCount()); return bit; } public static Bitmap getBitmapByFormatConfig(String path, Bitmap.Config config) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = config; Bitmap bitmap = BitmapFactory.decodeFile(path, options); Log.i("info", "图片大小:" + bitmap.getByteCount()); return bitmap; } public static Bitmap getBitmapByScaleSize(Bitmap bitmap, int width, int height) { Bitmap bit = Bitmap.createScaledBitmap(bitmap, width, height, true); Log.i("info", "图片大小:" + bit.getByteCount()); return bit; } public static Bitmap getBitmapByFormat(Bitmap bitmap, Bitmap.CompressFormat format) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(format, 100, baos); byte[] bytes = baos.toByteArray(); Bitmap bit = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); Log.i("info", "图片大小:" + bit.getByteCount()); return bit; } public static Bitmap getBitmap(String filePath, int inSampleSize) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(filePath, options);//此时不耗费和占用内存 options.inSampleSize = inSampleSize; options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(filePath, options); } public static Bitmap getBitmap(String filePath) { return BitmapFactory.decodeFile(filePath); } public static Bitmap view2Bitmap(View view) { if (view == null) return null; Bitmap ret = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); canvas canvas = new Canvas(ret); Drawable bgDrawable = view.getBackground(); if (bgDrawable != null) { bgDrawable.draw(canvas); } else { canvas.drawColor(Color.WHITE); } view.draw(canvas); return ret; } public static void saveBitmap(Bitmap bitmap) { File file = new File(Environment.getExternalStorageDirectory() + "/img.jpg"); try { FileOutputStream fileOutputStream = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream); fileOutputStream.flush(); fileOutputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void saveBitmap(Bitmap bitmap,Bitmap.CompressFormat format) { File file = new File(Environment.getExternalStorageDirectory() + "/img.jpg"); try { FileOutputStream fileOutputStream = new FileOutputStream(file); bitmap.compress(format, 100, fileOutputStream); fileOutputStream.flush(); fileOutputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
--结束END--
本文标题: Android应用中实现图片压缩的方法有哪些
本文链接: https://www.lsjlt.com/news/222813.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-02
2024-05-02
2024-05-02
2024-05-02
2024-05-02
2024-05-02
2024-05-02
2024-05-02
2024-05-02
2024-05-02
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0