Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7844801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:00:32+00:00 2026-06-02T17:00:32+00:00

I’ve seen a few questions regarding multipage tiffs and a few questions about compression,

  • 0

I’ve seen a few questions regarding multipage tiffs and a few questions about compression, but none (that I’ve seen) linking the two. This question is as close as I’ve seen and puts me incredibly close, so I hope. I went into the Oracle forum thread mentioned (it is talking about a multipage PDF to TIFF with compression) and I think I’m closish to having the code finished to do this. Can anyone assist? I’m going to remove the try/catches to try and shorten this down a hair (basically all they did was output a message in console and return false).

 public static boolean CompressedTiff(List<BufferedImage> images, File path)
 {
    if (!path.getParentFile().exists())
         path.getParentFile().mkdirs();
    path.createNewFile();
    ImageOutputStream ios;
         ios = ImageIO.createImageOutputStream(path);

    Iterator<ImageWriter> imageWriters = ImageIO.getImageWritersByFormatName("TIFF");
    ImageWriter writer = (ImageWriter)imageWriters.next();
    writer.setOutput(ios);
    TIFFImageWriteParam writeParam = (TIFFImageWriteParam)writer.getDefaultWriteParam();
    writeParam.setCompressionMode(2);
    writeParam.setCompressionType("LZW"); 
    writer.prepareWriteSequence(null);

    for(int i = 0; i < images.size(); i++)
    {
        ImageTypeSpecifier spec = ImageTypeSpecifier.createFromRenderedImage(images.get(i));
        javax.imageio.metadata.IIOMetadata metadata = writer.getDefaultImageMetadata(spec, writeParam);
        IIOImage iioImage = new IIOImage(images.get(i), null, metadata);
        writer.writeToSequence(iioImage, writeParam);
        images.get(i).flush();//modified after release.

        images.get(i).flush();
        writer.endWriteSequence();
        ios.flush();
        writer.dispose();
        ios.close();
    }
    return true;

}

It failed out on the next pass at writer.writeToSequence saying I needed to call prepareWriteSequence. I changed it to

 writer.prepareWriteSequence(metadata);
 writer.writeToSequence(iioImage, writeParam);

also removed the earlier writer.prepareWriteSequence(null);

and it appears to be navigating the files properly, however, the output isn’t of any type of renderable tif. Multipage or otherwise.

I have JAI installed so if it is possible to use that in some way to achieve the compressed image, that’d be fantastic. The code I am using that generates a TIFF is using this, but I haven’t seen anything that worked as far as adding compression to the pages.

edit: I added a bunch of ios.flush(); ios.close(); calls in the catch blocks and it prevents the non-renderable TIFF issue. However, it isn’t adding any page beyond the first.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T17:00:34+00:00Added an answer on June 2, 2026 at 5:00 pm

    If it helps, this is code that I use for modifying a TiffImageWriteParam to set the compression:

    try {
        jWriteParam.setCompressionMode(_compression != TiffCompression.NO_COMPRESSION 
                      ? ImageWriteParam.MODE_EXPLICIT : ImageWriteParam.MODE_DISABLED);
    
        if (_compression != TiffCompression.NO_COMPRESSION) {
            // this code corrects the compression if, say, the client code asked for
            // CCITT but the actual image pixel format was CMYK or some other non-1 bit
            // image type.
            TiffCompression mode = recastToValidCompression(_compression, pf);
            jWriteParam.setCompressionType(getCompressionType(mode));
            TIFFCompressor compressor = getTiffCompressor(mode, jWriteParam, shouldUsePredictor(pf));
            jWriteParam.setTIFFCompressor(compressor);
            if (_compression == TiffCompression.JPEG_COMPRESSION) {
                // Java supports setting to 1.0 (ie 100), but it will not actually do lossless (maybe)
                if (_jpegQuality == 100 && !jWriteParam.isCompressionLossless())
                    continue;
                jWriteParam.setCompressionQuality(toJavaJpegQuality());
            }
        }
    }
    catch (UnsupportedOperationException e)
    {
        // this shouldn't get here, but you should consider what to do if it does.
        // set a default? throw?
    }
    

    here is getTiffCompressor():

    private TIFFCompressor getTiffCompressor (TiffCompression compression, TIFFImageWriteParam writeParam, boolean usePredictor)
    {
        int predictor = usePredictor 
                ? BaselineTIFFTagSet.PREDICTOR_HORIZONTAL_DIFFERENCING 
                : BaselineTIFFTagSet.PREDICTOR_NONE;
    
        switch (compression) 
        {
        case GROUP_3_FAX_ENCODING:
            return new TIFFT4Compressor();
        case GROUP_4_FAX_ENCODING:
            return new TIFFT6Compressor();
        case JPEG_COMPRESSION:
            return new TIFFJPEGCompressor(writeParam);
        case MACINTOSH_PACKBITS:
            return new TIFFPackBitsCompressor();
        case DEFLATE:
            return new TIFFDeflateCompressor(writeParam, predictor);
        case LZW:
            return new TIFFLZWCompressor(predictor);
        case MODIFIED_HUFFMAN:
            return new TIFFRLECompressor();
        case NO_COMPRESSION:
        case DEFAULT:
        default:
            return null;
        }
    }
    

    TiffCompression is my own enum that models the compressions that I offer for TIFF files. Finally, here is getCompressionType():

    private String getCompressionType (TiffCompression compression)
    {
        switch (compression)
        {
        case GROUP_3_FAX_ENCODING:
            return "CCITT T.4";
        case GROUP_4_FAX_ENCODING:
            return "CCITT T.6";
        case JPEG_COMPRESSION:
            return "JPEG";
        case MACINTOSH_PACKBITS:
            return "PackBits";
        case DEFLATE:
            return "Deflate";
        case LZW:
            return "LZW";
        case MODIFIED_HUFFMAN:
            return "CCITT RLE";
        case NO_COMPRESSION:
        case DEFAULT:
        default:
            return null;
        }
    }
    

    Now, I can’t show you everything because my code is built to encode an arbitrary number of images and yours is not, so our code structures differ wildly. In my case, I set up the encoder for using a sequence writer with a much more open architecture. I pull in an image, fire an event to optionally change the default compression, create the writer and write param, set metadata/image tags, fire progress events then write the sequence. Then I have to go in and patch the last written ifd because the tiff encoder writes them broken, so they need to be patched.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.