Android整理好的图片压缩工具类
作者:franksight 发布时间:2023-11-06 03:24:19
标签:android,图片,压缩,性能优化
Android设备的内存有限,对于大图片,必须进行压缩后再进行显示,否则会出现内存溢出:OOM;
处理策略:
1.使用缩略图(Thumbnails);
Android系统会给检测到的图片创建缩略图;可以操作Media内容提供者中的Image对图片进行操作;
2.手动压缩:
(1)根据图片和屏幕尺寸,等比压缩,完美显示;
(2)降低图片质量,压缩图片大小;
以下是自己整理的小工具类(对于按比例缩放后,在此并未再进行质量缩放,此时图片大小有可能超出我们期望的限制;假如我们有严格的大小限制需求,可先进行按比例缩放后,判断此时图片大小是否超出限制;如果超出限制,对其再进行质量缩放即可。建议使用按比例缩放,按质量缩放很有可能导致图片失真。)
</pre><p><pre name="code" class="java">package com.util;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.media.ExifInterface;
/**
* 图片压缩工具类
* @author 丶Life_
*/
public class ImageCompressUtil {
/**
* 通过降低图片的质量来压缩图片
* @param bmp
* 要压缩的图片位图对象
* @param maxSize
* 压缩后图片大小的最大值,单位KB
* @return 压缩后的图片位图对象
*/
public static Bitmap compressByQuality(Bitmap bitmap, int maxSize) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int quality = 100;
bitmap.compress(CompressFormat.JPEG, quality, baos);
System.out.println("图片压缩前大小:" + baos.toByteArray().length + "byte");
boolean isCompressed = false;
while (baos.toByteArray().length / 1024 > maxSize) {
quality -= 10;
baos.reset();
bitmap.compress(CompressFormat.JPEG, quality, baos);
System.out.println("质量压缩到原来的" + quality + "%时大小为:"
+ baos.toByteArray().length + "byte");
isCompressed = true;
}
System.out.println("图片压缩后大小:" + baos.toByteArray().length + "byte");
if (isCompressed) {
Bitmap compressedBitmap = BitmapFactory.decodeByteArray(
baos.toByteArray(), 0, baos.toByteArray().length);
recycleBitmap(bitmap);
return compressedBitmap;
} else {
return bitmap;
}
}
/**
* 传入图片url,通过压缩图片的尺寸来压缩图片大小
* @param pathName 图片的完整路径
* @param targetWidth 缩放的目标宽度
* @param targetHeight 缩放的目标高度
* @return 缩放后的图片
*/
public static Bitmap compressBySize(String pathName, int targetWidth,
int targetHeight) {
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;// 不去真的解析图片,只是获取图片的头部信息,包含宽高等;
Bitmap bitmap = BitmapFactory.decodeFile(pathName, opts);
// 得到图片的宽度、高度;
int imgWidth = opts.outWidth;
int imgHeight = opts.outHeight;
// 分别计算图片宽度、高度与目标宽度、高度的比例;取大于等于该比例的最小整数;
int widthRatio = (int) Math.ceil(imgWidth / (float) targetWidth);
int heightRatio = (int) Math.ceil(imgHeight / (float) targetHeight);
if (widthRatio > 1 || heightRatio > 1) {
if (widthRatio > heightRatio) {
opts.inSampleSize = widthRatio;
} else {
opts.inSampleSize = heightRatio;
}
}
// 设置好缩放比例后,加载图片进内容;
opts.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(pathName, opts);
return bitmap;
}
/**
* 传入bitmap,通过压缩图片的尺寸来压缩图片大小
* @param bitmap 要压缩图片
* @param targetWidth 缩放的目标宽度
* @param targetHeight 缩放的目标高度
* @return 缩放后的图片
*/
public static Bitmap compressBySize(Bitmap bitmap, int targetWidth,
int targetHeight) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, baos);
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
bitmap = BitmapFactory.decodeByteArray(baos.toByteArray(), 0,
baos.toByteArray().length, opts);
// 得到图片的宽度、高度;
int imgWidth = opts.outWidth;
int imgHeight = opts.outHeight;
// 分别计算图片宽度、高度与目标宽度、高度的比例;取大于该比例的最小整数;
int widthRatio = (int) Math.ceil(imgWidth / (float) targetWidth);
int heightRatio = (int) Math.ceil(imgHeight / (float) targetHeight);
if (widthRatio > 1 || heightRatio > 1) {
if (widthRatio > heightRatio) {
opts.inSampleSize = widthRatio;
} else {
opts.inSampleSize = heightRatio;
}
}
// 设置好缩放比例后,加载图片进内存;
opts.inJustDecodeBounds = false;
Bitmap compressedBitmap = BitmapFactory.decodeByteArray(
baos.toByteArray(), 0, baos.toByteArray().length, opts);
recycleBitmap(bitmap);
return compressedBitmap;
}
/**
* 通过压缩图片的尺寸来压缩图片大小,通过读入流的方式,可以有效防止网络图片数据流形成位图对象时内存过大的问题;
* @param InputStream 要压缩图片,以流的形式传入
* @param targetWidth 缩放的目标宽度
* @param targetHeight 缩放的目标高度
* @return 缩放后的图片
* @throws IOException 读输入流的时候发生异常
*/
public static Bitmap compressBySize(InputStream is, int targetWidth,
int targetHeight) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buff = new byte[1024];
int len = 0;
while ((len = is.read(buff)) != -1) {
baos.write(buff, 0, len);
}
byte[] data = baos.toByteArray();
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,
opts);
// 得到图片的宽度、高度;
int imgWidth = opts.outWidth;
int imgHeight = opts.outHeight;
// 分别计算图片宽度、高度与目标宽度、高度的比例;取大于该比例的最小整数;
int widthRatio = (int) Math.ceil(imgWidth / (float) targetWidth);
int heightRatio = (int) Math.ceil(imgHeight / (float) targetHeight);
if (widthRatio > 1 || heightRatio > 1) {
if (widthRatio > heightRatio) {
opts.inSampleSize = widthRatio;
} else {
opts.inSampleSize = heightRatio;
}
}
// 设置好缩放比例后,加载图片进内存;
opts.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opts);
return bitmap;
}
/**
* 旋转图片摆正显示
* @param srcPath
* @param bitmap
* @return
*/
public static Bitmap rotateBitmapByExif(String srcPath, Bitmap bitmap) {
ExifInterface exif;
Bitmap newBitmap = null;
try {
exif = new ExifInterface(srcPath);
if (exif != null) { // 读取图片中相机方向信息
int ori = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
int digree = 0;
switch (ori) {
case ExifInterface.ORIENTATION_ROTATE_90:
digree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
digree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
digree = 270;
break;
}
if (digree != 0) {
Matrix m = new Matrix();
m.postRotate(digree);
newBitmap = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(), m, true);
recycleBitmap(bitmap);
return newBitmap;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
/**
* 回收位图对象
* @param bitmap
*/
public static void recycleBitmap(Bitmap bitmap) {
if (bitmap != null && !bitmap.isRecycled()) {
bitmap.recycle();
System.gc();
bitmap = null;
}
}
}
来源:https://blog.csdn.net/whj9073/article/details/53196357
![](https://www.aspxhome.com/images/zang.png)
![](https://www.aspxhome.com/images/jiucuo.png)
猜你喜欢
- 调试的时候,在循环里增加条件判断,可以极大的提高效率,心情也能愉悦。以下介绍下IDEA使用条件【Condition】断点的方法1、编写一段样
- Spring Data JPA设置字段默认值在Column注解中写入以下两个属性,为什么自己查一下文档吧,即可实现字段默认值设置,并顺带解决
- 本文实例讲述了C#中Winform窗体Form的关闭按钮变灰色的方法,对C#程序设计有一定的借鉴价值,分享给大家供大家参考之用。具体方法如下
- 目前的应用市场上,使用毛玻璃效果的APP随处可见,比如用过微信语音聊天的人可以发现,语音聊天页面就使用了高斯模糊效果。先看下效果图:&nbs
- 今天从数据库生成了一份数据字典,但是没有备注,所以需要程序把表格都读出来。用到了下面的代码,亲测可用~~object oFileName =
- 我们知道,进入百度图片后,输入一个关键字后,首先看到的是很多缩略图,当我们点击某张缩略图时,我们就可以进入到大图显示页面,在大图显示页面,中
- 高斯模糊高斯模糊(英语:Gaussian Blur),也叫高斯平滑,是在Adobe Photoshop、GIMP以及Paint.NET等图像
- 找不同给定两个字符串 s 和 t ,它们只包含小写字母。字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。
- -----------------------------------------------------操作---------------
- 一、概述从3.1版本起,Spring框架就已经支持将缓存添加到现有的Spring应用中,和事务支持一样,缓存抽象允许在对代码影响最小的情况下
- 前言:回顾上一节服务器配置的内容,我们已经可以自己完成公众号服务器的配置。配置完成之后,我们就可以通过调用的方式,完成对消息管理的处理。当用
- 前言上文讲的MyBatis部署运行且根据官网运行了一个demo:一步到位部署运行MyBatis3源码<保姆级>jdbc再贴一个J
- mybatis自带对枚举的处理类org.apache.ibatis.type.EnumOrdinalTypeHandler<E>
- SnowFlake 算法,是 Twitter 开源的分布式 id 生成算法。其核心思想就是:使用一个 64 bit 的 long 型的数字作
- 特别是针对循环或timer处理中需要在窗体控件显示数据时,因后台处理过度繁忙而出现没刷新或者假死现象时,可以使用Application.Do
- ThreadLocal 看名字 就可以看出一点头绪来,线程本地。来看一下java对他的描述:该类提供线程本地变量。这些变量与它们的正常对应变
- Maven 组件界面介绍 如上图标注 1 所示,为常用的 Maven 工具栏,其中最常用的有:第一个按钮:Reimport All
- 获取sdcard目录 public static String getSDPath() { File sdDir
- 通过AIDL接口在进程间传递数据,记录在开发中遇到的一写问题AIDL支持数据类型如下:1. Java 的原生类型2. String 和Cha
- 欲达此目的,可以采用下列两种作法: ◆使用XmlConvert类。 ◆将一个XSLT转换套用至DataSet数据的XML表示。 程序范例 本