android bitmap 转 灰度 byte[] 数组

mbitmap = BitmapFactory.decodeStream(getClass().getResourceAsStream("/res/drawable/testyuv2.bmp"));
int width = mbitmap.getWidth();            //获取位图的宽
int height = mbitmap.getHeight();        //获取位图的高
datas = new int[width * height];    //通过位图的大小创建像素点数组
mbitmap.getPixels(datas, 0, width, 0, 0, width, height);
int alpha = 0xFF << 24;
for (int i = 0; i < height; i++) {
    for (int j = 0; j < width; j++) {
        int grey = datas[width * i + j];

        int red = ((grey & 0x00FF0000) >> 16);
        int green = ((grey & 0x0000FF00) >> 8);
        int blue = (grey & 0x000000FF);

        grey = (int) ((float) red * 0.3 + (float) green * 0.59 + (float) blue * 0.11);
        grey = alpha | (grey << 16) | (grey << 8) | grey;
        datas[width * i + j] = grey;
    }
}

猜你喜欢

转载自blog.csdn.net/gaoxiaochuan89/article/details/81435920
今日推荐