Bimap getPixels方法研究

Android bitmap getPixels有俩个方法,这个俩个方法都是获取图片的pixel的。

1、getPixel(int x, int y)

2、getPixels(int[] pixels, int offset, int stride, int x, int y, int width, int height)

先来看第1个方法:

    /**
     * Returns the {@link Color} at the specified location. Throws an exception
     * if x or y are out of bounds (negative or >= to the width or height
     * respectively). The returned color is a non-premultiplied ARGB value.
     *
     * @param x    The x coordinate (0...width-1) of the pixel to return
     * @param y    The y coordinate (0...height-1) of the pixel to return
     * @return     The argb {@link Color} at the specified coordinate
     * @throws IllegalArgumentException if x, y exceed the bitmap's bounds
     */
    public int getPixel(int x, int y) {
        checkRecycled("Can't call getPixel() on a recycled bitmap");
        checkPixelAccess(x, y);
        return nativeGetPixel(mNativeBitmap, x, y);
    }
从方法执行上看,先判断这个bitmap是否被recycle掉。 

接着 checkPixelAccess(x, y);来检查x,y的值是否合法。0 < x < bitmap.getWidth(); y类似

然后就是native方法。

这个方法的作用就是:获取指定bitmap上x,y坐标上的点的像素。

接着看第2个方法:

     * Returns in pixels[] a copy of the data in the bitmap. Each value is
     * a packed int representing a {@link Color}. The stride parameter allows
     * the caller to allow for gaps in the returned pixels array between
     * rows. For normal packed results, just pass width for the stride value.
     * The returned colors are non-premultiplied ARGB values.
     *
     * @param pixels   The array to receive the bitmap's colors
     * @param offset   The first index to write into pixels[]
     * @param stride   The number of entries in pixels[] to skip between
     *                 rows (must be >= bitmap's width). Can be negative.
     * @param x        The x coordinate of the first pixel to read from
     *                 the bitmap
     * @param y        The y coordinate of the first pixel to read from
     *                 the bitmap
     * @param width    The number of pixels to read from each row
     * @param height   The number of rows to read
     *
     * @throws IllegalArgumentException if x, y, width, height exceed the
     *         bounds of the bitmap, or if abs(stride) < width.
     * @throws ArrayIndexOutOfBoundsException if the pixels array is too small
     *         to receive the specified number of pixels.
     */
    public void getPixels(int[] pixels, int offset, int stride,
                          int x, int y, int width, int height) {
        checkRecycled("Can't call getPixels() on a recycled bitmap");
        if (width == 0 || height == 0) {
            return; // nothing to do
        }
        checkPixelsAccess(x, y, width, height, offset, stride, pixels);
        nativeGetPixels(mNativeBitmap, pixels, offset, stride,
                        x, y, width, height);
    }
与上面相同的方法就不再提了。

看接下来执行 checkPixelsAccess(x, y, width, height, offset, stride, pixels);方法:

首先x,y满足 大于0,小于bitmap.getWidth() 和getHeight(),x + width还需要小于bitmap.width(),y类同。否则会抛出IllegalArgumentException异常

offset参数不可为负,offset+width不能大于pixels[]数组的长度,

stride参数可正可负,但需要 小于传入的参数width,注意是width,不是bitmap.getWidth()

最终该方法将结果写入到pixels数组。

再来说说几个参数具体含义:

pixels      获取位图颜色值的数组 
offset       写入到pixels[]中的第一个像素索引值 
stride       pixels[]中的行间距个数值(必须大于等于位图宽度)。可以为负数 
x           从位图中读取的第一个像素的x坐标值。 
y              从位图中读取的第一个像素的y坐标值 
width       从每一行中读取的像素宽度 
height     读取的行数 

stride = getWidth + padding 这个getWidth是图片的width,padding里面是附加信息。

你可能感兴趣的:(android,bitmap)