如何在 JAVAFX 中加载计算机目录图像

发布于 2024-12-11 02:26:49 字数 1179 浏览 0 评论 0原文

我正在尝试将计算机文件夹图像加载到缩略图墙上。我在另一个帖子上在一个线程上阅读了论坛上说 ImageView “url”实例变量不支持系统路径。我尝试了那里的解决方案,但它抛出异常:java.lang.OutOfMemoryError:Java堆空间,因为它不断读取文件

另一个问题是它不断向我发出使用包 javafx.ext -> 的警告。 SwingUtils.toFXImage 方法。

我也尝试过这样输入 URL:

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

我尝试显示许多图像,但它总是只显示 3 到 4 个图像。

我检查了 ImageView 给出的错误函数,它并不表明我的图像读取遇到错误。

还有其他选择吗?

代码

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

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.

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"

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

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

Are there any alternatives?

Code

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

幽蝶幻影 2024-12-18 02:26:49

目前尚不清楚您到底想做什么。如果您谈论的是 JavaFX 2.0,则以下代码有效。如果您要加载大量图像并且需要节省内存,则只需为一次要显示的数量创建足够的 ImageView 即可。然后,当您翻阅图像时,您可以交换 ImageView 中包含的 Image 对象。

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

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();
}
只是在用心讲痛 2024-12-18 02:26:49

其简单:使用浏览器打开图像并复制图片的整个 url 并将其粘贴为 Image 对象的参数。不要删除“file///:”,因为这会使图像可加载。你将从那里得到你的其他逻辑。快乐编码例如

Image image = new Image("file:///C:/Users/Nigel/Desktop/my_image.png");
ImageView imgview = new ImageView(image);

its simple: open a image with a browser and copy the whole url of the pic and paste it as a parameter of the Image object. DO NOT REMOVE "file///:" because this makes the image loadable. you will get your other logic from there. happy coding e.g

Image image = new Image("file:///C:/Users/Nigel/Desktop/my_image.png");
ImageView imgview = new ImageView(image);
眼泪都笑了 2024-12-18 02:26:49

抱歉,如果我的回答有点晚了,但这就是我的方法,无论你把文件放在哪里,它都 100% 有效
1.将图像分配给文件
2.将文件解析为URI
3.将 uri.toString() 分配给图像

ex 代码:

File imageFile = new File("path/to/image/outside/your/jar/awesomeless.jpg");
String fileLocation = imageFile.toURI().toString();
Image fxImage = new Image(fileLocation);    

或者您可以简单地将它们放在一起,如下所示:

Image fxImage = new Image(new File("path/.../awesomemore.jpg").toURI().toString());

如果有人知道更好的方法,请告诉我们!

sorry if my answer came a little bit late but that was my approach and it works 100% wherever you are putting your file
1.assign your image to File
2.parse the File to URI
3.assign the uri.toString() to the image

ex code:

File imageFile = new File("path/to/image/outside/your/jar/awesomeless.jpg");
String fileLocation = imageFile.toURI().toString();
Image fxImage = new Image(fileLocation);    

or you can simply put it all together like this:

Image fxImage = new Image(new File("path/.../awesomemore.jpg").toURI().toString());

and please if anyone knows a better approach let us know!

人生戏 2024-12-18 02:26:49

以前的答案都不适合我。然而,我不久前看到过这个,但找不到原始链接,它工作得完美无缺。

@Override
public void start(Stage primaryStage) throws Exception {
    //create a pane to hold the image views
    Pane pane = new HBox(10);
    pane.setPadding(new Insets(5,5,5,5));

    //create the image to be used!
    Image image = new Image("/Content/vortex.jpg");

    //set some custom properties and add an image
    ImageView imageView = new ImageView(image);
    imageView.setFitHeight(100);
    imageView.setFitWidth(100);
    pane.getChildren().add(imageView);

    //add the second image view with this image and no custom properties
    pane.getChildren().add(new ImageView(image));

    ImageView imageView2 = new ImageView(image);
    imageView2.setRotate(45);
    pane.getChildren().add(imageView2);

    Scene scene = new Scene(pane, 200, 200);
    primaryStage.setScene(scene);
    primaryStage.show();
}

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

关键是,当您创建图像时,以“/”开始路径(即:“/Content/vortex.jpg”)。请注意,在此设置中,根文件夹是大多数 IDE 中的 src 文件夹。

None of the previous answers worked for me. However, this one, which I saw a while back but can't find an original link from, works flawlessly.

@Override
public void start(Stage primaryStage) throws Exception {
    //create a pane to hold the image views
    Pane pane = new HBox(10);
    pane.setPadding(new Insets(5,5,5,5));

    //create the image to be used!
    Image image = new Image("/Content/vortex.jpg");

    //set some custom properties and add an image
    ImageView imageView = new ImageView(image);
    imageView.setFitHeight(100);
    imageView.setFitWidth(100);
    pane.getChildren().add(imageView);

    //add the second image view with this image and no custom properties
    pane.getChildren().add(new ImageView(image));

    ImageView imageView2 = new ImageView(image);
    imageView2.setRotate(45);
    pane.getChildren().add(imageView2);

    Scene scene = new Scene(pane, 200, 200);
    primaryStage.setScene(scene);
    primaryStage.show();
}

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

The key is that when you create the image, START the path with a '/' (ie: "/Content/vortex.jpg"). Note that in this setup, the root folder is the src folder in most IDEs.

绅士风度i 2024-12-18 02:26:49

另一种解决方案是将InputStream传递到Image类构造函数中;并且正在工作...

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

    java.io.FileInputStream fis = new FileInputStream("/System/Library/CoreServices/loginwindow.app/Contents/Resources/LogOut.png");
    ImageView iv = new ImageView(new Image(fis));

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

Another solution is to pass the InputStream into the Image class constructor; and is working...

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

    java.io.FileInputStream fis = new FileInputStream("/System/Library/CoreServices/loginwindow.app/Contents/Resources/LogOut.png");
    ImageView iv = new ImageView(new Image(fis));

    root.getChildren().add(iv);
    primaryStage.setScene(scene);
    primaryStage.show();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文