Reputation: 89
I am new and learning Java (more as a hobby). I am trying to write a program to display TIFs
in JavaFX
. I understand TIFs
are not part of the basic Java-8 JRE/JDK
and JAI
needs to be used.
First, it looks like I lack the understanding between the different classes of images
, JavaFX Image
, AWT Image
, BufferedImage
, RenderedImage
, Raster
, PlanarImage
, etc.
I think once I can wrap my head around those differences, I can better understand the image processing module.
I was finally able to piece together some code based on examples found. (I love the Java community because it always looks like someone has had a similar issue and someone else has resolved it).
I am able to use the JAI
to decode a TIFF
as a ByteArraySeekableStream
into a RenderedImage
and use SwingFXUtils
to convert to a JavaFX image
.
Issue: The file I am trying to process is 50-M
. I kept receiving a java.lang.reflect.InvocationTargetException
and not understanding why! (still learning).
It wasn't until I processed a 30-kB
TIFF
that actually displayed the processed image, that I realized the java.lang.reflect.InvocationTargetException
was due to java.lang.OutOfMemoryError
: Java heap space.
Is there a better memory managed way to process these large TIF files?
static Image load(byte[] inData) throws Exception {
Image image = null;
SeekableStream inStream = new ByteArraySeekableStream(inData);
ImageDecoder dec
= ImageCodec.createImageDecoder("tiff", inStream, null);
System.out.println(dec.getInputStream());
RenderedImage im = dec.decodeAsRenderedImage();
image = SwingFXUtils.toFXImage(PlanarImage.wrapRenderedImage(im).getAsBufferedImage(),null);
return image;
}
@Override
public void start(Stage primaryStage) throws Exception {
String path = "charts\\DTW_sectional\\Detroit SEC 98.tif";
//String path = "charts\\javafx-documentation.tif";
FileInputStream in = new FileInputStream(path);
FileChannel channel = in.getChannel();
ByteBuffer buffer = ByteBuffer.allocate((int) channel.size());
channel.read(buffer);
// create JavaFX image and load into an ImageView
Image image = load(buffer.array());
System.out.println("Path: " + path + "\nImage: "
+ image + "\n");
ImageView imageView = new ImageView(image);
//Creating a Group object
Group root = new Group(imageView);
//Creating a scene object
Scene scene = new Scene(root, 600, 500);
//Setting title to the Stage
primaryStage.setTitle("Loading an image");
//Adding scene to the stage
primaryStage.setScene(scene);
//Displaying the contents of the stage
primaryStage.show();
}
Upvotes: 0
Views: 411