工作中遇到图片转灰度数组的须要,经过研究和大神的指导。终于得到例如以下两个方法。能够实现位图转灰度数组

简单的位图转灰度数组就是:得到位图中的每一个像素点,然后依据像素点得到RGB值,最后对RGB值,依据灰度算法得到灰度值就可以


/*如一张480*800的图片,终于得到一个byte[480*800/2]的灰度数组,由于函数把每两个相邻高的像素灰度转化为一个灰度*/

private byte[] java_convertIMG2GreyArray(Bitmap img) {

        byte[] theBytes = null;
        /* 得到位图的宽高 */
        int width = img.getWidth();
        int height = img.getHeight();
        /* 取得位图的像素点 */
        int[] pixels = new int[width * height];
        img.getPixels(pixels, 0, width, 0, 0, width, height);
        /* 定义结果数据数组 */
        theBytes = new byte[width * height / 2];

        /*定义循环中用到的变量,节约内存和时间*/
        int x, y, k;
        int pixel, r, g, b;
        for (y = 0; y < height; y++) {
            for (x = 0, k = 0; x < width; x++) {
                //依次取得像素点
                pixel = pixels[y * width + x];
                //得到rgb
                r = (pixel >> 16) & 0xFF;
                g = (pixel >> 8) & 0xFF;
                b = pixel & 0xFF;
                /*每两行存为一行*/
                if (x % 2 == 1) {
                    theBytes[k + y * width / 2] = (byte) (theBytes[k + y
                            * width / 2] | ((r * 299 + g * 587 + b * 114 + 500) / 1000) & 0xf0);
                    k++;
                } else {
                    theBytes[k + y * width / 2] = (byte) (theBytes[k + y
                            * width / 2] | (((r * 299 + g * 587 + b * 114 + 500) / 1000) >> 4) & 0x0f);
                }
            }
        }
        return theBytes;

    }


/*灰度依次转换 如:480 * 800最后得到一个byte[480*800]的灰度数组*/

private void java_convertIMGtoGreyArray(Bitmap img) {

        boolean usedebug = true;

        if (debugImage == null || debugUihandler == null)
            usedebug = false;

        int width = img.getWidth(); // 获取位图的宽
        int height = img.getHeight(); // 获取位图的高

        theBytes = null;

        theBytes = new byte[width * height];

        int[] pixels = new int[width * height]; // 通过位图的大小创建像素点数组

        img.getPixels(pixels, 0, width, 0, 0, width, height);

        for (int i = 0; i < height; i++) {

            for (int j = 0; j < width; j++) {

                int colorAtPixel = pixels[width * i + j];

                int alpha = (colorAtPixel >> 24) & 0xFF;
                int red = (colorAtPixel >> 16) & 0xFF;
                int green = (colorAtPixel >> 8) & 0xFF;
                int blue = colorAtPixel & 0xFF;

                theBytes[width * i + j] = (byte) ((red + green + blue) / 3);

                int theb = (0xff & theBytes[width * i + j]);

                pixels[width * i + j] = alpha << 24 | theb << 16 | theb << 8
                        | theb;

            }
        }

        bmo = img;

        bmo.setPixels(pixels, 0, width, 0, 0, width, height);

        pixels = null;

        if (debugUihandler != null)
            debugUihandler.post(new Runnable() {

                @Override
                public void run() {

                    if (debugImage != null && bmo != null)
                        debugImage.setImageBitmap(bmo);

                }
            });

    }