Scale multi page TIFF Image in java

前端 未结 1 662
温柔的废话
温柔的废话 2021-01-17 02:28

I want to change the height of multi page TIFF image so I am using below code snippet to scale it. But It returns just first page from the tiff file , I guess it converts it

相关标签:
1条回答
  • 2021-01-17 02:57

    ImageIO.write(...) will only ever write single stand-alone images. Writing multiple images to the same output stream does not remedy this. However the ImageIO package comes with full support for what you want, it just requires more code.

    Obtain the proper ImageWriter for TIFF format, using :

    ImageWriter writer =  ImageIO.getImageWritersByFormatName("TIFF").next();  // Assumes TIFF plugin installled
    

    See the API docs for ImageIO.getImageWritersByFormatName(String) for more information.

    Then use writer.canWriteSequence() to tell if your writer instance supports writing sequences. It should return truefor TIFF. If not, you need to find a different plugin.

    Next, use writer.prepareWriteSequence(...) to prepare your image sequence.

    For each image (page) you want to append, use writer.writeToSequence(new IIOImage(..., bufferedImage, null), ...) in a for loop.

    Then finally, outside the for loop use writer.endWriteSequence() to end the image sequence.

    Hope these pointers should get you going in the right direction.

    Update: Here's a cleaned up and modified version of your code that I think should work (I don't have and can't install JAI on my work laptop).

    The important changes are removing the writer.setOutput(ios) from the loop (you should only set it once), and moving writer.endWriteSequence() oustide the loop. I also removed the BufferedImage array, to avoid keeping all the images in memory, you might want to keep it for convenience.

    Let me know if you still have trouble, and I'll see what I can do.

    public static byte[] resize(byte[] img) throws IOException {
        byte[] outimage = null;
    
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream(30000);
            ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
    
            ImageReader reader = getTiffImageReader();
            ImageInputStream imageStream = ImageIO.createImageInputStream(new ByteArrayInputStream(img));
            reader.setInput(imageStream);
    
            int pages = reader.getNumImages(true);
    
            Iterator<ImageWriter> imageWriters = ImageIO.getImageWritersByFormatName("TIFF");
            ImageWriter writer = imageWriters.next();
    
            writer.setOutput(ios);
            ImageWriteParam writeParam = writer.getDefaultWriteParam();
            writeParam.setTilingMode(ImageWriteParam.MODE_DEFAULT);
    
            writer.prepareWriteSequence(reader.getStreamMetadata()); // You want the stream metadata here
    
            for (int i = 0; i < pages; i++) {
                IIOImage iioImage = reader.readAll(i, null); // Save some lines by using readAll
    
                BufferedImage image = (BufferedImage) iioImage.getRenderedImage();
    
                // Modify image here...                
    
                iioImage.setRenderedImage(image);
    
                writer.writeToSequence(iioImage, writeParam);
            }
    
            writer.endWriteSequence(); // Crucial, must be done outside loop
    
            ios.flush();
            ios.close();
    
            outimage = baos.toByteArray();            
            baos.close();
    
            writer.dispose();
            reader.dispose();
    
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        return outimage;
    }
    
    0 讨论(0)
提交回复
热议问题