保存后,位图会压缩颗粒状图像

问题描述 投票:1回答:1

我正在通过Glide转换对图像进行转换,将其设置为背景为黑色背景的视图,然后将设备保存为PNG。该视图如下所示:

enter image description here

压缩并将其保存为PNG后,它看起来像这样:

enter image description here

忽略尺寸和背景差异,注意边缘周围的颗粒感。这在保存后持续存在。 PNG的大小与原始图像一样正确,因此不执行缩放。我该如何防止这种情况发生?压缩的代码是:

File file = new File(context.getFilesDir().getPath() + "/" + String.valueOf(num) + ".png");
OutputStream os = null;
try {
   os = new FileOutputStream(file);
} catch (FileNotFoundException e) {
   e.printStackTrace();
}
bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);

设置为位图的属性为:

Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
b.setHasAlpha(true);

我试过的事情:

  • 使用WEBP;同样的问题。
  • 保存视图并构建图形缓存,然后保存该位图。同样,同样的问题。
  • 由于我需要alpha图层,因此无法使用JPEG。

似乎PNG的压缩无法处理明亮的颜色。

更新:

使用RGB_565而不是ARGB_8888可以消除颗粒感,但会向外部添加黑色,因为没有alpha层。如果有任何alpha像素,压缩似乎无法处理它。

android bitmap android-glide
1个回答
0
投票

尝试使用BitmapFactory.decodeFile

String filePath = context.getFilesDir().getPath() + "/" + String.valueOf(num) + ".png";

BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.inDither = false;

BitmapFactory.decodeFile(filePath, options);

或者,你可以使用createScaledBitmap方法,它有一个标志,你可以设置是否应该过滤缩放的图像。该标志改善了位图的质量。

boolean shouldFilter = false;
bitmap.createScaledBitmap(bitmap, desiredWidth, desiredHeight, shouldFilter);
© www.soinside.com 2019 - 2024. All rights reserved.