3

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

好的,我已经在谷歌上搜索了一段时间,每个人似乎都使用如下内容:

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

在之前和之后,位图没有变小:

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+"");
4

1 回答 1

1

compress方法,如文档中所述:

将位图的压缩版本写入指定的输出流。位图可以通过将相应的输入流传递给BitmapFactory.decodeStream()

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

于 2013-11-05T04:36:06.043 回答