1
votes

I am putting together an image gallery that displays a preview of the image. For most of the images this is no problem, only when the image is an animated gif the image is animated as well, which in this case is not necessary. What I would like to do for an animated gif is to just display one frame of the animation.

While searching for a solution I have come across this.

GifDecoder d = new GifDecoder();
d.read( filename);
WritableImage wimg = null;
Image img = SwingFXUtils.toFXImage(d.getFrame(0), wimg);

But first of all this seems to be too much overhead and a quick test so far ended in a NullPointerException due to the fact that d.getFrame(0) returns null.

I was thinking that there must be an easier way, like some property on the Image to avoid animating the image, or as an alternative approach not displaying the image in an ImageView.

1

1 Answers

0
votes

There were two reasons the GifDecoder did not work.

The first one was really my mistake, as the String passed to the read method should be an URI:

File f = new File(filename);
d.read(f.toURI().toString());

The second issue lies with the GifDecoder: To check the input URI it is converted to lower case and that converted String is then used to create in InputStream. This works on an OS that is not case sensitive (like Windows) but not on NIX based systems. I therefore had to fix that and now it is working.