My question is, what how can I successfully load a .tif file into an Image instance in Java?
Let me give some more detail now. I have read a lot of the threads on st
Yes, you need JAI.
import javax.media.jai.PlanarImage;
import com.sun.media.jai.codec.ByteArraySeekableStream;
import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.ImageDecoder;
import com.sun.media.jai.codec.SeekableStream;
import java.awt.Image;
import java.awt.image.RenderedImage;
...
static Image load(byte[] data) throws Exception{
Image image = null;
SeekableStream stream = new ByteArraySeekableStream(data);
String[] names = ImageCodec.getDecoderNames(stream);
ImageDecoder dec =
ImageCodec.createImageDecoder(names[0], stream, null);
RenderedImage im = dec.decodeAsRenderedImage();
image = PlanarImage.wrapRenderedImage(im).getAsBufferedImage();
return image;
}
You don't need to use JAI. I would recommend you use the normal ImageIO API and a TIFF plugin.
For Java 9 and later, a basic TIFF plugin is bundled with the JDK. For earlier versions, or if you need more advanced capabilities, there are several other TIFF plugins for ImageIO available, and depending on your needs, you could use the one from jai-imageio, my TIFF plugin or some other third-party plugin.
My plugin supports most common types of TIFFs. In contrast to jai-imageio, no native libs are required. You can either add Maven dependencies to your project or download directly from this link.
The good thing about using the ImageIO API, is that after you have the plugins properly installed, all you need to do is:
BufferedImage img = ImageIO.read("pics/img"+i+".tif");