3

我有一个尺寸为 16x6080 的图像。这是一个堆叠的图像,其中包含 16x16 部分的国家标志。我的目标是仅从该图像中提取特定国家/地区的标志并将其保存为自己的文件。这是我当前的代码

            //Original Image
        BufferedImage image = ImageIO.read(new File(countryImageNamePath));

        System.out.println("Original Image Dimension: "+image.getWidth()+"x"+image.getHeight());

        //Get the cropped image
        BufferedImage out = image.getSubimage(0, 1808, 16, 16);

        //Create a file to stream the out buffered image to
        File croppedFile = new File(countryImagePath + countryName + ".gif");

        //Write the cropped file
        ImageIO.write(out, "gif", croppedFile);

产生的输出是

Original Image Dimension: 16x6080
Write File : C:\Applications\WorldCoinParser\images\country\US.gif

我为 Y 坐标输入什么值并不重要,我总是得到图像的顶部,从 x=0 和 y=0 开始,宽度和高度为 16。

有谁看到我在哪里搞砸了?

谢谢!

4

1 回答 1

0

正如@MattPerry 所说,我们只需升级到 Java 7 就可以解决这个问题。

仅出于文档目的,该错误似乎是这个http://bugs.sun.com/view_bug.do?bug_id=6795544并影响了 Java 6。

The reason of problem here is that the optimized writing loop (utilized 
 direct access to image data buffer) does not take into account a data 
 band offset (which is non-trivial for sub-images, for example). It results 
 in writing image data starting from top left corner of the parent image 
 instead of expected top left corner of the sub-image.

 We  should take into account data bands offset, calculated by translated
 raster instance.

我可以使用 JDK5 重现这个错误

它适用于JDK7

于 2014-01-18T01:08:29.143 回答