gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 00:33:03 25 4
gpt4 key购买 nike

我有一张尺寸为 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com