【问题标题】:coordinates out of bound:bufferedimage坐标越界:bufferedimage
【发布时间】:2014-09-01 17:01:47
【问题描述】:

我编写了一个程序来分离图像的红蓝和绿色分量,但下面的代码给出了错误:

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

【问题讨论】:

    标签: java rgb bufferedimage


    【解决方案1】:

    方法是getRGB(x,y),这意味着你的外环应该是宽度,内环应该是高度。改为

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

    原因
    您正试图获得一个不存在的坐标。这是因为

    1. 您的循环应该从 0 开始,到 getWidth()-1/getHeight()-1 结束,因为最后一个坐标是 (WIDTH-1,HEIGHT-1)
    2. 您的外部循环获取 y 值,而内部循环获取 x 值,因此您尝试获取 (y,x) 值,而它应该是 (x,y) 值。如果它是方形图片,这不会导致任何问题,但如果它是矩形,则会导致异常,就像您的情况一样。

    所以按照我在代码中的建议进行更改,它应该可以工作。

    【讨论】:

      【解决方案2】:

      你得到一个 ArrayIndexOutOfBoundsException 因为你的 for 循环关闭了一个。像素索引从 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);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-12
        • 1970-01-01
        相关资源
        最近更新 更多