Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The problem (at least with png to jpg conversion) is that the color scheme isn't the same, because jpg doesn't support transparency. </p> <p>What we've done successfully is something along these lines (this is pulled from various bits of code - so please forgive the crudeness of the formatting):</p> <pre><code>File file = new File("indexed_test.gif"); BufferedImage image = ImageIO.read(file); int width = image.getWidth(); int height = image.getHeight(); BufferedImage jpgImage; //you can probably do this without the headless check if you just use the first block if (GraphicsEnvironment.isHeadless()) { if (image.getType() == BufferedImage.TYPE_CUSTOM) { //coerce it to TYPE_INT_ARGB and cross fingers -- PNGs give a TYPE_CUSTOM and that doesn't work with //trying to create a new BufferedImage jpgImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB); } else { jpgImage = new BufferedImage(width, height, image.getType()); } } else { jgpImage = GraphicsEnvironment.getLocalGraphicsEnvironment(). getDefaultScreenDevice().getDefaultConfiguration(). createCompatibleImage(width, height, image.getTransparency()); } //copy the original to the new image Graphics2D g2 = null; try { g2 = jpg.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g2.drawImage(image, 0, 0, width, height, null); } finally { if (g2 != null) { g2.dispose(); } } File f = new File("indexed_test.jpg"); ImageIO.write(jpgImage, "jpg", f); </code></pre> <p>This works for png to jpg and gif to jpg. And you will have a white background where the transparent bits were. You can change this by having g2 fill the image with another color before the drawImage call. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload