【问题标题】:What order does PixelGrabber put pixels into the array in java?PixelGrabber在java中将像素放入数组中的顺序是什么?
【发布时间】:2015-06-04 06:30:59
【问题描述】:

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

【问题讨论】:

  • 这是我目前得到的代码: public static int[] convertImgToPixels(Image img, int width, int height) { int[] pixel = new int[width * height]; PixelGrabber 像素 = new PixelGrabber(img, 0, 0, width, height, pixel, 0, width);尝试 {像素.grabPixels(); } catch (InterruptedException e) { throw new IllegalStateException("Interrupted Waiting for Pixels"); } if ((pixels.getStatus() & ImageObserver.ABORT) != 0) { throw new IllegalStateException("Image Fetch Aborted"); } 返回像素; }
  • 我正在尝试将像素从图像中取出并放入数组中

标签: java arrays image


【解决方案1】:

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] 表明它首先沿着行,然后沿着列。它首先沿宽度抓取像素。

【讨论】:

    【解决方案2】:

    我很确定它使用行主要顺序,但最简单的方法是实际抓取像素,将它们的序列设置为特定颜色(以便于识别),然后将它们保存到图像中。如果像素条出现垂直,则顺序为列主要,否则为行主要。您可以使用如下代码: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[] 的任何修改都将反映在图像中,反之亦然。这肯定是行专业。

    【讨论】:

      猜你喜欢
      • 2011-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-16
      • 1970-01-01
      • 1970-01-01
      • 2013-10-31
      • 1970-01-01
      相关资源
      最近更新 更多