PixelGrabber在java中将像素放入数组的顺序是什么?

时间:2015-03-31 02:53:59

标签: java arrays image

PixelGrabber在java中将像素放入数组的顺序是什么?是否首先沿图像宽度拍摄像素?或者首先沿着图像的高度?

public static int[] convertImgToPixels(Image img, int width, int height) {
    int[] pixel = new int[width * height]; 
    PixelGrabber pixels = new PixelGrabber(img, 0, 0, width, height, pixel, 0, width); 
    try { 
        pixels.grabPixels(); 
    } catch (InterruptedException e) { 
        throw new IllegalStateException("Interrupted Waiting for Pixels"); 
    } 
    if ((pixels.getStatus() & ImageObserver.ABORT) != 0) { 
        throw new IllegalStateException("Image Fetch Aborted"); 
    } 
    return pixel; 
}

2 个答案:

答案 0 :(得分:0)

我非常确定它使用行主要顺序,但最简单的方法是实际抓取像素,将它们的序列设置为特定颜色(以便于识别),然后将它们保存到图像中。如果像素条显示为垂直,则顺序为列主要,否则为行主要。您可以使用以下代码:here

public static Image getImageFromArray(int[] pixels, int width, int height) {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        WritableRaster raster = (WritableRaster) image.getData();
        raster.setPixels(0,0,width,height,pixels);
        return image;
    }

int[]转换为图片。

另外,我使用((DataBufferInt)img.grtRaster().getDataBuffer()).getData()快速抓取图像的像素。对int []的任何修改都将反映在图像中,反之亦然。这肯定是行专业。

答案 1 :(得分:0)

documentation

提供的代码示例中

它有以下for循环:

for (int j = 0; j < h; j++) {
    for (int i = 0; i < w; i++) {
        handlesinglepixel(x+i, y+j, pixels[j * w + i]);
    }
}

访问pixels[j * w + i]显示它首先沿着行,然后沿着列。它首先沿宽度抓取像素。