使用java image.getSubimage裁剪图像的问题

时间:2013-04-06 17:42:50

标签: java image crop

我的图片尺寸为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.

有谁看到我搞砸了?

谢谢!

1 个答案:

答案 0 :(得分: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