2

我想调整 .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

提前致谢。

4

2 回答 2

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

试试看:)

于 2010-07-22T08:56:45.880 回答
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);
于 2010-07-22T12:02:28.013 回答