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

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

问题描述

我正在尝试将我的计算机文件夹图像加载到缩略图墙中.我在另一个线程上在另一个线程上阅读了ImageView url"的论坛实例变量不支持系统路径.我尝试了那里的解决方案,但它抛出了一个异常:java.lang.OutOfMemoryError: Java heap space as it keep reading the file.

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.

我也试过这样输入网址:

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 中包含的 Image 对象.

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天全站免登陆