Creating a image thumbnail in javafx

≡放荡痞女 提交于 2019-12-10 15:55:12

问题


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.


回答1:


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

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.

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.

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

ImageView imageView = new 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.



来源:https://stackoverflow.com/questions/34786958/creating-a-image-thumbnail-in-javafx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!