如何在 JAVAFX 中加载计算机目录图像
我正在尝试将计算机文件夹图像加载到缩略图墙上。我在另一个帖子上在一个线程上阅读了论坛上说 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
目前尚不清楚您到底想做什么。如果您谈论的是 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.
其简单:使用浏览器打开图像并复制图片的整个 url 并将其粘贴为 Image 对象的参数。不要删除“file///:”,因为这会使图像可加载。你将从那里得到你的其他逻辑。快乐编码例如
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
抱歉,如果我的回答有点晚了,但这就是我的方法,无论你把文件放在哪里,它都 100% 有效
1.将图像分配给文件
2.将文件解析为URI
3.将 uri.toString() 分配给图像
ex 代码:
或者您可以简单地将它们放在一起,如下所示:
如果有人知道更好的方法,请告诉我们!
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:
or you can simply put it all together like this:
and please if anyone knows a better approach let us know!
以前的答案都不适合我。然而,我不久前看到过这个,但找不到原始链接,它工作得完美无缺。
关键是,当您创建图像时,以“/”开始路径(即:“/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.
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.
另一种解决方案是将InputStream传递到Image类构造函数中;并且正在工作...
Another solution is to pass the InputStream into the Image class constructor; and is working...