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

标签 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。

有人看到我哪里搞砸了吗?

谢谢!

最佳答案

正如@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 一起工作

关于java - 使用 java image.getSubimage 裁剪图像的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15854069/

相关文章:

android - 上传前从相机裁剪图像(Phonegap)

iphone - 动态裁剪风景图像

jquery - 使用 jQuery 上传和裁剪个人资料图片

java - 无法让java使用多个核心

java - HashSet 相对于 ArrayList 的优势,反之亦然

java - Firebase Android : how do I access params nested in data via RemoteMessage?

ios - 将带有 exif 的图像转换为 NSData 类型

.net - 有可靠的.NET 图像元数据库吗?

java - Spring Boot集成测试模拟方法返回null

java - 在图像中寻找模式