读取JavaFX Canvas像素的最佳方法是什么?

读取JavaFX Canvas像素的最佳方法是什么?

问题描述:

我想在 Canvas 中获取特定坐标的颜色。我已经尝试使用此代码获取快照:

I want to get the color for specific coordinates inside a Canvas. I already tried getting a snapshot using this code:

WritableImage snap = gc.getCanvas().snapshot(null, null);
snap.getPixelReader().getArgb(x, y); //This just gets the color without assigning it.

但是我的申请需要花费太多时间。我想知道是否有其他方法来访问我知道坐标的像素的颜色。

But it just takes too much time for my application. I was wondering if there is any other way to access the color of a pixel for which I know the coordinates.

A Canvas 通过调用指令 tutorial / jfx-architecture.htm#JFXST108rel =nofollow noreferrer> GraphicsContext 。在以后 Canvas 之前, 没有要读取的像素/8/javafx/get-started-tutorial/jfx-architecture.htm#JFXST108\"rel =nofollow noreferrer> pulse ,指令缓冲区的内部格式未在API中公开。

A Canvas buffers the drawing instructions prescribed by invoking the methods of a GraphicsContext. There are no pixels to read until the Canvas is rendered in a later pulse, and the internal format of the instruction buffer is not exposed in the API.

作为替代方案,请考虑绘制成 BufferedImage 这里,允许直接访问图像的像素,并通过其 WritableRaster 。将以下行添加到此完整的示例中,以ARGB顺序输出不透明红色的预期值: ffff0000

As an alternative, consider drawing into a BufferedImage, illustrated here, which allows access to the image's pixels directly and via its WritableRaster. Adding the following line to this complete example outputs the expected value for opaque red in ARGB order: ffff0000.

System.out.println(Integer.toHexString(bi.getRGB(50, 550)));