inPreferredConfig中的BitmapFactory.Options对图像文件没有任何影响

时间:2015-02-10 16:05:11

标签: android android-bitmap

我说的是,如果我使用相机拍摄全分辨率照片,并使用bitmap.compress()将位图保存为jpg,那么inPreferredConfig参数对最终图像没有任何影响吗?

我已尝试指定两者,并且生成的图像是相同的8位每通道jpgs(根据Photoshop),无论我是否使用 ARGB_8888 RGB_565 模式创建位图。它们的文件大小大致相同。因此,在显示jpgs时保存堆内存的唯一方法是像文档状态一样对其进行子采样吗?

所以这是我正在使用的代码,你可以看到我没有对它进行二次采样(比例因子为1)

BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    /* Figure out which way needs to be reduced less */
    int scaleFactor = 1;
    if ((targetW > 0) || (targetH > 0)) {
        scaleFactor = Math.min(photoW/targetW, photoH/targetH); 
    }

    /* Set bitmap options to scale the image decode target */
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPreferredConfig = Bitmap.Config.RGB_565;
    bmOptions.inPurgeable = true;

    /* Decode the JPEG file into a Bitmap */
    Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);

    String path = Environment.getExternalStorageDirectory().toString();
    OutputStream fOut = null;
    File file = new File(path, "TEST_IMAGE.jpg"); // the File to save to
    try {
        fOut = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut); // saving the Bitmap to a file compressed as a JPEG with 85% compression rate
        fOut.flush();
        fOut.close(); // do not forget to close the stream

        MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

0 个答案:

没有答案