使用Java中的image.getSubimage裁剪图像时遇到的问题

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像素。
有没有人看出我的错误所在呢?
谢谢!

我没有看到你的代码片段中有任何明显的错误。考虑创建并发布一个sscce,使用一个所有人都可以访问的在线图像。 - Hovercraft Full Of Eels
1
结果证明这是Java 6(及更低版本)中的一个错误,试图解析gif文件。升级到Java7解决了这个问题。 - Matt Perry
2
哇塞,真的吗?谢谢您回复我们。考虑将此作为您问题的答案发布。 - Hovercraft Full Of Eels
因为我是一个新手,所以我必须等待8个小时才能发布答案。我会在今天稍后更新答案。 - Matt Perry
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中可以工作


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接