Bitmap图片压缩

如果图片像素过大,使用BitmapFactory 类的方法实例化Bitmap 的过程中,需要大于8M 的内存空间,就必定会发生OutOfMemory 异常。这个时候该如何处理呢?如果有这种情况,则可以将图片缩小,以减少载入图片过程中的内存的使用,避免异常发生。
使用BitmapFactory.Options 设置inSampleSize 就可以缩小图片。属性值inSampleSize 表示缩略图大小为原始图片大小的几分之一。

即如果这个值为2,则取出的缩略图的宽和高都是原始图片的1/2,图片的大小就为原始大小的1/4。
如果知道图片的像素过大,就可以对其进行缩小。那么如何才知道图片过大呢?
使用BitmapFactory.Options 设置inJustDecodeBounds 为true 后,再使用
decodeFile()等方法,并不会真正的分配空间,即解码出来的Bitmap 为null,
但是可计算出原始图片的宽度和高度, 即options.outWidth 和
options.outHeight。通过这两个值,就可以知道图片是否过大了。
BitmapFactory.Options opts = new BitmapFactory.Options();
// 设置inJustDecodeBounds 为true
opts.inJustDecodeBounds = true;
// 使用decodeFile 方法得到图片的宽和高
BitmapFactory.decodeFile(path, opts);
// 打印出图片的宽和高
Log.d("example", opts.outWidth + "," + opts.outHeight);
在实际项目中,可以利用上面的代码,先获取图片真实的宽度和高度,然后判断是
否需要跑缩小。如果不需要缩小,
设置inSampleSize 的值为1。如果需要缩小,则动态计算并设置inSampleSize
的值,对图片进行缩小。需要注意的是,
在下次使用BitmapFactory 的decodeFile()等方法实例化Bitmap 对象前,别忘
记将opts.inJustDecodeBound 设置回false。否则获取的bitmap 对象还是null。
经验分享:
如果程序的图片的来源都是程序包中的资源,或者是自己服务器上的图片,图片的
大小是开发者可以调整的,那么一般来说,
就只需要注意使用的图片不要过大,并且注意代码的质量,及时回收Bitmap 对象,
就能避免OutOfMemory 异常的发生。
如果程序的图片来自外界,这个时候就特别需要注意OutOfMemory 的发生。一
个是如果载入的图片比较大,就需要先缩小;另一个是一定要捕获异常,避免程序Crash。
4.图片太大,进行压缩,BitMapFactory.Options 重新设置宽,高,像素。。。
package cn.itcast.androidtext;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class BitmapUtil {
/**
* 得到图片的缩略图
*
* @param pathName
* 路径
* @param reqWidth
* 宽
* @param reqHeight
* 高
* @return
*/
public static Bitmap decodeSampledBitmapFromFd(String pathName, int
reqWidth, int reqHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(pathName, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
options.inJustDecodeBounds = false;
Bitmap src = BitmapFactory.decodeFile(pathName, options);
return createScaleBitmap(src, reqWidth, reqHeight);
}
// 如果是放大图片,filter 决定是否平滑,如果是缩小图片,filter 无影响
private static Bitmap createScaleBitmap(Bitmap src, int dstWidth, int
dstHeight) {
Bitmap dst = Bitmap.createScaledBitmap(src, dstWidth, dstHeight,
false);
if (src != dst) { // 如果没有缩放,那么不回收
src.recycle(); // 释放Bitmap 的native 像素数组
}
return dst;
}
private static int calculateInSampleSize(BitmapFactory.Options options, int
reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
while ((halfHeight / inSampleSize) > reqHeight && (halfWidth /
inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
/**
* 从Resources 中加载图片
*
* @param res
* @param resId
* @param reqWidth
* @param reqHeight
* @return
*/
public static Bitmap decodeSampledBitmapFromResource(Resources res,
int resId, int reqWidth, int reqHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options); // 读取图片长款
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight); // 计算inSampleSize
options.inJustDecodeBounds = false;
Bitmap src = BitmapFactory.decodeResource(res, resId, options); // 载
入一个稍大的缩略图
return createScaleBitmap(src, reqWidth, reqHeight); // 进一步得到目标
大小的缩略图
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值