title: Convert PNG to JPEG, Using Compression Quality, Why Inverted colors?
body: <p>I have low information about the image files.</p>
  
  <p>To be able to convert PNG to JPEG in Java, at first I have used below code part to convert PNG to JPEG: inverted</p>
  
  <p><strong>Code Part 1)</strong></p>
  
  <pre><code>BufferedImage newBufferedImage = new BufferedImage(bufferedImageFile.getWidth(), bufferedImageFile.getHeight(),
          BufferedImage.TYPE_INT_RGB);
  newBufferedImage.createGraphics().drawImage(bufferedImageFile, 0, 0, Color.WHITE, null);
  File retTempFile = MakeupFileUtil.createTempFile(fileName);
  ImageIO.write(newBufferedImage, "jpg", retTempFile);
  return retTempFile;
  </code></pre>
  
  <p>This accomplishes the task very well but the output quality is lower than the original PNG image quality(let's say PNG quality is 100 but JPEG quality is 75)</p>
  
  <p>Then, <a href="https://stackoverflow.com/a/17108301/1201725">from this answer</a>, I tried below code part which uses <strong>"Compression Quality"</strong>:</p>
  
  <p><strong>Code Part 2)</strong></p>
  
  <pre><code>JPEGImageWriteParam jpegParams = new JPEGImageWriteParam(null);
  jpegParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
  jpegParams.setCompressionQuality(1f);
  final ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();
  writer.setOutput(new FileImageOutputStream(jpegFile));
  writer.write(null, new IIOImage(bufferedImageFile, null, null), jpegParams);
  </code></pre>
  
  <p>But this time, converted JPEG file got inverted colors.</p>
  
  <p>Source Image (PNG):
  <a href="https://i.stack.imgur.com/D4Yep.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/D4Yep.png" alt="source png image"></a></p>
  
  <p>Image After Code Part 2 (JPEG):
  <a href="https://i.stack.imgur.com/rliTU.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rliTU.jpg" alt="part 2 code result jpeg"></a></p>
  
  <p>Why is this happening?</p>