如何在JAVAFX中加载计算机目录图像 [英] How can I load Computer Directory images in JAVAFX

查看:143
本文介绍了如何在JAVAFX中加载计算机目录图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将计算机文件夹图像加载到缩略图墙中。我在网上阅读了另一个论坛 ImageView url实例变量不支持系统路径。我尝试使用该解决方案,但它抛出异常: java.lang.OutOfMemoryError:Java堆空间,因为它一直在读取文件

I am trying to load my computer folder images into a wall of thumbnails. I read on a thread from another forum that ImageView "url" instance variable does not support system paths. I tried with the solution there, but it throws an exception: java.lang.OutOfMemoryError: Java heap space as it keeps reading the file.

另一个问题是它一直给我警告使用包javafx.ext - > SwingUtils.toFXImage 方法。

another problem is it keeps giving me warning of using package javafx.ext -> SwingUtils.toFXImage method.

我还尝试输入这样的URL:

I have also tried to input the URL like that:

"file://localhost//Users/USER/Pictures/Camera/test/1.JPG"

我试图显示一些图像,但它总是只显示3到4张图像。

I tried to display a number of images, but it always only displays 3 to 4 images.

我使用 ImageView 给出的错误函数进行检查,它并不表示我的图像读数出现错误。

I checked with the error function given from ImageView, it does not indicate that the reading of my images encountered an error.

还有其他选择吗?

function load() {
    println("RUNTIME {Runtime.getRuntime().maxMemory()}");
    System.gc();
    Runtime.getRuntime().freeMemory();

    //MAC Folder PATH
    var path: String = "/Users/username/Pictures/camera/test/1.JPG";;
    var file: File = new File(path);

    //http://download.oracle.com/docs/cd/E17802_01/javafx/javafx/1.3/docs/api/javafx.ext.swing/javafx.ext.swing.SwingUtils.html

    //public toFXImage(image: java.awt.image.BufferedImage) : Image
    //Creates a JavaFX Image from a BufferedImage.
    img = SwingUtils.toFXImage(ImageIO.read(file));
}


推荐答案

目前尚不清楚究竟是什么你想要做的。如果您正在谈论JavaFX 2.0,以下代码可以正常工作。如果要加载大量图像并需要节省内存,则只需为要一次显示的数字创建足够的ImageView。然后,当您浏览图片时,可以换出 ImageView 中包含的图片对象。

It is not clear exactly what you are trying to do. If you are talking about JavaFX 2.0, the following code works. If you are loading a lot of images and need to conserve memory, you only have to create enough ImageView's for the number you want to display at one time. Then as you page through the images, you can swap out the Image object contained in the ImageView.

public void start(Stage primaryStage) {
    primaryStage.setTitle("Hello World");
    StackPane root = new StackPane();
    Scene scene = new Scene(root, 300, 250);

    File file = new File("/System/Library/CoreServices/loginwindow.app/Contents/Resources/LogOut.png");
    Image image = new Image(file.toURI().toString());
    ImageView iv = new ImageView(image);

    root.getChildren().add(iv);
    primaryStage.setScene(scene);
    primaryStage.show();
}

这篇关于如何在JAVAFX中加载计算机目录图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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