保存在JavaFX中作为jpg的图像是粉红色的 [英] Image saved in JavaFX as jpg is pink toned

查看:260
本文介绍了保存在JavaFX中作为jpg的图像是粉红色的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将ImageView中的图像保存到具有不同分辨率的文件中。像.png那样按预期结果。至于.jpg - 我得到所有文件粉红色调。

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.

诀窍在哪里?下面是代码:

Where is the trick? Here is the code:

Object[] imagesFromFotoListView = ta.myFotoListView.getItems().toArray();
new File(localDir).mkdirs();
for (int i = 0; i < imagesFromFotoListView.length; i++) {
    new File(localDir + "/" + i).mkdirs(); 
    final ImageView iv = new ImageView((Image) imagesFromFotoListView[i]);
    ImageIO.write(SwingFXUtils.fromFXImage(iv.snapshot(null, null), null), "jpg", new File(localModelFotoDir + "/" + i + "/large.jpg")); // JPG THAT FAILS PINK.
    BufferedImage bi = SwingFXUtils.fromFXImage(iv.snapshot(null, null), null);
    int resolution[] = new int[]{500, 250, 75};
    for (int j = 0; j < resolution.length; j++) {
        BufferedImage resizedBImage;
        if (bi.getWidth() == bi.getHeight()) {
            resizedBImage = resizeBufferedImage(bi, Scalr.Method.ULTRA_QUALITY, Scalr.Mode.AUTOMATIC, resolution[j], resolution[j]);
        } else if (bi.getWidth() > bi.getHeight()) {
            resizedBImage = resizeBufferedImage(bi, Scalr.Method.ULTRA_QUALITY, Scalr.Mode.AUTOMATIC, resolution[j], (int) ((double) resolution[j] * bi.getHeight() / bi.getWidth()));
        } else {
            resizedBImage = resizeBufferedImage(bi, Scalr.Method.ULTRA_QUALITY, Scalr.Mode.AUTOMATIC, (int) ((double) resolution[j] * bi.getWidth() / bi.getHeight()), resolution[j]);
        }
        Image resizedI = (Image) SwingFXUtils.toFXImage(resizedBImage, null);
        ImageIO.write(SwingFXUtils.fromFXImage((Image) SwingFXUtils.toFXImage(resizedBImage, null), null), "png", new File(localModelFotoDir + "/" + i + "/" + resolution[j] + ".png")); // PNG THAT GOES WELL
    }
}


推荐答案

我在Oracle论坛上找到了一个解决方案。正如广泛讨论的那样,问题在alpha-channel 中需要从源图像中排除,目标是 .jpg save。我还重新安排了我的代码,使其更短。解决方法是:

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();

在Java 8中修复: https://bugs.openjdk.java.net/browse/JDK-8114609

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

这篇关于保存在JavaFX中作为jpg的图像是粉红色的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆