[英]compress transparent png image to jpg give black background java
compress transparent png image to jpg give black background I want white background instead of black 将透明png图像压缩为jpg给黑色背景,我想要白色背景而不是黑色
public static byte[] getBitMapBytes(Bitmap bmp, int quality) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, quality, stream);
byte[] byteArray = stream.toByteArray();
return byteArray;
}
fos = new FileOutputStream(f);
fos.write(getBitMapBytes(compressedImage, 60));
Got the answer : 得到了答案:
Bitmap newBitmap = Bitmap.createBitmap(image.getWidth(),
image.getHeight(), image.getConfig());
Canvas canvas = new Canvas(newBitmap);
canvas.drawColor(Color.WHITE);
Rect dest = new Rect(0, 0, image.width, image.height);
Rect src = new Rect(0, 0, image.width, image.height);
canvas.drawBitmap(bmp, src, dest, null);
This is a normal behavior. 这是正常现象。 Jpeg doesn't manage transparency. Jpeg不管理透明度。
You can use this kind of lib if you just want to reduce the size of the image: http://objectplanet.com/pngencoder/ or online https://tinypng.com/ 如果您只是想减小图像的大小,则可以使用这种lib: http : //objectplanet.com/pngencoder/或在线https://tinypng.com/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.