我需要一些关于Java (11,Javafx)中的Writable和BufferedImages的帮助:
如何将WritabeImage转换为BufferedImage?-不使用SwingFXUtils.fromFXImage()
找到一个没有SwingFXUtils.fromFXImage()的解决方案对我来说很重要。我已经问过谷歌了,但我得到的唯一结果是SwingFXUtils.fromFXImage()。所以我想请教你的专业知识。
你能帮帮我吗?
谢谢!
发布于 2020-06-02 23:27:27
谢谢你的帮忙!
这就是我的代码的样子(包括我需要避免的'SwingFXUtils‘):
static public void createImg(Node NODE, String name) {
WritableImage SNAPSHOT = NODE.snapshot(new SnapshotParameters(), null);
File file = new File(Properties.getSettingsPath() + File.separator + name + ".png");
try {
ImageIO.write(SwingFXUtils.fromFXImage(SNAPSHOT, null), "png", file);
} catch (IOException e) {
e.printStackTrace();
}
}发布于 2020-06-02 23:50:45
您可以自己复制像素:
BufferedImage convert(Image fxImage) {
int width = (int) Math.ceil(fxImage.getWidth());
int height = (int) Math.ceil(fxImage.getHeight());
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_ARGB);
int[] buffer = new int[width];
PixelReader reader = fxImage.getPixelReader();
WritablePixelFormat<IntBuffer> format =
PixelFormat.getIntArgbInstance();
for (int y = 0; y < height; y++) {
reader.getPixels(0, y, width, 1, format, buffer, 0, width);
image.getRaster().setDataElements(0, y, width, 1, buffer);
}
return image;
}https://stackoverflow.com/questions/62153843
复制相似问题