我想将 ImageView 中的图像保存到具有不同分辨率的文件中。将其作为 .png 结果按预期进行。至于 .jpg - 我把所有文件都调成粉红色。
诀窍在哪里?这是代码:
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
}
}