gpt4 book ai didi

Android:从 Uri 压缩位图

转载 作者:太空狗 更新时间:2023-10-29 15:08:08 24 4
gpt4 key购买 nike

使用普通智能手机相机拍照。

好吧,我已经用谷歌搜索了一段时间了,似乎每个人都在使用类似下面的东西:

Bitmap bm = BitmapFactory.decodeStream(getContentResolver().openInputStream(fileUri));
ByteArrayOutputStream out = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 25, out);
Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));

我用它来检查文件大小:

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
protected int sizeOf(Bitmap data) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1) {
return data.getRowBytes() * data.getHeight();
} else {
return data.getByteCount();
}
}

Bitmap 没有变小,之前和之后:

Log.d("image", sizeOf(bm)+"");
Log.d("image", sizeOf(decoded)+"");

结果:

11-05 02:51:52.739: D/image(2558): 20155392
11-05 02:51:52.739: D/image(2558): 20155392

指针?

答案:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 50;
Bitmap bmpSample = BitmapFactory.decodeFile(fileUri.getPath(), options);

Log.d("image", sizeOf(bmpPic)+"");

ByteArrayOutputStream out = new ByteArrayOutputStream();
bmSample.compress(Bitmap.CompressFormat.JPEG, 1, out);
byte[] byteArray = out.toByteArray();

Log.d("image", byteArray.length/1024+"");

最佳答案

文档中提到的 compress 方法:

Write a compressed version of the bitmap to the specified outputstream. The bitmap can be reconstructed by passing a corresponding inputstream to BitmapFactory.decodeStream()

因此变量 out 现在包含压缩位图。由于您在调用 decodeStream 后检查了大小,位图被解压缩 并返回给您。因此大小相同。

关于Android:从 Uri 压缩位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19780812/

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