Skip to main content

Last Page has error when generate multi-page tiff

5 replies [Last post]
zombie_do
Offline
Joined: 2009-06-04
Points: 0

Hello:

I used below code to generate multi-page TIFF in byte[], the input is a list of BufferedImage. However, After byte[] is output to a physical TIFF file, the last page has always an error.

Can someone please help me? Thank you very much.

The jar file I am using is jai-imageio-1_2-pre-dr-b04-lib-windows-i586-04_Jun_2009.zip.

public static byte[] convertPDFtoTIFF(List images) throws Exception {
byte[] out = null;

ByteArrayOutputStream baos = null;
ImageOutputStream os = null;
ImageWriter writer = null;
try {
baos = new ByteArrayOutputStream();
os = (ImageOutputStream)(new MemoryCacheImageOutputStream(baos));
writer = (ImageWriter)ImageIO.getImageWritersByFormatName("tiff").next();

writer.setOutput(os);
ImageWriteParam iwp = writer.getDefaultWriteParam();
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionType("CCITT T.6");
writer.prepareWriteSequence(null);
for(int i=0; i

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
zombie_do
Offline
Joined: 2009-06-04
Points: 0

Thank you guys for the help. I figured out the problem. Below is the fixed code.

...

writer.endWriteSequence();

[u][b]

((ImageOutputStream)writer.getOutput()).flush();

((ImageOutputStream)writer.getOutput()).close();

[/b][/u]

out = baos.toByteArray();

...

marcojacob
Offline
Joined: 2006-07-10
Points: 0

Did you also try

os.flush();
os.close();

before baos.toByteArray()? Is writer.getOutput() different from os
saying the writer encapsulates the ImageOutputStream again?

chcornwell
Offline
Joined: 2004-09-16
Points: 0

Do you need to create an IIOWriteProgressListener to wait for an imageComplete event before flushing the output stream?

- Carl

zombie_do
Offline
Joined: 2009-06-04
Points: 0

Thank you Carl.

I added writer.addIIOWriteProgressListener(myListenser); and sleep if myListenser is not finished like below code. but got the same result.

while(!myListener.isDone()){
Thread.sleep(5000);
}
os.flush();

I got the same problem even with one page. I guess if there is bug in writer.writeToSequence.

marcojacob
Offline
Joined: 2006-07-10
Points: 0

try 4 more things:
1. Did you check if the last page is OK?
2. Close os and baos before accessing byte[]
3. Flush baos
4. close writer

Don't know if one helps.