0

I have to load images:

Image image = new Image(f.toURI().toString(), width, height, true, true);

Since I also have tiffs, I have to load them differently using JAI:

BufferedImage read = ImageIO.read(f);
Image image = SwingFXUtils.toFXImage(read, null);

Now I have an Image object, but it has the wrong size. Image offers no methods to resize the object. How do I resize is so it has the same size as if I would load a jpg or png with the shown line?

user1406177
  • 1,328
  • 2
  • 22
  • 36
  • Maybe you can read the tiff, convert it to for ex. to bmp and open it again as an Image? http://stackoverflow.com/a/2898596/4170073 – aw-think Sep 09 '15 at 17:07

1 Answers1

2

Seems like there should be an easier way to do this, but you can try:

BufferedImage read = ImageIO.read(f);
int[] pixels = new int[width * height] ;
PixelGrabber grabber = new PixelGrabber(read.getScaledInstance(width, height, Image.SCALE_SMOOTH), 
    0, 0, width, height, 0, pixels, width);
grabber.grabPixels();
WritableImage fxImage = new WritableImage(width, height);
PixelWriter pw = fxImage.getPixelWriter();
pw.setPixels(0, 0, width, height, PixelFormat.getIntArgbInstance(), pixels, 0, width);

Now fxImage is a javafx.scene.image.Image that has dimensions width by height.

Be aware that PixelGrabber.grabPixels() is a blocking call, so you will need to handle InterruptedExceptions. For large images you might want to do this in a Task executed on a background thread, so as not to block the FX Application Thread.

James_D
  • 201,275
  • 16
  • 291
  • 322