Android图片压缩尺寸和质量
Android在处理图片时,如果不进行压缩处理,很容易就出现OOM内存溢出(OutOfMemory)问题,所以无论是第三方图片加载还是自己在处理图片时,都要进行压缩处理。
Android系统中,一张图片所占用的内存大小与图片尺寸(像素点)和色彩格式(质量)有关,所以Android图片压缩分为尺寸压缩和质量压缩:
- 尺寸压缩:一张1920*1080的图片尺寸压缩一半后,即960*540,尺寸和像素点是未压缩前的1/4,所占内存也会变成1/4;
质量压缩:
1.图片的色彩格式压缩即:
-Bitmap.Config ARGB_8888:每个像素占四位,即A=8,R=8,G=8,B=8,那么一个像素点占8+8+8+8=32位
-Bitmap.Config ARGB_4444:每个像素占四位,即A=4,R=4,G=4,B=4,那么一个像素点占4+4+4+4=16位
-Bitmap.Config RGB_565:每个像素占四位,即R=5,G=6,B=5,没有透明度,那么一个像素点占5+6+5=16位
-Bitmap.Config ALPHA_8:每个像素占四位,只有透明度,没有颜色。2.通过 bitmap.compress(CompressFormat format, int quality, OutputStream stream)方法进行压缩,压缩过程可能会损失透明度;
代码
/*** 压缩图片分辨率** @param imgpath 文件路径* @param width 最大输出宽度* @param height 最大输出高度* @return*/public static Bitmap decodeBitmap(String imgpath, int width, int height) {BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;//解码bitmap时可以只返回其高、宽和Mime类型,而不必为其申请内存,从而节省了内存空间。options.inPreferredConfig = Bitmap.Config.RGB_565;//图像质量,565不带透明度BitmapFactory.decodeFile(imgpath, options);//缩放倍数计算int outHeight = options.outHeight;int outWidth = options.outWidth;if (outHeight / height > outWidth / width) {int heightScale = outHeight / height;if (outHeight % height == 0) {options.inSampleSize = heightScale;} else {options.inSampleSize = heightScale + 1;}} else {int widthScale = outWidth / width;if (outWidth % width == 0) {options.inSampleSize = widthScale;} else {options.inSampleSize = widthScale + 1;}}LogUtil.e("options.inSampleSize:" + options.inSampleSize);options.inJustDecodeBounds = false;//加载到内存中Bitmap bitmap = BitmapFactory.decodeFile(imgpath, options);return compressBitmap(bitmap);
// return Bitmap.createScaledBitmap(bitmap, width, height, filter);//将图片缩放到指定大小}/*** 压缩图片质量*/public static Bitmap compressBitmap(Bitmap bitmap) {int maxSize = 300 * 1024;//最大文件大小300kbByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中int quality = 90;while (byteArrayOutputStream.toByteArray().length > maxSize) { // 循环判断如果压缩后图片是否大于maxSize字节,大于继续压缩byteArrayOutputStream.reset(); // 重置baos即清空baosbitmap.compress(Bitmap.CompressFormat.JPEG, quality, byteArrayOutputStream);// 这里压缩quality%,把压缩后的数据存放到byteArrayOutputStream中quality -= 5;}LogUtil.e(byteArrayOutputStream.toByteArray().length/1024+"kb");ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());Bitmap decodeBitmap = BitmapFactory.decodeStream(byteArrayInputStream, null, null);// 把ByteArrayInputStream数据生成bitmaptry {byteArrayInputStream.close();byteArrayOutputStream.close();} catch (IOException e) {e.printStackTrace();}return decodeBitmap;}
================================下面是处理图片时用得到的方法===========================/*** 保存bitmap到文件** @param bitmap* @param filePath*/public static void saveBitmapToFile(Bitmap bitmap, String filePath) {File file = new File(filePath);if (file.exists()) {file.delete();}FileOutputStream out = null;try {out = new FileOutputStream(filePath);bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);out.flush();} catch (FileNotFoundException e1) {e1.printStackTrace();} catch (IOException e2) {e2.printStackTrace();} finally {try {if (out != null)out.close();} catch (Exception e3) {e3.printStackTrace();}}}/*** 读取图片属性:旋转的角度** @param path 图片绝对路径* @return degree旋转的角度*/public static int readPictureDegree(String path) {int degree = 0;try {ExifInterface exifInterface = new ExifInterface(path);int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);switch (orientation) {case ExifInterface.ORIENTATION_ROTATE_90:degree = 90;break;case ExifInterface.ORIENTATION_ROTATE_180:degree = 180;break;case ExifInterface.ORIENTATION_ROTATE_270:degree = 270;break;}} catch (IOException e) {e.printStackTrace();}return degree;}/*** 旋转图片** @param angle* @param bitmap* @return Bitmap*/public static Bitmap rotaingImageView(int angle, Bitmap bitmap) {//旋转图片 动作Matrix matrix = new Matrix();matrix.postRotate(angle);// 创建新的图片Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);return resizedBitmap;}/*** 旋转图片** @param imagePath* @return true:旋转成功;false:无需旋转*/public static boolean rotaingImageView(String imagePath) {int degree = ImageUtils.readPictureDegree(imagePath);if (degree != 0) {Bitmap decodeBitmap = decodeBitmap(imagePath, 1000, 1000);//把图片旋转为正的方向Bitmap newbitmap = rotaingImageView(degree, decodeBitmap);//同步保存文件(文件过大,同步保存会出现ANR异常,可采用异步保存)saveBitmapToFile(newbitmap, imagePath);decodeBitmap.recycle();newbitmap.recycle();
// new Thread(new Runnable() {// @Override
// public void run() {// //异步保存文件
// saveBitmapToFile(newbitmap, new File(curimgpath));
// handler.sendEmptyMessage(0);
// }
// }).start();return true;}return false;}
Android图片压缩尺寸和质量相关推荐
- Android图片压缩(质量压缩和尺寸压缩)
在网上调查了图片压缩的方法并实装后,大致上可以认为有两类压缩:质量压缩(不改变图片的尺寸)和尺寸压缩(相当于是像素上的压缩):质量压缩一般可用于上传大图前的处理,这样就可以节省一定的流量,毕竟现在的手 ...
- Android图片压缩(质量压缩和尺寸压缩)Bitmap转成字符串上传
在网上调查了图片压缩的方法并实装后,大致上可以认为有两类压缩:质量压缩(不改变图片的尺寸)和尺寸压缩(相当于是像素上的压缩):质量压缩一般可用于上传大图前的处理,这样就可以节省一定的流量,毕竟现在的手 ...
- [Android]Android图片压缩(质量压缩和尺寸压缩)
原文地址:http://blog.csdn.net/jdsjlzx/article/details/44228935 在网上调查了图片压缩的方法并实装后,大致上可以认为有两类压缩:质量压缩(不改变图片 ...
- android 图片压缩总结1
在网上调查了图片压缩的方法并实装后,大致上可以认为有两类压缩:质量压缩(不改变图片的尺寸)和尺寸压缩(相当于是像素上的压缩):质量压缩一般可用于上传大图前的处理,这样就可以节省一定的流量,毕竟现在的手 ...
- 最详细的Android图片压缩攻略
Mr.Louis的博客地址: https://blog.csdn.net/weixin_44005563 最近在研究图片压缩原理,看了大量资料,从上层尺寸压缩.质量压缩原理到下层的哈夫曼压缩,走成华大 ...
- android 图片压缩,bitmap压缩总结
1. 图片压缩相关概念 在android开发中,图片加载到内存中通常需要占用大量的内存,导致app性能过度消耗,严重的话可能会导致OOM异常,所以对图片进行优化就尤为重要. android图片压 ...
- Android 图片压缩各种方式
前言:由于公司项目当中需要用到压缩这块的相应技术,之前也做过的图片压缩都不是特别的理想, 所以这次花了很多心思,仔细研究和在网上找到了很多相对应的资料.为了就是 以后再做的时候直接拿来用就可以了! 第 ...
- 可能是最详细的Android图片压缩原理分析(二)—— 鲁班压缩算法解析
本篇文章已授权微信公众号guolin_blog(郭霖)独家发布 稀土掘金链接 前言 通过上一篇,我们了解了一些关于图片压缩的基础知识,这篇文章我们主要讲解一下鲁班压缩的算法逻辑,很多博客都是从Gith ...
- android图片压缩小结
只是作为自己的笔记记录. android开发中,经常会遇到图片的上传后者下载等操作,但是系统分配给当前应用的最大可用内存有限,一般是16M,32M,所以加载大图很容易出现OOM.因此需要对图片进行压缩 ...
最新文章
- C语言程序设计第十章字符串,C语言程序设计(字符串)
- windows installer清理实用工具
- 常用的排序算法的时间复杂度和空间复杂度
- linux命令scp
- php 半角全角,PHP 全角转半角实现代码
- 面试中如何做一个好的自我介绍?
- 蔡高厅老师 - 高等数学阅读笔记 - 05 - 导数和微分 - 01 (22、23、24、25、26、27)
- layui弹出框php,layui中使用的一些弹出框
- 什么样的流_量最容易变现?
- 解决Linux中文乱码问题
- SQL Server 数据库调整表中列的顺序操作
- MySQL及其图形界面navicat的安装
- javplayer 使用教程_童装裁剪之连衣裙打版教程 有图纸
- EXCEL之画带数据点的折线图只有点没有线
- Mysql——》三星索引
- uniapp 视频图片切换,视频高度宽度自适应
- 机器人摘果子看图写话_二年级摘苹果看图写话范例
- 如何在Debian 9上安装和使用Docker
- 杭电OJ1001 C
- Hadoop Streaming 实战: 多路输出