我正在寻找一种从旋转的文件(0,90,180,270度)解码位图的方法。我需要它,因为图像很大,如果我解码位图,然后使用像
这样的东西Bitmap rotatedBitmap=Bitmap.createBitmap(source, x, y, width, height, m, filter)
有时候两个位图都在内存中,并且存在内存不足的风险。
有什么想法吗?感谢。
答案 0 :(得分:1)
也许这就是你要找的东西?
//decodes image and scales it to reduce memory consumption
//NOTE: if the image has dimensions which exceed int width and int height
//its dimensions will be altered.
private Bitmap decodeToLowResImage(byte [] b, int width, int height) {
try {
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new ByteArrayInputStream(b), null, o);
//The new size we want to scale to
final int REQUIRED_SIZE_WIDTH=(int)(width*0.7);
final int REQUIRED_SIZE_HEIGHT=(int)(height*0.7);
//Find the correct scale value. It should be the power of 2.
int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;
while(true){
if(width_tmp/2<REQUIRED_SIZE_WIDTH || height_tmp/2<REQUIRED_SIZE_HEIGHT)
break;
width_tmp/=2;
height_tmp/=2;
scale*=2;
}
//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(new ByteArrayInputStream(b), null, o2);
} catch (OutOfMemoryError e) {
//handle this
}
return null;
}
答案 1 :(得分:0)