繁体   English   中英

Android Bitmap解码和缩放质量损失

[英]Android Bitmap Decode and Scale quality loss

我需要一些BitmapFactory.decode和BitmapFactory.createScaledBitmap的帮助。

在我们的应用程序中,我们使用此代码调整图像大小:

 public static synchronized File resizeBitmap(File file, int expectedWidth) throws IOException {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inScaled = false;
    options.inDither = false;
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);

    float width = bitmap.getWidth() / (float) expectedWidth;
    int expectedHeight = (int) (bitmap.getHeight() / width);

    Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, expectedWidth, expectedHeight, true);
    String path = Environment.getExternalStorageDirectory().toString();
    File tempFile = new File(path, "temp.jpg");
    if (tempFile.exists())
        tempFile.delete();

    FileOutputStream fileOutputStream = new FileOutputStream(tempFile.getAbsolutePath());
    scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 80, fileOutputStream);
    fileOutputStream.flush();
    fileOutputStream.close();
    return tempFile;
}

但是我们在Bitmap上有质量损失。 我们能做些什么来解决这个问题?

之前: 之前 后: 后

如您所见,图像变得更加锐利

scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 80, fileOutputStream);

在这行代码中,这里用80代替80表示压缩。 因此,您的图像将被压缩到80%的质量。

虽然预计会有一些质量下降。

您也可以尝试将其保存为.png而不是jpg,并尝试它是否有帮助

尝试将其设置为100:

scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM