获取事件的节点坐标并从 BufferedImage Javafx 读取 RGB
Getting the Node Coordinates of an Event and Reading RGB from BufferedImage Javafx
您好,我正在尝试获取显示图像 5 个视图的滚动窗格中点击坐标的 rgb。 ImageView
适合屏幕尺寸,而原始图像约为 8k x 4k 像素。下面是我试图找到 rgb 的尝试,但是我总是得到一个无法读取输入文件的异常,尽管我以前使用过这个图像。也可能需要注意我有一个缩放机制,其缩放总数由 SCALE_TOTAL
表示
点击代码
mapScroll.addEventFilter(MouseEvent.MOUSE_CLICKED, e -> {
File mapFile = new File("WorldProvincialMap-v1.01.png");
commandOutput.setText(commandOutput.getText() + " \n" + e.getSceneX() + " " + e.getSceneY());
try {
BufferedImage mapBuffered = ImageIO.read(mapFile);
int rgb = 0;
if(mapScroll.getHvalue() < 0.2)
{
rgb = mapBuffered.getRGB((int) ( e.getSceneX() / SCALE_TOTAL), (int)(e.getSceneY() / SCALE_TOTAL));
}
else if(mapScroll.getHvalue() >= 0.2 && mapScroll.getHvalue() < 0.4)
{
rgb = mapBuffered.getRGB((int) (e.getSceneX() / (2/SCALE_TOTAL)), (int) (e.getSceneY() / (2/SCALE_TOTAL)));
}
else if(mapScroll.getHvalue() >= 0.4 && mapScroll.getHvalue() < 0.6)
{
rgb = mapBuffered.getRGB((int) (e.getSceneX() / (3/SCALE_TOTAL)), (int) (e.getSceneY() / (3/SCALE_TOTAL)));
}
else if(mapScroll.getHvalue() >= 0.6 && mapScroll.getHvalue() < 0.8)
{
rgb = mapBuffered.getRGB((int) (e.getSceneX() / (4/SCALE_TOTAL)), (int) (e.getSceneY() / (4/SCALE_TOTAL)));
}
else if(mapScroll.getHvalue() >= 0.8 && mapScroll.getHvalue() < 1);
{
rgb = mapBuffered.getRGB((int) (e.getSceneX() / (5/SCALE_TOTAL)), (int) (e.getSceneY() / (5/SCALE_TOTAL)));
}
commandOutput.setText(commandOutput.getText() + " \n" + Integer.toString(rgb));
} catch (IOException e1) {
e1.printStackTrace();
commandOutput.setText(commandOutput.getText() + " \n" + e1.getMessage());
}
});
堆栈跟踪
javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(Unknown Source)
at gameaspects.SourceCodeVersion9.lambda(SourceCodeVersion9.java:208)
at com.sun.javafx.event.CompositeEventHandler$NormalEventFilterRecord.handleCapturingEvent(CompositeEventHandler.java:282)
at com.sun.javafx.event.CompositeEventHandler.dispatchCapturingEvent(CompositeEventHandler.java:98)
at com.sun.javafx.event.EventHandlerManager.dispatchCapturingEvent(EventHandlerManager.java:223)
at com.sun.javafx.event.EventHandlerManager.dispatchCapturingEvent(EventHandlerManager.java:180)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchCapturingEvent(CompositeEventDispatcher.java:43)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:52)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470)
at javafx.scene.Scene$ClickGenerator.access00(Scene.java:3398)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
at javafx.scene.Scene$MouseHandler.access00(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:380)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent4(GlassViewEventHandler.java:416)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null8(WinApplication.java:191)
at java.lang.Thread.run(Unknown Source)
滚动窗格
Image mapImage = new Image("WorldProvincialMap-v1.01.png");
//Sets the map to full screen
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
int width = gd.getDisplayMode().getWidth();
int height = gd.getDisplayMode().getHeight();
//Creating the gridPane
GridPane mapGrid = new GridPane();
//Creating the image views
ImageView mapView1 = new ImageView(mapImage);
ImageView mapView2 = new ImageView(mapImage);
ImageView mapView3 = new ImageView(mapImage);
ImageView mapView4 = new ImageView(mapImage);
ImageView mapView5 = new ImageView(mapImage);
//Fitting the image views to the screen size.
mapView1.setFitHeight(height);
mapView1.setFitWidth(width);
mapView2.setFitHeight(height);
mapView2.setFitWidth(width);
mapView3.setFitHeight(height);
mapView3.setFitWidth(width);
mapView4.setFitHeight(height);
mapView4.setFitWidth(width);
mapView5.setFitHeight(height);
mapView5.setFitWidth(width);
//Adding the imageViews to the girdPane
mapGrid.add(mapView1,0,0);
mapGrid.add(mapView2, 1, 0);
mapGrid.add(mapView3, 2, 0);
mapGrid.add(mapView4, 3, 0);
mapGrid.add(mapView5, 4, 0);
mapGrid.setManaged(false);
mapScroll.setContent(new Group(mapGrid));
mapScroll.setPannable(true);
//Removes the ScrollBars
mapScroll.setVbarPolicy(ScrollBarPolicy.NEVER);
mapScroll.setHbarPolicy(ScrollBarPolicy.NEVER);
不要将数据加载到 BufferedImage
,您应该使用 PixelReader
作为 javafx.scene.Image
来获取数据:
Image image = ...
int rgb = image.getPixelReader().getArgb(x, y);
// remove alpha; is this necessary???
rgb = rgb & 0xffffff;
这样可以避免多次将图像加载到内存中。
您好,我正在尝试获取显示图像 5 个视图的滚动窗格中点击坐标的 rgb。 ImageView
适合屏幕尺寸,而原始图像约为 8k x 4k 像素。下面是我试图找到 rgb 的尝试,但是我总是得到一个无法读取输入文件的异常,尽管我以前使用过这个图像。也可能需要注意我有一个缩放机制,其缩放总数由 SCALE_TOTAL
点击代码
mapScroll.addEventFilter(MouseEvent.MOUSE_CLICKED, e -> {
File mapFile = new File("WorldProvincialMap-v1.01.png");
commandOutput.setText(commandOutput.getText() + " \n" + e.getSceneX() + " " + e.getSceneY());
try {
BufferedImage mapBuffered = ImageIO.read(mapFile);
int rgb = 0;
if(mapScroll.getHvalue() < 0.2)
{
rgb = mapBuffered.getRGB((int) ( e.getSceneX() / SCALE_TOTAL), (int)(e.getSceneY() / SCALE_TOTAL));
}
else if(mapScroll.getHvalue() >= 0.2 && mapScroll.getHvalue() < 0.4)
{
rgb = mapBuffered.getRGB((int) (e.getSceneX() / (2/SCALE_TOTAL)), (int) (e.getSceneY() / (2/SCALE_TOTAL)));
}
else if(mapScroll.getHvalue() >= 0.4 && mapScroll.getHvalue() < 0.6)
{
rgb = mapBuffered.getRGB((int) (e.getSceneX() / (3/SCALE_TOTAL)), (int) (e.getSceneY() / (3/SCALE_TOTAL)));
}
else if(mapScroll.getHvalue() >= 0.6 && mapScroll.getHvalue() < 0.8)
{
rgb = mapBuffered.getRGB((int) (e.getSceneX() / (4/SCALE_TOTAL)), (int) (e.getSceneY() / (4/SCALE_TOTAL)));
}
else if(mapScroll.getHvalue() >= 0.8 && mapScroll.getHvalue() < 1);
{
rgb = mapBuffered.getRGB((int) (e.getSceneX() / (5/SCALE_TOTAL)), (int) (e.getSceneY() / (5/SCALE_TOTAL)));
}
commandOutput.setText(commandOutput.getText() + " \n" + Integer.toString(rgb));
} catch (IOException e1) {
e1.printStackTrace();
commandOutput.setText(commandOutput.getText() + " \n" + e1.getMessage());
}
});
堆栈跟踪
javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(Unknown Source)
at gameaspects.SourceCodeVersion9.lambda(SourceCodeVersion9.java:208)
at com.sun.javafx.event.CompositeEventHandler$NormalEventFilterRecord.handleCapturingEvent(CompositeEventHandler.java:282)
at com.sun.javafx.event.CompositeEventHandler.dispatchCapturingEvent(CompositeEventHandler.java:98)
at com.sun.javafx.event.EventHandlerManager.dispatchCapturingEvent(EventHandlerManager.java:223)
at com.sun.javafx.event.EventHandlerManager.dispatchCapturingEvent(EventHandlerManager.java:180)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchCapturingEvent(CompositeEventDispatcher.java:43)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:52)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470)
at javafx.scene.Scene$ClickGenerator.access00(Scene.java:3398)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
at javafx.scene.Scene$MouseHandler.access00(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:380)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent4(GlassViewEventHandler.java:416)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null8(WinApplication.java:191)
at java.lang.Thread.run(Unknown Source)
滚动窗格
Image mapImage = new Image("WorldProvincialMap-v1.01.png");
//Sets the map to full screen
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
int width = gd.getDisplayMode().getWidth();
int height = gd.getDisplayMode().getHeight();
//Creating the gridPane
GridPane mapGrid = new GridPane();
//Creating the image views
ImageView mapView1 = new ImageView(mapImage);
ImageView mapView2 = new ImageView(mapImage);
ImageView mapView3 = new ImageView(mapImage);
ImageView mapView4 = new ImageView(mapImage);
ImageView mapView5 = new ImageView(mapImage);
//Fitting the image views to the screen size.
mapView1.setFitHeight(height);
mapView1.setFitWidth(width);
mapView2.setFitHeight(height);
mapView2.setFitWidth(width);
mapView3.setFitHeight(height);
mapView3.setFitWidth(width);
mapView4.setFitHeight(height);
mapView4.setFitWidth(width);
mapView5.setFitHeight(height);
mapView5.setFitWidth(width);
//Adding the imageViews to the girdPane
mapGrid.add(mapView1,0,0);
mapGrid.add(mapView2, 1, 0);
mapGrid.add(mapView3, 2, 0);
mapGrid.add(mapView4, 3, 0);
mapGrid.add(mapView5, 4, 0);
mapGrid.setManaged(false);
mapScroll.setContent(new Group(mapGrid));
mapScroll.setPannable(true);
//Removes the ScrollBars
mapScroll.setVbarPolicy(ScrollBarPolicy.NEVER);
mapScroll.setHbarPolicy(ScrollBarPolicy.NEVER);
不要将数据加载到 BufferedImage
,您应该使用 PixelReader
作为 javafx.scene.Image
来获取数据:
Image image = ...
int rgb = image.getPixelReader().getArgb(x, y);
// remove alpha; is this necessary???
rgb = rgb & 0xffffff;
这样可以避免多次将图像加载到内存中。