Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

io

image

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 :)

like image 507
Rafcik Avatar asked Aug 10 '15 07:08

Rafcik


People also ask

How do I convert a PNG to a JPEG without losing it?

Paint is a built-in Windows tool that you can use it to convert a PNG image to JPEG without losing quality. , open the PNG image with Paint. Open the PNG image with Paint and navigate to File > Save as > JPEG picture. Then, choose a location, add a name, and make sure the file format is set to JPEG.

How do I convert multiple PNG to JPG?

Convert in Microsoft PaintGo to File > Save as and open the Save as type drop-down menu. You can then select JPEG and PNG, as well as TIFF, GIF, HEIC, and multiple bitmap formats. Save the file to your computer and it will convert.

Can you convert jpegs to Pngs?

In seconds, you can convert your JPG images to PNG format. PNG files offer capabilities such as transparency, faded edges, and higher quality compression. Whether you need to convert small or large JPG images, our converter tool upholds the quality and provides results instantly.


3 Answers

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/

like image 74
uoyilmaz Avatar answered Nov 16 '22 22:11

uoyilmaz


Try this:

BufferedImage bufferedImageUp = (BufferedImage)up;    
BufferedImage old = new BufferedImage(bufferedImageUp.getWidth(), bufferedImageUp.getHeight(), bufferedImageUp.TYPE_INT_RGB);
ImageIO.write(old, "jpg", temp);
like image 36
Zaid Malhis Avatar answered Nov 16 '22 22:11

Zaid Malhis


The keypart is to write the BufferedImage onto a new BufferedImage using an RGB channel with White background. That'll fix the issue of weird colors:

public static InputStream encodeToJpg(String filepath) throws IOException {
    System.out.println("Encoding to JPG...");
    BufferedImage buffImg;
    InputStream origStream = new FileInputStream(new File(filepath));
    buffImg = ImageIO.read(origStream);
    origStream.close();

    // Recreate the BufferedImage to fix channel issues
    BufferedImage newBuffImg = new BufferedImage(buffImg.getWidth(), buffImg.getHeight(), BufferedImage.TYPE_INT_RGB);
    newBuffImg.createGraphics().drawImage(buffImg, 0, 0, Color.WHITE, null);
    buffImg.flush();

    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    ImageIO.write(newBuffImg, "jpg", outStream);

    ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray());
    return inStream;
}
like image 1
Cardin Avatar answered Nov 16 '22 22:11

Cardin