位图的尺寸意图通过返回的相机? [英] Size of bitmap returned by camera via intent?
问题描述
我如何得到一个位图从相机一定(内存型)大小?
How do I get a bitmap with a certain (memory friendly) size from the camera?
我开始用相机意图:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra("return-data", true);
photoUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "mytmpimg.jpg"));
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(cameraIntent, REQUEST_CODE_CAMERA);
我在这里处理结果是:
I handle the result here:
// Bitmap photo = (Bitmap) intent.getExtras().get("data");
Bitmap photo = getBitmap(photoUri);
现在,如果我使用注释行 - 直接将位图,我总是得到一个160×120位,这是太小了。如果我使用一些标准的东西,我发现(方法getBitmap)从URI加载它,它加载一个2560 X 1920的位图(!),并消耗将近20 MB的内存。
Now if I use the commented line - get the bitmap directly, I get always a 160 x 120 bitmap, and that's too small. If I load it from the URI using some standard stuff I found (method getBitmap), it loads a 2560 x 1920 bitmap (!) and that consumes almost 20 mb memory.
如何加载比方说480 * 800(同样大小的相机preVIEW显示我)?
How do I load let's say 480 x 800 (the same size the camera preview shows me)?
而无需将2560 X 1920加载到内存中,并缩小。
Without having to load the 2560 x 1920 into memory and scaling down.
推荐答案
下面是我想出的基础上,从中旧Android版本删除了作物库调用getBitmap()方法。我做了一些修改:
Here is what I came up with, based on a method called getBitmap() from a crop library which was removed from old Android version. I did some modifications:
private Bitmap getBitmap(Uri uri, int width, int height) {
InputStream in = null;
try {
int IMAGE_MAX_SIZE = Math.max(width, height);
in = getContentResolver().openInputStream(uri);
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in, null, o);
in.close();
int scale = 1;
if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
scale = (int)Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
}
//adjust sample size such that the image is bigger than the result
scale -= 1;
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
in = getContentResolver().openInputStream(uri);
Bitmap b = BitmapFactory.decodeStream(in, null, o2);
in.close();
//scale bitmap to desired size
Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, width, height, false);
//free memory
b.recycle();
return scaledBitmap;
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
return null;
}
这样做是加载使用BitmapFactory.Options()位图+一些样本量 - 这样原始图像不会加载到内存中。问题是,样本大小只是工作在步骤。而为了得到这将产生最小样本量中减去1 - 我使用一些数学我复制获得最小样本规模为我的形象。比位图的大小,我需要更大的。
What this does is load the bitmap using BitmapFactory.Options() + some sample size - this way the original image is not loaded into memory. The problem is that the sample size just works in steps. I get the "min" sample size for my image using some maths I copied - and subtract 1 in order to get the sample size which will produce the min. bitmap bigger than the size I need.
和则为了准确地与要求的尺寸得到位图做 Bitmap.createScaledBitmap(B,宽度,高度,FALSE)正常比例;
。之后立刻就回收了更大的位图。这是很重要的,因为,例如,在我的情况下,为了获得480 x 800的位图,更大的位图是1280 * 960,并占用4.6MB内存。
And then in order to get the bitmap with exactly the size requested do normal scaling with Bitmap.createScaledBitmap(b, width, height, false);
. And immediatly after it recycle the bigger bitmap. This is important, because, for example, in my case, in order to get 480 x 800 bitmap, the bigger bitmap was 1280 x 960 and that occupies 4.6mb memory.
一个更多的内存友好的方式将不能调整比例
,所以较小的位图将被调整以匹配所需的大小。但是,这将降低图像的质量。
A more memory friendly way would be to not adjust scale
, so a smaller bitmap will be scaled up to match the required size. But this will reduce the quality of the image.
这篇关于位图的尺寸意图通过返回的相机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!