简体   繁体   中英

coordinates out of bound:bufferedimage

I made a program to separate red blue and green components of a image but the code below gives an error:

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):

Here is the source code:

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);
    }

The method is getRGB(x,y) meaning your outer-loop should be for width and inner-loop for height. Change to

for (int i = 0; i < img.getWidth(); i++) {
        for (int j = 0; j < img.getHeight(); j++) {

REASON
You are trying to get a co-ordinate that does not exist. This is because

  1. Your loop should start from 0 and end to getWidth()-1/getHeight()-1 as the last co-ordinate is (WIDTH-1,HEIGHT-1)
  2. You outer-loop is getting y value while inner one is getting x value, so you are trying to get a (y,x) value while it should be (x,y) value. This won't cause any problems if it's a square picture but if it is rectangular it will cause exception as it happened in your case.

So make the change as I suggested in code and it should work.

You are getting an ArrayIndexOutOfBoundsException because your for loops are off by one. The pixel indices start at 0 (not 1) and run to getWidth()-1 and getHeight()-1.

The second problem is that you are swapping the arguments when you call getRGB(). The signature of getRGB is getRGB( int x, int y ), but you are calling getRGB( y, x ).

You are looping through the image correctly though (outer loop iterates over rows, inner loop over columns). Don't swap the loops as proposed by other answers, but do swap the order of the arguments provided to getRGB.

Here is the corrected code, with i and j renamed to row and col to help clarify:

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM