无法从JavaFX中的ImageView获取图像 [英] Can't get Image from ImageView in JavaFX

查看:174
本文介绍了无法从JavaFX中的ImageView获取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在JavaFX中编写一个应用程序,只需点击一下按钮即可修改图片。所以我有一个ImageView,我必须从中提取图像,应用一些更改并将其设置回来。但是,当我显示我从ImageView获得的图像时,它看起来是空白的,只是黑屏。
我写了一些测试代码,代表了这种奇怪的行为。

I am writing an application in JavaFX which makes amendments to a picture at the click of a button. So I have an ImageView and I have to extract the image from it, apply some changes and set it back. However, when I show an image which I get from the ImageView, it appears to be blank, just black screen. I wrote some test code, which represents that strange behavior.

public class Corrector extends Application {
   @Override
    public void start(Stage stage) {
        try {
            javafx.scene.image.ImageView imageView = new ImageView();

            BufferedImage a = ImageIO.read(ClassLoader.getSystemResource("/image.jpg"));
            javafx.scene.image.Image image = SwingFXUtils.toFXImage(a, null);
            imageView.setImage(image);
            BufferedImage backImg = new BufferedImage(a.getWidth(), a.getHeight(), a.getType());
            SwingFXUtils.fromFXImage(imageView.getImage(), backImg);
            ShowImages.showWindow(backImg, "Image from imageVIew", true);

            stage.setTitle("ImageView");
            stage.setWidth(1000);
            stage.setHeight(500);
            Scene scene = new Scene(new Group());
            VBox root = new VBox();

            root.getChildren().addAll(imageView);

            scene.setRoot(root);

            stage.setScene(scene);
            stage.show();
        } catch (Exception e) {
            System.out.printf(e.getMessage());
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

当我运行应用程序时,ImageView屏幕包含图像,Image frm ImageView为空白。
我将不胜感激任何建议。

When I run the application, "ImageView" screen contains image, which "Image frm ImageView" is blank. I would appreciate any suggestions.

推荐答案

正如SwingFXUtils.fromFXImage的文档所说:

As the documentation of SwingFXUtils.fromFXImage says:


可以重复使用可选的BufferedImage参数来存储像素的副本
。如果提供的
对象为null,太小或者图像像素
无法轻易转换为的类型,则会创建一个新的BufferedImage。

The optional BufferedImage parameter may be reused to store the copy of the pixels. A new BufferedImage will be created if the supplied object is null, is too small or of a type which the image pixels cannot be easily converted into.

在你的情况下,与SwingFXUtils.fromFXImage的类型不匹配想要使用。因此,将创建并返回一个新的BufferedImage,其中包含复制的像素,而backImg保持不变。
您甚至不必提供BufferedImage。只需将第二个参数设置为null:

In your case the type mismatches with the one SwingFXUtils.fromFXImage wants to use. So a new BufferedImage will be created and returned, containing the copied pixels, while your backImg stays untouched. You dont even have to offer an BufferedImage. Just set the second argument to null:

BufferedImage backImg = SwingFXUtils.fromFXImage(imageView.getImage(), null);

更多信息:


  • 您已经将变量backImg定义了两次。删除第一个定义,因为它未被使用且无法使用。

  • 您甚至可以使用JavaFXImage-Constructor加载图像

Image fxFromFile = new Image(Corrector.class.getResourceAsStream("/image.png"));

这篇关于无法从JavaFX中的ImageView获取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆