Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中调整tiff图像的大小?_Java_Image_Jai - Fatal编程技术网

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

如何在java中调整tiff图像的大小?,java,image,jai,Java,Image,Jai,我想调整.tiff文件的大小。我使用JAI工具包调整了不同类型图像的大小。以下是我尝试实施的内容: int imageWidth = 330; int imageHeight = 490; BufferedImage tempImage = new BufferedImage(imageWidth, imageHeight,BufferedImage.TYPE_INT_RGB); Graphics2D graphics2D = tempImage

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


提前感谢。

这是因为您正在写tempImage,它仍然是原始图像

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

graphics2D.drawImage(tempImage, 0, 0, imageWidth, imageHeight, null);
或者将其他代码更改为写出image而不是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)您正在将tempImage写入其内部:

graphics2D.drawImage(tempImage, 0, 0, imageWidth, imageHeight, null);
应该是:

graphics2D.drawImage(originalImage, 0, 0, imageWidth, imageHeight, null);
encoder.encode(tempImage);
2) 你正在写你刚刚读过的图像(顺便说一句,你为什么要读它?)

应该是:

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

根据建议进行更改,但仍然得到与原始图像具有相同尺寸的图像。抱歉,伙计,在我放弃之前,我还有一个建议:)它创建具有预期尺寸的图像,但整个图像为黑色。缺少或错误。图像现在没有预览。应用并测试了更改,但仍然得到黑色图像。