Converting from Java to PNG to JPG changes white to red

When converting an 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 change to red.

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

Here's the effect:

before after

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

+5
source share
2 answers

The problem is the alpha channel in the PNG file, which is not in the JPG file. Therefore, the alpha channel replaces one of the red / green / blue channels at the output, and the colors are erroneous. Here you can find an example of how to do this: http://www.mkyong.com/java/convert-png-to-jpeg-image-file-in-java/

+5
source

Try the following:

 BufferedImage bufferedImageUp = (BufferedImage)up; BufferedImage old = new BufferedImage(bufferedImageUp.getWidth(), bufferedImageUp.getHeight(), bufferedImageUp.TYPE_INT_RGB); ImageIO.write(old, "jpg", temp); 
+1
source

All Articles