Image saved in JavaFX as jpg is pink toned

前端 未结 2 1960
无人共我
无人共我 2021-02-09 00:15

I want to save an Image from my ImageView to files with different resolutions. Doing it as .png results as expected. As for .jpg - I get all files pink toned.

2条回答
  •  清酒与你
    2021-02-09 01:14

    I found a solution on Oracle forums. As widely discussed, the problem is in alpha-channel that needs to be excluded from the source image, targeted for .jpg save. I also rearranged my code to make it shorter. The workaround is:

    // Get buffered image:
    BufferedImage image = SwingFXUtils.fromFXImage(myJavaFXImage, null); 
    
    // Remove alpha-channel from buffered image:
    BufferedImage imageRGB = new BufferedImage(
        image.getWidth(), 
        image.getHeight(), 
        BufferedImage.OPAQUE); 
    
    Graphics2D graphics = imageRGB.createGraphics();
    
    graphics.drawImage(image, 0, 0, null);
    
    ImageIO.write(imageRGB, "jpg", new File("/mydir/foto.jpg"));
    
    graphics.dispose();
    

    Fixed in Java 8: https://bugs.openjdk.java.net/browse/JDK-8114609

提交回复
热议问题