gpt4 book ai didi

android - 相机通过 Intent 返回的位图大小?

转载 作者:IT王子 更新时间:2023-10-28 23:31:28 31 4
gpt4 key购买 nike

如何从相机中获取具有特定(内存友好)大小的位图?

我正在启动相机 Intent :

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);

我在这里处理结果:

//  Bitmap photo = (Bitmap) intent.getExtras().get("data");

Bitmap photo = getBitmap(photoUri);

现在,如果我使用注释行 - 直接获取位图,我总是得到一个 160 x 120 的位图,这太小了。如果我使用我找到的一些标准内容(方法 getBitmap)从 URI 加载它,它会加载一个 2560 x 1920 位图(!),这会消耗近 20 mb 内存。

我如何加载 480 x 800(与相机预览显示的尺寸相同)?

无需将 2560 x 1920 加载到内存中并按比例缩小。

最佳答案

这是我根据从旧版 Android 版本中删除的裁剪库中名为 getBitmap() 的方法得出的。我做了一些修改:

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 以获得将产生最小值的样本大小。位图比我需要的大。

然后为了得到与请求大小完全相同的位图,请使用 Bitmap.createScaledBitmap(b, width, height, false); 进行正常缩放。并立即回收更大的位图。这很重要,因为例如,在我的例子中,为了获得 480 x 800 位图,较大的位图为 1280 x 960,占用 4.6mb 内存。

对内存更友好的方法是不调整 scale,因此将放大较小的位图以匹配所需的大小。但这会降低图像的质量。

关于android - 相机通过 Intent 返回的位图大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11709550/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com