Android rgb 转bitmap

来源:互联网 发布:ica负熵的迭代算法 编辑:程序博客网 时间:2024/05/18 03:30

Android rgb转bitmap

        int width = 11;        int height = 8;        Integer r[][]={            {000,000,255,000,000,000,000,000,255,000,000},            {000,000,000,255,000,000,000,255,000,000,000},            {000,000,255,255,255,255,255,255,255,000,000},            {000,255,255,255,255,255,255,255,255,255,000},            {255,255,255,255,255,255,255,255,255,255,255},            {255,000,255,255,255,255,255,255,255,000,255},            {255,000,255,000,000,000,000,000,255,000,255},            {000,000,000,255,255,000,255,255,000,000,000}};        Integer g[][]={            {000,000,255,000,000,000,000,000,255,000,000},            {000,000,000,255,000,000,000,255,000,000,000},            {000,000,255,255,255,255,255,255,255,000,000},            {000,255,255,000,255,255,255,000,255,255,000},            {255,255,255,255,255,255,255,255,255,255,255},            {255,000,255,255,255,255,255,255,255,000,255},            {255,000,255,000,000,000,000,000,255,000,255},            {000,000,000,255,255,000,255,255,000,000,000}};        Integer b[][]={            {000,000,255,000,000,000,000,000,255,000,000},            {000,000,000,255,000,000,000,255,000,000,000},            {000,000,255,255,255,255,255,255,255,000,000},            {000,255,255,000,255,255,255,000,255,255,000},            {255,255,255,255,255,255,255,255,255,255,255},            {255,000,255,255,255,255,255,255,255,000,255},            {255,000,255,000,000,000,000,000,255,000,255},            {000,000,000,255,255,000,255,255,000,000,000}};//char* rgba = (char*)malloc(width*height*4);        int[] pixels=new int[width*height*4];        int offset=0;        int alpha = 0xFF << 24;        for(int i=0; i < height; ++i)        {            for (int j=0; j < width; j++)            {
//这个会放大四倍
  pixels[4*offset] = r[i][j]; pixels[4*offset+1] = g[i][j]; pixels[4*offset+2] = b[i][j].byteValue(); pixels[4*offset+3] = 0; offset ++;
//这个计算的不对 //             int   grey =(r[i][j] & 0x00FF0000)>> 16 + (g[i][j] & 0x0000FF00) >> 8 +b[i][j]& 0x000000FF;
// grey = alpha | (grey << 16) | (grey << 8) | grey;// pixels[width * i + j] = grey;
这个能出图               pixels[width * i + j] =  0xFF << 24 |  r[i][j] << 16|  g[i][j] << 8 | b[i][j];
  }
}
        //失败//        Bitmap bmp = Bitmap.createBitmap(pixels, 0, width, width, height,//                Bitmap.Config.ARGB_8888);//成功,但是图好像不对
sonarBitmap =new MyBitmap().createMyBitmap(pixels,width,height); 

//成功
sonarBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
sonarBitmap.setPixels(pixels, 0, width, 0, 0, width, height);
// sonarBitmap=result;// createBimap(result); sonarImage.setImageBitmap(sonarBitmap);

 public class MyBitmap{        public Bitmap createMyBitmap(int[] data, int width, int height){            int []colors = convertByteToColor(data);            if (colors == null){                return null;            }            Bitmap bmp = Bitmap.createBitmap(colors, 0, width, width, height,                    Bitmap.Config.ARGB_8888);            return bmp;        }        // 将一个byte数转成int// 实现这个函数的目的是为了将byte数当成无符号的变量去转化成int        public  int convertByteToInt(int data){            int heightBit = (int) ((data>>4) & 0x0F);            int lowBit = (int) (0x0F & data);            return heightBit * 16 + lowBit;        }        // 将纯RGB数据数组转化成int像素数组        public int[] convertByteToColor(int[] data){            int size = data.length;            if (size == 0){                return null;            }            int arg = 0;            if (size % 3 != 0){                arg = 1;            }            // 一般情况下data数组的长度应该是3的倍数,这里做个兼容,多余的RGB数据用黑色0XFF000000填充            int []color = new int[size / 3 + arg];            int red, green, blue;            if (arg == 0){                for(int i = 0; i < color.length; ++i){                    red = convertByteToInt(data[i * 3]);                    green = convertByteToInt(data[i * 3 + 1]);                    blue = convertByteToInt(data[i * 3 + 2]);                    // 获取RGB分量值通过按位或生成int的像素值                    color[i] = (red << 16) | (green << 8) | blue | 0xFF000000;                }            }else{                for(int i = 0; i < color.length - 1; ++i){                    red = convertByteToInt(data[i * 3]);                    green = convertByteToInt(data[i * 3 + 1]);                    blue = convertByteToInt(data[i * 3 + 2]);                    color[i] = (red << 16) | (green << 8) | blue | 0xFF000000;                }                color[color.length - 1] = 0xFF000000;            }            return color;        }    }


原创粉丝点击