下面先讲解关于BitmapFactory.options选项的所有字段:
以上是摘自Android官方的文档:
下面我们说一个问题:
怎么获取图片的大小呢?
首先我们要把这个图片转化成Bitmap,然后在利用Bitmap的getwidth()和getHeight()方法就可以取得图片的宽和高了,
但是此时问题来了,在通过BitmapFactory.decodeFile(Sting file)方法转化成Bitmap时,遇到大一些的图片时,我们经常遇到OOM(out of memory)的问题,怎么避免呢?
此时就用到了上面提到的BitmapFactory.options这个类:
BitmapFactory.options这个类,有一个字段为: inJustDecodeBounds
如果我们把它设为true,那么BitmapFactory.decodeFile(String path, Options opt)并不会真的返回一个Bitmap给你,它仅仅会把它的宽,高取回来给你,这样就不会占用太多的内存,也就不会那么频繁的发生OOM了。
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bmp = BitmapFactory.decodeFile(path, options);
/* 这里返回的bmp是null */
这段代码之后,options.outWidth 和 options.outHeight就是我们想要的宽和高了。
有了宽,高的信息,我们怎样在图片不变形的情况下获取到图片指定大小的缩略图呢?
比如我们需要在图片不变形的前提下得到宽度为200的缩略图。
那么我们需要先计算一下缩放之后,图片的高度是多少
/* 计算得到图片的高度 */
/* 这里需要主意,如果你需要更高的精度来保证图片不变形的话,需要自己进行一下数学运算 */
int height = options.outHeight * 200 / options.outWidth;
options.outWidth = 200;
options.outHeight = height;
/* 这样才能真正的返回一个Bitmap给你 */
options.inJustDecodeBounds = false;
Bitmap bmp = BitmapFactory.decodeFile(path, options);
image.setImageBitmap(bmp);
这样虽然我们可以得到我们期望大小的ImageView
但是在执行BitmapFactory.decodeFile(path, options);时,并没有节约内存。要想节约内存,还需要用到BitmapFactory.Options这个类里的 inSampleSize 这个成员变量。
我们可以根据图片实际的宽高和我们期望的宽高来计算得到这个值。
inSampleSize = options.outWidth / 200;
另外,为了节约内存我们还可以使用下面的几个字段:
options.inPreferredConfig = Bitmap.Config.ARGB_4444; // 默认是Bitmap.Config.ARGB_8888
/* 下面两个字段需要组合使用 */
options.inPurgeable = true;
options.inInputShareable = true;
===========
- 1、设置缩放大小对图片作处理
-
- public Bitmap getBitmapFromFile(File dst, int width, int height) {
- if (null != dst && dst.exists()) {
- BitmapFactory.Options opts = null;
- if (width > 0 && height > 0) {
- opts = new BitmapFactory.Options();
- opts.inJustDecodeBounds = true;
- BitmapFactory.decodeFile(dst.getPath(), opts);
-
- final int minSideLength = Math.min(width, height);
- opts.inSampleSize = computeSampleSize(opts, minSideLength,
- width * height);
- opts.inJustDecodeBounds = false;
- opts.inInputShareable = true;
- opts.inPurgeable = true;
- }
- try {
- return BitmapFactory.decodeFile(dst.getPath(), opts);
- } catch (OutOfMemoryError e) {
- e.printStackTrace();
- }
- }
- return null;
- }
-
-
-
-
-
- public static int computeSampleSize(BitmapFactory.Options options,
- int minSideLength, int maxNumOfPixels) {
- int initialSize = computeInitialSampleSize(options, minSideLength,
- maxNumOfPixels);
-
- int roundedSize;
- if (initialSize <= 8) {
- roundedSize = 1;
- while (roundedSize < initialSize) {
- roundedSize <<= 1;
- }
- } else {
- roundedSize = (initialSize + 7) / 8 * 8;
- }
-
- return roundedSize;
- }
-
- private static int computeInitialSampleSize(BitmapFactory.Options options,
- int minSideLength, int maxNumOfPixels) {
- double w = options.outWidth;
- double h = options.outHeight;
-
- int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math
- .sqrt(w * h / maxNumOfPixels));
- int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(Math
- .floor(w / minSideLength), Math.floor(h / minSideLength));
-
- if (upperBound < lowerBound) {
-
- return lowerBound;
- }
-
- if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
- return 1;
- } else if (minSideLength == -1) {
- return lowerBound;
- } else {
- return upperBound;
- }
- }
-
- public class BitmapUtil {
-
- public 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 heightRatio = Math.round((float) height / (float) reqHeight);
- final int widthRatio = Math.round((float) width / (float) reqWidth);
-
- inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
- }
- return inSampleSize;
-
- }
- public Bitmap decodeSampleFromSD(String path,int sdwidth,int sdheight){
- BitmapFactory.Options options=new BitmapFactory.Options();
- options.inJustDecodeBounds=true;
-
- BitmapFactory.decodeFile(path, options);
- options.inSampleSize=calculateInSampleSize(options, sdwidth, sdheight);
- options.inJustDecodeBounds=false;
- options.inDither=false;
- options.inPreferredConfig=Bitmap.Config.ARGB_8888;
-
-
- return BitmapFactory.decodeFile(path, options);
- }
- }
第一:我们先看下质量压缩方法:
- private Bitmap compressImage(Bitmap image) {
-
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
- int options = 100;
- while ( baos.toByteArray().length / 1024>100) {
- baos.reset();
- image.compress(Bitmap.CompressFormat.JPEG, options, baos);
- options -= 10;
- }
- ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
- Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);
- return bitmap;
- }
第二:图片按比例大小压缩方法(根据路径获取图片并压缩):
- private Bitmap getimage(String srcPath) {
- BitmapFactory.Options newOpts = new BitmapFactory.Options();
-
- newOpts.inJustDecodeBounds = true;
- Bitmap bitmap = BitmapFactory.decodeFile(srcPath,newOpts);
-
- newOpts.inJustDecodeBounds = false;
- int w = newOpts.outWidth;
- int h = newOpts.outHeight;
-
- float hh = 800f;
- float ww = 480f;
-
- int be = 1;
- if (w > h && w > ww) {
- be = (int) (newOpts.outWidth / ww);
- } else if (w < h && h > hh) {
- be = (int) (newOpts.outHeight / hh);
- }
- if (be <= 0)
- be = 1;
- newOpts.inSampleSize = be;
-
- bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
- return compressImage(bitmap);
- }
第三:图片按比例大小压缩方法(根据Bitmap图片压缩):
- private Bitmap comp(Bitmap image) {
-
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
- if( baos.toByteArray().length / 1024>1024) {
- baos.reset();
- image.compress(Bitmap.CompressFormat.JPEG, 50, baos);
- }
- ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
- BitmapFactory.Options newOpts = new BitmapFactory.Options();
-
- newOpts.inJustDecodeBounds = true;
- Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
- newOpts.inJustDecodeBounds = false;
- int w = newOpts.outWidth;
- int h = newOpts.outHeight;
-
- float hh = 800f;
- float ww = 480f;
-
- int be = 1;
- if (w > h && w > ww) {
- be = (int) (newOpts.outWidth / ww);
- } else if (w < h && h > hh) {
- be = (int) (newOpts.outHeight / hh);
- }
- if (be <= 0)
- be = 1;
- newOpts.inSampleSize = be;
-
- isBm = new ByteArrayInputStream(baos.toByteArray());
- bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
- return compressImage(bitmap);
- }