0

I'm trying to save tiff images uploaded via browser using Spring and JAI ImageIO. I can save the image using the code below but the problem is that the saved image doesn't have the layers(the ones that tiff images consist of) which it has before uploading.

Are there any other parameters to ensure that the saved image also has the layers?

private void saveTiffImage(byte[] bytes, String uuid) throws Exception {
    SeekableStream stream = new ByteArraySeekableStream(bytes);

    String[] names = ImageCodec.getDecoderNames(stream);
    ImageDecoder dec =
            ImageCodec.createImageDecoder(names[0], stream, null);
    RenderedImage im = dec.decodeAsRenderedImage();

    String fileName = uuid + ".tif";

    com.sun.media.jai.codec.TIFFEncodeParam params = new com.sun.media.jai.codec.TIFFEncodeParam();
    params.setCompression(com.sun.media.jai.codec.TIFFEncodeParam.COMPRESSION_PACKBITS);

    FileOutputStream os = new FileOutputStream(IMG_LOCATION + fileName);

    javax.media.jai.JAI.create("filestore", im, IMG_LOCATION + fileName, "TIFF", params);

    os.flush();
    os.close();
}
cuneyttyler
  • 1,255
  • 2
  • 17
  • 27

1 Answers1

0

I've found the solution using the answer here : How to combine two or many tiff image files in to one multipage tiff image in JAVA.

private Object[] saveTiffImage(byte[] bytes, String uuid) throws Exception {
    SeekableStream stream = new ByteArraySeekableStream(bytes);

    String[] names = ImageCodec.getDecoderNames(stream);
    ImageDecoder dec =
            ImageCodec.createImageDecoder(names[0], stream, null);

    // Here we get the other pages.   
    Vector vector = new Vector();
    int pageCount = dec.getNumPages();
    for (int i = 1; i < pageCount; i++) {
        RenderedImage im = dec.decodeAsRenderedImage(i);
        vector.add(im);
    }

    String fileName = uuid + ".tif";

    com.sun.media.jai.codec.TIFFEncodeParam params = new com.sun.media.jai.codec.TIFFEncodeParam();
    params.setCompression(com.sun.media.jai.codec.TIFFEncodeParam.COMPRESSION_PACKBITS);
    // Then set here
    params.setExtraImages(vector.iterator()); 

    // This is the first page
    RenderedImage im = dec.decodeAsRenderedImage(0);

    FileOutputStream os = new FileOutputStream(IMG_LOCATION + fileName);

    javax.media.jai.JAI.create("filestore", im, IMG_LOCATION + fileName, "TIFF", params);

    os.flush();
    os.close();
}
Community
  • 1
  • 1
cuneyttyler
  • 1,255
  • 2
  • 17
  • 27