Java >> JAI funky tiff dpi makes pdf conversion scrunched to half page

by dwilson » Sat, 09 Apr 2005 02:40:03 GMT

Hello Everybody,

I am trying to convert various tiff images into pdf, and have
encountered a problem when the tiff image has a funky dpi, i.e.
x-dpi=203 & y-dpi=96. The image is scrunched to a half page towards the
bottom of the pdf. The temp tiff created is also scrunched to about
half. I was looking for a way to compensate for this, so that it takes
up the whole page. A link to this image
http://www.cranberymedia.net/danw/1388242_1.FOB3.tif and here is my
code...

import java.awt.image.RenderedImage;
import java.io.*;
import java.util.*;

import javax.media.jai.JAI;
import javax.media.jai.RenderedOp;

import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
import com.sun.media.jai.codec.*;
import com.sun.media.jai.codecimpl.*;

public class test {

public static void main(String[] args) {
go();
}

private static void go() {
try {
File imageFile = new File("c:\\work\\1388242_1.FOB3.tif");
SeekableStream ss = new FileSeekableStream(imageFile);
ImageDecoder decoder = ImageCodec.createImageDecoder("tiff", ss,
null);
int count = decoder.getNumPages();
InputStream images[] = new InputStream[count];
for (int j = 0; j < decoder.getNumPages(); j++) {
TIFFEncodeParam param = new TIFFEncodeParam();
param.setCompression(TIFFEncodeParam.COMPRESSION_GROUP3_1D);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
RenderedImage rImage = decoder.decodeAsRenderedImage(j);

TIFFImageEncoder encoder = (TIFFImageEncoder) TIFFCodec
.createImageEncoder("tiff", baos, param);
encoder.encode(rImage);
baos.toByteArray();
InputStream inputStream = new ByteArrayInputStream(baos
.toByteArray());
images[j] = inputStream;
}

FileOutputStream fout = new FileOutputStream("c:\\work\\bpdf.pdf");

java.util.List pages = new ArrayList();
Rectangle pagesize = PageSize.LETTER;

for (int i = 0; i < images.length; i++) {
InputStream tempInputStream = images[i];
File tempIs = File.createTempFile("pdfTempIn"
+ (new Date()).getTime() + "" + i, null);
tempIs.deleteOnExit();
FileOutputStream outputStream = new FileOutputStream(tempIs);
int b = tempInputStream.read();
while (b != -1) {
outputStream.write(b);
b = tempInputStream.read();
}
outputStream.flush();
outputStream.close();
tempInputStream.close();
tempInputStream = new FileInputStream(tempIs);
RenderedOp image1 = JAI.create("stream", SeekableStream
.wrapInputStream(tempInputStream, true));
Image img = Image.getInstance(image1.getAsBufferedImage(),
null, true);
img.scaleToFit(pagesize.width(), pagesize.height());
img.setAbsolutePosition(0, 0);
pages.add(img);
tempInputStream.close();
}
int leftMargin = 0;
int rightMargin = 0;
int topMargin = 0;
int bottomMargin = 0;
Document document = new Document(pagesize, leftMargin, rightMargin,
topMargin, bottomMargin);
PdfWriter writer = PdfWriter.getInstance(document, fout);
writer.open();
document.open();
PdfContentByte cb = writer.getDirectContent();
Iterator pi = pages.iterator();
while (pi.hasNext()) {
Image img = (Image) pi.next();
cb.addImage(img);
document.newPage();
}
document.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("done");
System.exit(0);
}
}

Any insight on this issue would be greatly appreciated.

dwilson



Similar Threads

1. Wrong image size of JPG after TIFF to JPG conversion with JAI - Java

2. JAI Problem - TIFF Image to JPEG conversion

Hi,

   I am trying to convert TIFF Image to JPEG. For some of the images I
get the following exception.

   My first thought was may be the image is not correct. I can view
the Tiff image ok and I can convert to PNG as well.

   Please post here if you have any info on this exception.

Regards,
Suren

EXCEPTION DETAILS:
      java.lang.ArrayIndexOutOfBoundsException: 44544
	at com.sun.media.jai.util.SunTileScheduler.scheduleTile(SunTileScheduler.java:941)
	at javax.media.jai.OpImage.getTile(OpImage.java:1139)
	at javax.media.jai.PlanarImage.getData(PlanarImage.java:2220)
	at javax.media.jai.PlanarImage.getData(PlanarImage.java:2029)
	at javax.media.jai.RenderedOp.getData(RenderedOp.java:2277)
	at com.sun.media.jai.codecimpl.JPEGImageEncoder.encode(JPEGImageEncoder.java:180)

SOURCE CODE:
   try{
   	
        RenderedOp source = JAI.create("fileload", filename);   	
 
        FileOutputStream stream = null;

        stream = new FileOutputStream(jpegFileName );
   	com.sun.media.jai.codec.JPEGEncodeParam JPEGparam = new
	com.sun.media.jai.codec.JPEGEncodeParam();
	ImageEncoder encoder =
ImageCodec.createImageEncoder("jpeg",stream,JPEGparam);

	 encoder.encode(source);
   }catch(Exception e){
   	e.printStackTrace();
   }

3. DPI for TIFF - Java

4. JAI: Tiff b/w to gray

I'm using JAI for reading Tiff images: black and white, 1 bit depth. I
want to scale them to reduce their size, but I want to do
antialiasing. If I scale them as they are (using the scale operator)
the images remain b/w and no antialiasing is done (I'm using bilinear
interpolation).

So I'm trying to convert the image to gray before scaling. But an
exception raises when trying to do the ColorConvert operation, because
"the number of bands in the source raster image is different of the
number of components in the source color space" (normal, because it's
an IndexedColorModel).

Anyone can help me?. This is the source code:


        MemoryCacheSeekableStream seekStream = new
MemoryCacheSeekableStream (image);
        TIFFDecodeParam tiffParam = new TIFFDecodeParam();
        img = TIFFDescriptor.create (seekStream, tiffParam, new
Integer(0), null);
        byte [] c = new byte[256];
        for (int i=0; i<c.length; i++)  c[i] = (byte) i;
        ColorModel cm = new IndexColorModel (8,256, c,c,c);
        img = ColorConvertDescriptor.create (img, cm,
null).getAsBufferedImage();


Thanks in advance,
Andres

5. How to convert a Fax-Tiff with JAI to a PNG - Java

6. JAI - How to convert PNG to TIFF

Hi,

JAI can be used to convert PNG images files to TIFF:


	String source = "c:/tmp/1.png";
	FileOutputStream out = new FileOutputStream ("c:/tmp/2.tiff");
	RenderedOp src = JAI.create("fileload", source);
	TIFFImageEncoder encoder = new TIFFImageEncoder (out, null);
	encoder.encode (src);

1. I don't want to run it file based, so I was wondering how to pass the 
source file as a stream.

2. A 15K PNG file is blown up to 2.5 MB TIFF. How can I use a compression?

thank you

Armin

7. Displaying Palette Tiff Image using JAI (very slow when scale operation is performed) - Java

8. JAI, Image I/O, TIFF Images, and ImageReadParam