gpt4 book ai didi

java - BufferedImage.getRGB 不返回数组

转载 作者:行者123 更新时间:2023-12-01 12:38:34 25 4
gpt4 key购买 nike

我使用方法 BufferedImage.getRGB(...)BufferedImage 获取 int 数组,但是当我尝试访问数组中的那些 int 时,我收到 ArrayIndexOutOfBounds 异常。我想要存储这些值的 int 数组的长度似乎为 0,即使看起来图像已正确加载。我哪里做错了?

代码如下:

import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;

public class SpriteSheet {

public String path;
public int width, height;
public int[] pixels;

public SpriteSheet(String path){
BufferedImage image = null;
try {
image = ImageIO.read(SpriteSheet.class.getResourceAsStream(path));
} catch (IOException e) {
e.printStackTrace();
}

if (image == null){
return;
}


this.path = path;
this.width = image.getWidth();
this.width = image.getHeight();

pixels = image.getRGB(0, 0, width, height, pixels, 0, width);

for (int i = 0; i < pixels.length; i++){
pixels[i] = (pixels[i] & 0xff)/64; // remove alpha channel
}

for (int i = 0; i < 8; i++){
System.out.println(pixels[i]);
}
}

}

当我尝试显示 int 的值时,ArrayIndexOutOfBounds 在最后一个 for 循环中抛出。

最佳答案

改变

this.width = image.getWidth();
this.width = image.getHeight();

this.width = image.getWidth();
this.height = image.getHeight(); // set height properly
<小时/>

改变

for (int i = 0; i < 8; i++) {

for (int i = 0; i < pixels.length; i++) {

关于java - BufferedImage.getRGB 不返回数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25340391/

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