0

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));
Jerome
  • 1,153
  • 1
  • 17
  • 28
Amit
  • 1,841
  • 1
  • 19
  • 36
  • Duplicate of https://stackoverflow.com/questions/16906144/transparent-background-in-jpeg-image – Jerome Mar 29 '18 at 12:10
  • i want white background instead of black – Amit Mar 29 '18 at 12:24
  • Then it's a duplicate of https://stackoverflow.com/questions/4572564/how-to-change-the-background-color-of-a-saved-transparent-bitmap?rq=1. Draw your png on a white bmp and save it as a jpg – Jerome Mar 29 '18 at 12:30

2 Answers2

3

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);
Amit
  • 1,841
  • 1
  • 19
  • 36
0

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/

Jerome
  • 1,153
  • 1
  • 17
  • 28