[英]What is the best way to read pixels of a JavaFX Canvas?
我想獲得Canvas
特定坐標的顏色。 我已經嘗試使用此代碼獲取快照:
WritableImage snap = gc.getCanvas().snapshot(null, null);
snap.getPixelReader().getArgb(x, y); //This just gets the color without assigning it.
但是我的申請需要花費太多時間。 我想知道是否有任何其他方法來訪問我知道坐標的像素的顏色。
Canvas
通過調用GraphicsContext
的方法緩沖規定的繪圖指令 。 沒有像素讀取,直到Canvas
在后面呈現的脈沖 ,和指令緩沖器的內部格式沒有在API中露出。
作為替代方案,可以考慮拉絲成BufferedImage
,示出在這里 ,它允許直接在圖像的像素並通過其訪問WritableRaster
。 將以下行添加到此完整示例中將以ARGB順序輸出不透明紅色的預期值: ffff0000
。
System.out.println(Integer.toHexString(bi.getRGB(50, 550)));
public class Pixel
{
private static final SnapshotParameters SP = new SnapshotParameters();
private static final WritableImage WI = new WritableImage(1, 1);
private static final PixelReader PR = WI.getPixelReader();
private Pixel()
{
}
public static int getArgb(Node n, double x, double y)
{
synchronized (WI)
{
Rectangle2D r = new Rectangle2D(x, y, 1, 1);
SP.setViewport(r);
n.snapshot(SP, WI);
return PR.getArgb(0, 0);
}
}
public static Color getColor(Node n, double x, double y)
{
synchronized (WI)
{
Rectangle2D r = new Rectangle2D(x, y, 1, 1);
SP.setViewport(r);
n.snapshot(SP, WI);
return PR.getColor(0, 0);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.