Results 1 to 2 of 2

Thread: TIFF dpi scale

  1. #1
    enrico is offline Member
    Join Date
    Jan 2012
    Posts
    1
    Rep Power
    0

    Default 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):


    Java 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
    Last edited by JosAH; 01-30-2012 at 05:17 PM. Reason: added [code] ... [/code] tags

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: TIFF dpi scale

    Moved from 'Advanced Java'

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Need advice on best way to read tiff files
    By madroadbiker in forum NetBeans
    Replies: 0
    Last Post: 04-29-2011, 12:54 PM
  2. tiff image in ie7 is not working
    By kailash in forum Java Servlet
    Replies: 1
    Last Post: 02-06-2011, 04:43 PM
  3. Replies: 0
    Last Post: 11-03-2009, 11:36 AM
  4. Tiff Images Tranfer
    By pkarka@gmail.com in forum Advanced Java
    Replies: 3
    Last Post: 08-05-2009, 05:42 AM
  5. Problem in TIFF Conversion
    By lakshmipriyam in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2007, 10:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •