zoukankan      html  css  js  c++  java
  • Bitmap转灰度字节数组byte[]

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

    简单的位图转灰度数组就是:得到位图中的每一个像素点,然后依据像素点得到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);

                    }
                });

        }



  • 相关阅读:
    P1939 【模板】矩阵加速(数列)
    NUMBER BASE CONVERSION POJ
    Raid POJ
    Genius ACM HihoCoder
    BZOJ1500: [NOI2005]维修数列 Splay维护序列
    BZOJ3245: 最快路线 拆点dijkstra
    存个头
    895E
    894E
    tarjan缩点/求桥模板
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/6724206.html
Copyright © 2011-2022 走看看