我需要有关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会导致质量损失。我们能做什么来解决这个问题?

之前:
android - Android位图解码和缩放质量损失-LMLPHP
后:
android - Android位图解码和缩放质量损失-LMLPHP

如您所见,图像变得更加清晰

最佳答案

尝试将其设置为100:

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

关于android - Android位图解码和缩放质量损失,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31668015/

10-10 07:03