簡體   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