简体   繁体   中英

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

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.

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM