tags:

views:

122

answers:

2

Hi,

I want to resize a .tiff file. I have used JAI toolkit to resize different types of images. Here is what I have tried to implement:

int imageWidth = 330;
        int imageHeight = 490;

        BufferedImage tempImage = new BufferedImage(imageWidth, imageHeight,BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2D = tempImage.createGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        graphics2D.drawImage(tempImage, 0, 0, imageWidth, imageHeight, null);
        graphics2D.dispose();           

        File outfile = new File("D:/Work/YoursGallery/output.tif");

        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outfile));

        FileSeekableStream ss = new FileSeekableStream("D:/Work/YoursGallery/sample1.tif");

        ImageDecoder dec = ImageCodec.createImageDecoder("tiff", ss, null);  
        TIFFEncodeParam param = new TIFFEncodeParam();
        param.setTileSize(tempImage.getWidth(), tempImage.getHeight());


        TIFFImageEncoder encoder = (TIFFImageEncoder) TIFFCodec.createImageEncoder("tiff", out, param);         
        encoder.encode(dec.decodeAsRenderedImage());

        out.close();

The image created is having same size as original image has. Can anyone please tell what is the issue?

Here is the sample tiff image which I am using to test it.

http://docs.google.com/fileview?id=0BxCDhEXNFvbeMTYyMGZmNDYtODhhNy00YWI3LTkxNDgtZTNhM2FhMjg5Y2Q3&hl=en&authkey=CPCEypgM

Thanks in advance.

A: 

That's because you are writing out tempImage which is still the original image.

graphics2D.drawImage(image, 0, 0, imageWidth, imageHeight, null);

change that to:

graphics2D.drawImage(tempImage, 0, 0, imageWidth, imageHeight, null);

or change your other code to write out image instead of tempImage

--Edit--

OK Attempt 2. Maybe having the source and destination the same is daft.

   BufferedImage bsrc = ImageIO.read(new File(src));
   BufferedImage bdest =
      new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
   Graphics2D g = bdest.createGraphics();
   AffineTransform at =
      AffineTransform.getScaleInstance((double)width/bsrc.getWidth(),
          (double)height/bsrc.getHeight());
   g.drawRenderedImage(bsrc,at);

Try that :)

willcodejavaforfood
Changes as per suggestion but still getting image having same dimension as original image has.
Amit Patel
Sorry mate, I have one more suggestion for you before I give up :)
willcodejavaforfood
It creates image with expected dimension but entire image is black.
Amit Patel
A: 

1) You are writing tempImage inside itself:

graphics2D.drawImage(tempImage, 0, 0, imageWidth, imageHeight, null);

Should be:

graphics2D.drawImage(originalImage, 0, 0, imageWidth, imageHeight, null);

2) You are writing the image that you just read (why are You reading it btw?):

encoder.encode(dec.decodeAsRenderedImage());

Should be:

encoder.encode(tempImage);
Maurice Perry
Changes applied and tested but still getting black image.
Amit Patel