如何在java中调整tiff图像的大小?

时间:2010-07-22 08:51:08

标签: java image jai

我想调整.tiff文件的大小。我使用JAI工具包来调整不同类型的图像。以下是我试图实施的内容:

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();

创建的图像与原始图像具有相同的尺寸。任何人都可以告诉我们这是什么问题吗?

以下是我用来测试它的样本tiff图像。

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

提前致谢。

2 个答案:

答案 0 :(得分:1)

那是因为你正在写出仍然是原始图像的tempImage。

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

将其更改为:

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

或更改您的其他代码以写出图像而不是tempImage

- 编辑 -

确定尝试2.可能源头和目的地相同是愚蠢的。

   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);

试试:)

答案 1 :(得分:0)

1)你正在自己编写tempImage:

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

应该是:

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

2)你正在写你刚刚读过的图像(你为什么要读它?):

encoder.encode(dec.decodeAsRenderedImage());

应该是:

encoder.encode(tempImage);