Search code examples
javaimageio

Conversion in Java from PNG to JPG changes white color into red


While converting image using

UploadedFile uf; //as a paremeter in function; PrimeFaces Object;
BufferedImage old = ImageIO.read(uf.getInputstream());
ByteArrayOutputStream temp = new ByteArrayOutputStream();
ImageIO.write(old, "jpg", temp);

white colors are changed into red..

http://www.primefaces.org/showcase/ui/file/upload/basic.xhtml

Here's the effect:

beforeafter

Do you know how to handle this problem? Thanks for your help in advance :)


Solution

  • The problem is the alpha channel in the PNG file, which doesn't exist in the JPG file. Therefore, the alpha channel is replacing one of the red/green/blue channels in the output, and the colors are wrong. You can find an example of how to do it properly here: http://www.mkyong.com/java/convert-png-to-jpeg-image-file-in-java/