Java 坐标越界:BuffereImage
我制作了一个程序来分离图像的红、蓝和绿色分量,但下面的代码给出了一个错误:Java 坐标越界:BuffereImage,java,rgb,bufferedimage,Java,Rgb,Bufferedimage,我制作了一个程序来分离图像的红、蓝和绿色分量,但下面的代码给出了一个错误: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds! at sun.awt.image.ByteInterleavedRaster.getDataElements(ByteInterleavedRaster.java:318) at java.awt.image.BufferedImag
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
at sun.awt.image.ByteInterleavedRaster.getDataElements(ByteInterleavedRaster.java:318)
at java.awt.image.BufferedImage.getRGB(BufferedImage.java:888)
at rgb.Rgb.main(Rgb.java:46):
以下是源代码:
public static void main(String[] args) {
String type = "jpg";
BufferedImage img = null;
try {
img = ImageIO.read(new File("d:\\a.jpg"));
System.out.println(img.getWidth() + " " + img.getHeight());
} catch (IOException ex) {
Logger.getLogger(Rgb.class.getName()).log(Level.SEVERE, null, ex);
}
BufferedImage rp, gp, bp;
rp = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
bp = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
gp = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
for (int i = 1; i <= img.getHeight(); i++) {
for (int j = 1; j <= img.getWidth(); j++) {
int pixel = img.getRGB(i, j);
int alpha = pixel & 0xff000000;
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel) & 0xff;
rp.setRGB(i, j, alpha | (red << 16));
gp.setRGB(i, j, alpha | (green << 8));
bp.setRGB(i, j, alpha | blue);
}
}
try {
ImageIO.write(rp, type, new File("d:\\red.jpg"));
ImageIO.write(gp, type, new File("d:\\green.jpg"));
ImageIO.write(bp, type, new File("d:\\blue.jpg"));
} catch (IOException ex) {
Logger.getLogger(Rgb.class.getName()).log(Level.SEVERE, null, ex);
}
publicstaticvoidmain(字符串[]args){
字符串类型=“jpg”;
BuffereImage img=null;
试一试{
img=ImageIO.read(新文件(“d:\\a.jpg”);
System.out.println(img.getWidth()+“”+img.getHeight());
}捕获(IOEX异常){
Logger.getLogger(Rgb.class.getName()).log(Level.SEVERE,null,ex);
}
缓冲图像rp、gp、bp;
rp=新的BuffereImage(img.getWidth()、img.getHeight()、BuffereImage.TYPE\u INT\u ARGB);
bp=新的BuffereImage(img.getWidth()、img.getHeight()、BuffereImage.TYPE\u INT\u ARGB);
gp=新的BuffereImage(img.getWidth()、img.getHeight()、BuffereImage.TYPE\u INT\u ARGB);
对于(inti=1;i16)&0xff;
绿色整数=(像素>>8)&0xff;
蓝色整数=(像素)&0xff;
rp.setRGB(i,j,alpha |)(红色方法是getRGB(x,y)
这意味着你的外环应该是宽度,内环应该是高度。更改为
for (int i = 0; i < img.getWidth(); i++) {
for (int j = 0; j < img.getHeight(); j++) {
for(int i=0;i
原因
您试图获得一个不存在的坐标。这是因为
循环应该从0开始,结束于getWidth()-1/getHeight()-1,因为最后一个坐标是(WIDTH-1,HEIGHT-1)
您的外部循环得到y值,而内部循环得到x值,因此您试图得到一个(y,x)值,而它应该是(x,y)值。如果它是正方形图片,这不会导致任何问题,但如果它是矩形图片,它将导致异常,就像您的情况一样。
因此,按照我在代码中的建议进行更改,它应该会起作用。方法是getRGB(x,y)
这意味着您的外环应该是宽度,内环应该是高度。更改为
for (int i = 0; i < img.getWidth(); i++) {
for (int j = 0; j < img.getHeight(); j++) {
for(int i=0;i
原因
您试图获得一个不存在的坐标。这是因为
循环应该从0开始,结束于getWidth()-1/getHeight()-1,因为最后一个坐标是(WIDTH-1,HEIGHT-1)
您的外部循环得到y值,而内部循环得到x值,因此您试图得到一个(y,x)值,而它应该是(x,y)值。如果它是正方形图片,这不会导致任何问题,但如果它是矩形图片,它将导致异常,就像您的情况一样。
因此,按照我在代码中的建议进行更改,它应该会起作用。您将获得ArrayIndexOutOfBoundsException,因为for循环关闭了1。像素索引从0(不是1)开始,运行到getWidth()-1和getHeight()-1
第二个问题是,在调用getRGB()时交换参数。getRGB的签名是getRGB(int x,int y),但您正在调用getRGB(y,x)
但是,您正在正确地循环图像(外部循环在行上迭代,内部循环在列上迭代)。不要像其他答案所建议的那样交换循环,而是交换提供给getRGB的参数的顺序
以下是更正后的代码,i和j重命名为row和col以帮助澄清:
for (int row = 0; row < img.getHeight(); row++)
{
for (int col = 0; col < img.getWidth(); col++)
{
int pixel = img.getRGB(col, row); // getRGB(x,y)
int alpha = pixel & 0xff000000;
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel) & 0xff;
rp.setRGB(col, row, alpha | (red << 16));
gp.setRGB(col, row, alpha | (green << 8));
bp.setRGB(col, row, alpha | blue);
}
}
for(int row=0;row>16)和0xff;
绿色整数=(像素>>8)&0xff;
蓝色整数=(像素)&0xff;
rp.setRGB(col,row,alpha |)(红色您将获得ArrayIndexOutOfBoundsException,因为for循环关闭了1。像素索引从0(不是1)开始,运行到getWidth()-1和getHeight()-1
第二个问题是,在调用getRGB()时交换参数。getRGB的签名是getRGB(int x,int y),但您正在调用getRGB(y,x)
但是,您正在正确地循环图像(外部循环在行上迭代,内部循环在列上迭代)。不要像其他答案所建议的那样交换循环,而是交换提供给getRGB的参数的顺序
以下是更正后的代码,i和j重命名为row和col以帮助澄清:
for (int row = 0; row < img.getHeight(); row++)
{
for (int col = 0; col < img.getWidth(); col++)
{
int pixel = img.getRGB(col, row); // getRGB(x,y)
int alpha = pixel & 0xff000000;
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel) & 0xff;
rp.setRGB(col, row, alpha | (red << 16));
gp.setRGB(col, row, alpha | (green << 8));
bp.setRGB(col, row, alpha | blue);
}
}
for(int row=0;row>16)和0xff;
绿色整数=(像素>>8)&0xff;
蓝色整数=(像素)&0xff;
rp.setRGB(列,行,α|)(红色