保存多层TIFF图像,而不会丢失Java中的图层信息

时间:2015-04-06 11:29:48

标签: java spring image tiff jai

我试图使用Spring和JAI ImageIO保存通过浏览器上传的tiff图像。我可以使用下面的代码保存图像,但问题是保存的图像没有上传之前的图层(tiff图像组成的图层)。

是否还有其他参数可以确保保存的图像还有图层?

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();
}

1 个答案:

答案 0 :(得分:0)

我在这里使用答案找到了解决方案: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();
}