在JavaFX中创建图像缩略图 [英] Creating a image thumbnail in javafx

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

问题描述

有人可以帮忙提供一些用于在JavaFx中为图像创建缩略图的代码吗.

Can someone please help with some code for creating a thumbnail for a image in JavaFx.

我是新来的,因此请逐步解释.

I'm new at this, so a step by step explanation would be appreciated.

推荐答案

您可以使用Image构造函数从较大的图像创建缩略图,这是

You can use an Image constructor to create a thumbnail image from a larger image, here is a sample from the Image javadoc:

// load an image and resize it to width of 100 while preserving its
// original aspect ratio, using faster filtering method
// The image is downloaded from the supplied URL through http protocol
Image image3 = new Image("http://sample.com/res/flower.png", 100, 0, false, false);

Image构造函数要做的是加载图像并调整其大小,仅将图像像素存储在内存中,因此它是I/O和处理器密集型的,但是占用内存少.

What the Image constructor does is load the image and resize it, storing only the image pixels in memory, so it is I/O and processor intensive, but memory light.

请注意,如果您经常执行此操作,则该过程将变得相当昂贵,这就是为什么某些图像查看系统在创建缩略图之后将其保存到磁盘的原因,因此下次需要从磁盘读取缩略图时,而不是读取和调整整个原始文件的大小. ImageIO 可以与 SwingFXUtils 可以将调整大小的图像持久保存到磁盘做到这一点.

Note that if you do this a lot, it becomes pretty process expensive, which is why some image viewing systems save the thumbnail images to disk after they have created them, so that next time a thumbnail is needed it is read from disk rather than reading and resizing the entire original file. ImageIO can be used with SwingFXUtils to persist resized images to disk if you wish to do that.

创建大小调整后的图像后,可以将其放置在ImageView中进行显示:

After you have created the resized Image, you can place it in an ImageView for display:

ImageView imageView = new ImageView(image);

您可以使用 ImageView 通过操纵ImageView的视口或fitHeight和fitWidth属性来调整图像视图的大小.如果您有很多缩略图,则不希望这样做.在ImageView中而不是Image构造函数中调整图像的大小意味着支持ImageView的Image保持完整大小,当您拥有很多图像时,这将迅速消耗大量内存.

You can use an ImageView to resize the view of images by manipulating the ImageView's viewport or fitHeight and fitWidth properties. If you have many thumbnail images, you would not wish to do that. Resizing the images in the ImageView rather than the Image constructor means that the Image backing the ImageView remains full size, which will quickly consume a lot of memory when you have a lot of images.

这篇关于在JavaFX中创建图像缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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