What is the best way to read pixels of a JavaFX Canvas?

后端 未结 2 1736
后悔当初
后悔当初 2020-12-03 20:11

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

WritableImage snap = gc.ge         


        
2条回答
  •  暖寄归人
    2020-12-03 20:25

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

提交回复
热议问题