[英]Bitmap compress grainy image after saving
我正在通过Glide转换对图像进行转换,将其设置为背景为黑色背景的视图,然后将设备保存为PNG。 该视图如下所示:
压缩并将其保存为PNG后,它看起来像这样:
忽略尺寸和背景差异,注意边缘周围的颗粒感。 这在保存后持续存在。 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);
我试过的事情:
似乎PNG的压缩无法处理明亮的颜色。
更新:
使用RGB_565而不是ARGB_8888可以消除颗粒感,但会向外部添加黑色,因为没有alpha层。 如果有任何alpha像素,压缩似乎无法处理它。
尝试使用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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.