【发布时间】:2012-04-15 01:07:34
【问题描述】:
我正在编写一个应用程序,它使用手机的相机拍照,然后在我的应用程序中使用它。问题是,应用程序内存不足,这可能是因为位图的高分辨率。有没有办法让位图保持相同的大小,但降低分辨率?
谢谢!
【问题讨论】:
标签: android bitmap resolution
我正在编写一个应用程序,它使用手机的相机拍照,然后在我的应用程序中使用它。问题是,应用程序内存不足,这可能是因为位图的高分辨率。有没有办法让位图保持相同的大小,但降低分辨率?
谢谢!
【问题讨论】:
标签: android bitmap resolution
来自 jeet.chanchawat 的回答:https://stackoverflow.com/a/10703256/3027225
public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
// "RECREATE" THE NEW BITMAP
Bitmap resizedBitmap = Bitmap.createBitmap(
bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
【讨论】:
这可以在创建位图时使用 Options.inSampleSize 来完成
【讨论】:
你可以设置它的宽度和高度
Bitmap bm = ShrinkBitmap(imagefile, 150, 150);
调用函数
Bitmap ShrinkBitmap(String file, int width, int height){
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height);
int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width);
if (heightRatio > 1 || widthRatio > 1)
{
if (heightRatio > widthRatio)
{
bmpFactoryOptions.inSampleSize = heightRatio;
} else {
bmpFactoryOptions.inSampleSize = widthRatio;
}
}
bmpFactoryOptions.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
return bitmap;
}
}
【讨论】: