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


猜你喜欢
- 初识LinkedHashMap大多数情况下,只要不涉及线程安全问题,Map基本都可以使用HashMap,不过HashMap有一个问题,就是迭
- gateway、webflux、reactor-netty请求日志输出场景在使用spring cloud gateway时想要输出请求日志,
- spring Session 提供了一套用于管理用户 session 信息的API和实现。Spring Session为企业级Java应用的
- Qt文件操作类QFile简介Qt中使用QFile类来操作文件的输入/输出。继承至QIODevice,QIODevice类是输入/输出设备的基
- 1. 在原有工程目录右键-> new ->Module->:2. 选择library:3. 一路next,最后finish
- 本文实例讲述了Android开发之DatePickerDialog、TimePickerDialog时间日期对话框用法。分享给大家供大家参考
- 前言:synchronized 在 JDK 1.5 之前性能是比较低的,在那时我们通常会选择使用 Lock 来替代 synchronized
- FilterInputStream FilterInputStream 的作用是用来“封装其它的输入流,并为它们提供额外的功能”。它的常用的
- 本文实例分析了C#双缓冲技术。分享给大家供大家参考,具体如下:双缓冲解决闪烁问题。整理:GDI+的双缓冲问题一直以来的误区:.net1.1
- 在C#的数字运算过程中,有时候针对十进制decimal类型的计算需要保留2位有效小数,针对decimal变量保留2位有效小数有多种方法,可以
- 下面还有投票,帮忙投个票👍前言最近在看某个开源项目代码并准备参与其中,代码过了一遍后发现多个自定义的配置文件用来装载业务配置代替数据库查询,
- 本文实例为大家分享了Android实现滑动标尺选择值,效果图1.自定义属性attrs.xml<declare-styleable na
- 1、Java字符串在 Java 中字符串被作为 String 类型的对象处理。 String 类位于 java.lang 包中,默认情况下该
- 一、包含与删除两种方法解析1.boolean contains(Object o);判断集合中是否包含某个元素。package com.bj
- 前言本文主要介绍下Spring事务中的传播行为。事务传播行为是Spring框架独有的事务增强特性,他不属于的事务实际提供方数据库行为。这是S
- 一、WebRequestMethods.Ftp类:表示可与 FTP 请求一起使用的 FTP 协议方法的类型。AppendFile:表示要用于
- 本文实例讲述了C#实现的文件操作封装类。分享给大家供大家参考,具体如下:最近发现群共享里面有个C# 文件操作封装类,其方法是调用Window
- 没有接触过音乐字幕方面知识的话,会对字幕的实现比较迷茫,什么时候转到下一句?看了这篇文章,你就会明白字幕so easy。先来一张效果图:字幕
- 1. 前言对于写Crud的老司机来说时间非常宝贵,一些样板代码写不但费时费力,而且枯燥无味。经常有小伙伴问我,胖哥你怎么天天那么有时间去搞新
- 本文实例为大家分享了Android读取手机通讯录联系人到项目的具体代码,供大家参考,具体内容如下一、主界面代码如下:<LinearLa