【问题标题】:Buffered image pixel manipulation缓冲图像像素操作
【发布时间】:2011-10-12 15:22:45
【问题描述】:

我有这个代码:

public Image toNegative()
{
    int imageWidth =  originalImage.getWidth();
    int imageHeight = originalImage.getHeight();
    int [] rgb = null; // new int[imageWidth * imageWidth];
    originalImage.getRGB(0, 0, imageWidth, imageHeight, rgb, 0,imageWidth);

    for (int y = 0; y < imageHeight; y++)
    {
         for (int x = 0; x < imageWidth; x++)
         {
             int index = y * imageWidth + x;
             int R = (rgb[index] >> 16) & 0xff;     //bitwise shifting
             int G = (rgb[index] >> 8) & 0xff;
             int B = rgb[index] & 0xff;

             R = 255 - R;
             G = 255 - R;
             B = 255 - R;

             rgb[index] = 0xff000000 | (R << 16) | (G << 8) | B;                                
         }
    }


    return getImageFromArray(rgb, imageWidth, imageHeight);
}

当我在传递 getRGB 之前分配数组时,它会抛出 NPE 或使用数组或 ArrayOutOfBoundsException。我签入调试器,图像具有大小并已分配。

更新: getRGB

 /**
 * Returns an array of integer pixels in the default RGB color model
 * (TYPE_INT_ARGB) and default sRGB color space,
 * from a portion of the image data.  Color conversion takes
 * place if the default model does not match the image
 * <code>ColorModel</code>.  There are only 8-bits of precision for
 * each color component in the returned data when
 * using this method.  With a specified coordinate (x,&nbsp;y) in the
 * image, the ARGB pixel can be accessed in this way:
 * </p>
 *
 * <pre>
 *    pixel   = rgbArray[offset + (y-startY)*scansize + (x-startX)]; </pre>
 *
 * <p>
 *
 * An <code>ArrayOutOfBoundsException</code> may be thrown
 * if the region is not in bounds.
 * However, explicit bounds checking is not guaranteed.
 *
 * @param startX      the starting X coordinate
 * @param startY      the starting Y coordinate
 * @param w           width of region
 * @param h           height of region
 * @param rgbArray    if not <code>null</code>, the rgb pixels are
 *          written here
 * @param offset      offset into the <code>rgbArray</code>
 * @param scansize    scanline stride for the <code>rgbArray</code>
 * @return            array of RGB pixels.
 * @see #setRGB(int, int, int)
 * @see #setRGB(int, int, int, int, int[], int, int)
 */
public int[] getRGB(int startX, int startY, int w, int h,
                    int[] rgbArray, int offset, int scansize) {
    int yoff  = offset;
    int off;
    Object data;
    int nbands = raster.getNumBands();
    int dataType = raster.getDataBuffer().getDataType();
    switch (dataType) {
    case DataBuffer.TYPE_BYTE:
        data = new byte[nbands];
        break;
    case DataBuffer.TYPE_USHORT:
        data = new short[nbands];
        break;
    case DataBuffer.TYPE_INT:
        data = new int[nbands];
        break;
    case DataBuffer.TYPE_FLOAT:
        data = new float[nbands];
        break;
    case DataBuffer.TYPE_DOUBLE:
        data = new double[nbands];
        break;
    default:
        throw new IllegalArgumentException("Unknown data buffer type: "+
                                           dataType);
    }

    if (rgbArray == null) {
        rgbArray = new int[offset+h*scansize];
    }

    for (int y = startY; y < startY+h; y++, yoff+=scansize) {
        off = yoff;
        for (int x = startX; x < startX+w; x++) {
            rgbArray[off++] = colorModel.getRGB(raster.getDataElements(x,
                                                                    y,
                                                                    data));
        }
    }

    return rgbArray;
}

【问题讨论】:

    标签: java bufferedimage pixel-manipulation


    【解决方案1】:

    有两个问题:

    1. 数组的宽度不是图像的宽度,而是“扫描尺寸”(某些图像尺寸会被额外的像素填充)

    2. 如果您使用 null 数组调用 getRGB(),该方法将创建一个数组,但不会更改 rgb 引用 - Java 不支持“输出参数”。

    要完成这项工作,请使用

    rgb = originalImage.getRGB(0, 0, imageWidth, imageHeight, null, 0,imageWidth);
    

    【讨论】:

      【解决方案2】:

      您的代码将抛出一个NullPointerException,因为您从未为rgb 变量分配非空引用。因此,对它的引用(例如rgb[index])将产生异常。如果您希望将空数组传递给 getRGB,您需要确保分配方法返回的结果数组;例如

      int[] rgb = originalImage.getRGB(0, 0, imageWidth, imageHeight, rgb, 0,imageWidth);
      

      如果您取消注释已注释掉的代码,则会出现一个错误,即您将数组分配为imageWidth * imageWidth 而不是imageWidth * imageHeight,这就是您看到ArrayIndexOutOfBoundsException 的原因。

      【讨论】:

      • 不。 getRGB 内部是 if (rgbArray == null) { rgbArray = new int[offset+h*scansize]; } 至少 NetBeans 显示的内容
      • getRGB 可能会执行空检查(我还没有查看),但您仍然尝试在代码中进一步索引空数组,这就是您看到 NullPointerException 的原因。如果您希望传入一个空数组,您需要将 getRGB 的结果分配给您的 rgb 引用以避免 NPE(请参阅我的编辑)。
      • 啊它被扔到别的地方了
      猜你喜欢
      • 2014-02-23
      • 1970-01-01
      • 1970-01-01
      • 2013-04-14
      • 2013-02-25
      • 2016-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多