我想把一张地图(,用photoshop画出来的,jpg格式,里面就两种颜色,障碍物为蓝色,没有障碍物地区为白色,700*700pixel),如何把每个像素的图象数据提取出来,保存在myImage[700][700]二维数组中,这样的话我估计里面就只有两种数值,因为只有两种颜色么.最后再把这副图片通过保存的数组画出来.
为什么我用
img_full_grabber = new PixelGrabber(img_full, 0, 0, img_width,img_height, img_full_data, 0, img_width);// 准备获取图像数据
try {
img_full_grabber.grabPixels();
}// 采集数据 catch (InterruptedException e) {
}
1\我把img_full_data[]数组打印出来,竟然是这种数据[[I@10b9d04,这是什么啊?为什么不是0~255的值呢?
如何能取得0~255的值呢?
2\后来我加了这些代码:
ColorModel colorModel = ColorModel.getRGBdefault();
red[i][j]=colorModel.getRed(img_full_data[i*window_width+j]);
打印出来看看,也是类似[[I@10b9d04这样的值.
3\还有为什么不把img_full_data保存成二维数组啊?

解决方案 »

  1.   

    BufferedImage img = ImageIO.read(file);
    pixel[x][y] = img.getRGB(x,y);
      

  2.   

    这里面的东西比较复杂。
    首先,执行grabPixels()之后,myImage中每个元素里的不是颜色,是像素。所以,简单的打印不会得到颜色值。
    再说像素,像素的组成方式有很多种,从PixelGrabber的Specification看,它使用ARGB的方式组成像素。在这里,每个像素是Integer,用其中的每8位保存一种分量,从高到低依次为:
    alpha(主成分,用于透明)
    red
    green
    blue其中,得到Red,Green,和Blue分量之后,使用Color.RGB(red, green, blue);应该可以得到颜色。下面是API Specification中给出的例子。 public void handlesinglepixel(int x, int y, int pixel) {
            int alpha = (pixel >> 24) & 0xff; // 得到位数:24 ~ 31
            int red   = (pixel >> 16) & 0xff; // 得到位数:16 ~ 23
            int green = (pixel >>  8) & 0xff; // 得到位数: 8 ~ 15
            int blue  = (pixel      ) & 0xff; // 得到位数: 0 ~  7
            // Deal with the pixel as necessary...
     } public void handlepixels(Image img, int x, int y, int w, int h) {
            int[] pixels = new int[w * h];
            PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);
            try {
                pg.grabPixels();
            } catch (InterruptedException e) {
                System.err.println("interrupted waiting for pixels!");
                return;
            }
            if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
                System.err.println("image fetch aborted or errored");
                return;
            }
            for (int j = 0; j < h; j++) {
                for (int i = 0; i < w; i++) {
                    handlesinglepixel(x+i, y+j, pixels[j * w + i]);
                }
            }
     }
      

  3.   

    这几天翻译了几篇文章,可能对你的问题有帮助。Programmer's Guide to the JavaTM 2D API -- Chapter 5 Imaging  <http://blog.csdn.net/unagain/archive/2006/05/04/707566.aspx>
    Programmer's Guide to the JavaTM 2D API -- Chapter 6 Color <http://blog.csdn.net/unagain/archive/2006/05/05/708656.aspx>LookupTable (Java 2 Platform SE 5.0) <http://blog.csdn.net/unagain/archive/2006/05/04/707561.aspx>
    ComponentSampleModel (Java 2 Platform SE 5.0) <http://blog.csdn.net/unagain/archive/2006/05/04/707558.aspx>
    BandCombineOp (Java 2 Platform SE 5.0) <http://blog.csdn.net/unagain/archive/2006/05/04/707557.aspx>