-
TIFF dpi scale
Hi, I have a Tiff with horizontal resolution of 204 dpi and verticale res of 84.
In order to see the image not streched I wrote the following code ( setting xres and yres = 200 dpi):
Code:
public class TiffRescale {
private static int DPI_X = 200;
private static int DPI_Y = 200;
public static void main(String args[]) throws FileNotFoundException, IOException {
try{
File fInputFile = new File("d:\\tif1.tif");
InputStream fis = new BufferedInputStream(new FileInputStream(
fInputFile));
IIOImage IIbi;
TIFFImageReaderSpi spi = new TIFFImageReaderSpi();
ImageReader reader = spi.createReaderInstance();
ImageInputStream iis = ImageIO.createImageInputStream(fis);
ImageReadParam readParam = reader.getDefaultReadParam();
reader.setInput(iis, false);
IIbi = reader.readAll(0, readParam);
TIFFImageWriterSpi spiTiff = new TIFFImageWriterSpi();
ImageWriter tiffWriter = spiTiff.createWriterInstance();
//lettura metadati
ImageTypeSpecifier imageType =
ImageTypeSpecifier.createFromRenderedImage(IIbi.getRenderedImage());
IIOMetadata imageMetadata =
tiffWriter.getDefaultImageMetadata(imageType, null);
/////////////////////////////
//imageMetadata = setDPIViaAPI(imageMetadata);
//provare:
//http://www.java.net/node/659630
// Derive the TIFFDirectory from the metadata.
TIFFDirectory dir = TIFFDirectory.createFromMetadata(imageMetadata);
/*
// Set tag value and tag name.
String tagName = "XResolution";
int tagNumber = 282;
// Add a value to the tag.
String[] _value = { "200" };
// Create a new TIFF field including a new TIFF ASCII TIFF tag.
TIFFTag newTag = new TIFFTag(tagName, tagNumber, (1 << TIFFTag.TIFF_ASCII));
TIFFField newField = new TIFFField(newTag, TIFFTag.TIFF_ASCII, 1, _value);
dir.addTIFFField(newField);
*/
// Get {X,Y}Resolution tags.
BaselineTIFFTagSet base = BaselineTIFFTagSet.getInstance();
TIFFTag tagXRes = base.getTag(BaselineTIFFTagSet.TAG_X_RESOLUTION);
TIFFTag tagYRes = base.getTag(BaselineTIFFTagSet.TAG_Y_RESOLUTION);
//TIFFTag tagXWidth = base.getTag(BaselineTIFFTagSet.TAG_IMAGE_WIDTH);
//TIFFTag tagRowsPerStrip = base.getTag(BaselineTIFFTagSet.TAG_ROWS_PER_STRIP);
//TIFFField fieldRowsPerStrip = new TIFFField(tagRowsPerStrip, TIFFTag.TIFF_SHORT, 1, (Object)new char[]{2200});
// Create {X,Y}Resolution fields.
TIFFField fieldXRes = new TIFFField(tagXRes, TIFFTag.TIFF_RATIONAL,
1, new long[][] {{200, 1}});
TIFFField fieldYRes = new TIFFField(tagYRes, TIFFTag.TIFF_RATIONAL,
1, new long[][] {{200, 1}});
//TIFFField fieldXWidth = new TIFFField(tagXWidth, TIFFTag.TIFF_RATIONAL,
// 1, new long[][] {{800, 1}});
// Append {X,Y}Resolution fields to directory.
//dir.addTIFFField(fieldRowsPerStrip);
dir.addTIFFField(fieldXRes);
dir.addTIFFField(fieldYRes);
//dir.addTIFFField(fieldXWidth);
// Convert to metadata object and return.
IIOMetadata newMeta = dir.getAsMetadata();
//imageMetadata = dir.getAsMetadata();
//////////////////////////
File fOutputFile = new File("d:\\intermedio.tiff");
ImageOutputStream ios = ImageIO.createImageOutputStream(fOutputFile);
tiffWriter.setOutput(ios);
tiffWriter.write(newMeta,
new IIOImage(IIbi.getRenderedImage(),null,null),
(TIFFImageWriteParam)tiffWriter.getDefaultWriteParam());
ios.close();
}
catch(Exception e)
{System.out.print(e.toString());
}
}
/**
* Enrico : Set Tiff
*
*/
private static IIOMetadata setDPIViaAPI(IIOMetadata imageMetadata)
throws IIOInvalidTreeException {
// Derive the TIFFDirectory from the metadata.
TIFFDirectory dir = TIFFDirectory.createFromMetadata(imageMetadata);
// Get {X,Y}Resolution tags.
BaselineTIFFTagSet base = BaselineTIFFTagSet.getInstance();
TIFFTag tagXRes = base.getTag(BaselineTIFFTagSet.TAG_X_RESOLUTION);
TIFFTag tagYRes = base.getTag(BaselineTIFFTagSet.TAG_Y_RESOLUTION);
//TIFFTag tagXWidth = base.getTag(BaselineTIFFTagSet.TAG_IMAGE_WIDTH);
//TIFFTag tagRowsPerStrip = base.getTag(BaselineTIFFTagSet.TAG_ROWS_PER_STRIP);
//TIFFField fieldRowsPerStrip = new TIFFField(tagRowsPerStrip, TIFFTag.TIFF_SHORT, 1, (Object)new char[]{2200});
// Create {X,Y}Resolution fields.
TIFFField fieldXRes = new TIFFField(tagXRes, TIFFTag.TIFF_RATIONAL,
1, new long[][] {{200, 1}});
TIFFField fieldYRes = new TIFFField(tagYRes, TIFFTag.TIFF_RATIONAL,
1, new long[][] {{200, 1}});
//TIFFField fieldXWidth = new TIFFField(tagXWidth, TIFFTag.TIFF_RATIONAL,
// 1, new long[][] {{800, 1}});
// Append {X,Y}Resolution fields to directory.
//dir.addTIFFField(fieldRowsPerStrip);
dir.addTIFFField(fieldXRes);
dir.addTIFFField(fieldYRes);
//dir.addTIFFField(fieldXWidth);
// Convert to metadata object and return.
return dir.getAsMetadata();
}
}
the issue is that the result "intermedio.tiff" is streched as before and has xres = 1 dpi = yres = 1 DPI !!!
Can anyone help me?
thanks
regards,
Enrico
-
Re: TIFF dpi scale
Moved from 'Advanced Java'
db