美文网首页
RGB数组中扣图

RGB数组中扣图

作者: Jowney | 来源:发表于2018-10-24 15:20 被阅读3次
     /**
         * 
         * @param src RGB数据源
         * @param des RGB裁剪后目标源
         * @param left  裁剪左边距
         * @param right 裁剪右边距
         * @param top 裁剪上边距
         * @param bottom 裁剪下边距
         */
    
        private void getMaxFaceBGR(byte[] src ,byte[] des,int left,int right,int top,int bottom) {
            int m = 0;
    
            for (int i = top; i <= bottom; i++) {
                for (int j = left; j <= right; j++) {
                    int k = (i * 480 + j) * 3;
                    des[3 * m] = src[k];   //R
    
                    des[3 * m + 1] = src[k + 1];   //G
    
                    des[3 * m + 2] =   src[k + 2];//B
    
                    m++;
    
                }
            }
    
        }
    
    
    
        static public Bitmap rgb2Bitmap(byte[] data, int width, int height) {
            int[] colors = convertByteToIntArray(data);    //取RGB值转换为int数组
            if (colors == null) {
                return null;
            }
            return  Bitmap.createBitmap(colors, 0, width, width, height, Bitmap.Config.ARGB_8888);
        }
    
        // 将一个byte数转成int
        // 实现这个函数的目的是为了将byte数当成无符号的变量去转化成int
        public static int convertByteToInt(byte data) {
    
            int heightBit = (int) ((data >> 4) & 0x0F);
            int lowBit = (int) (0x0F & data);
            return heightBit * 16 + lowBit;
        }
    
    
        // 将纯RGB数据数组转化成int像素数组
        public static int[] convertByteToIntArray(byte[] data) {
            int size = data.length;
            if (size == 0) return null;
            
            int arg = 0;
            if (size % 3 != 0) {
                arg = 1;
            }
            // 一般RGB字节数组的长度应该是3的倍数,
            // 不排除有特殊情况,多余的RGB数据用黑色0XFF000000填充
            int[] color = new int[size / 3 + arg];
            int r, g, b;
            int colorLen = color.length;
            if (arg == 0) {
                for (int i = 0; i < colorLen; ++i) {
                    r = convertByteToInt(data[i * 3]);
                    g = convertByteToInt(data[i * 3 + 1]);
                    b = convertByteToInt(data[i * 3 + 2]);
                    // 获取RGB分量值通过按位或生成int的像素值
                    color[i] = (r << 16) | (g << 8) | b | 0xFF000000;
                }
            } else {
                for (int i = 0; i < colorLen - 1; ++i) {
                    r = convertByteToInt(data[i * 3]);
                    g = convertByteToInt(data[i * 3 + 1]);
                    b = convertByteToInt(data[i * 3 + 2]);
                    color[i] = (r << 16) | (g << 8) | b | 0xFF000000;
                }
    
                color[colorLen - 1] = 0xFF000000;
            }
    
            return color;
        }
    
    

    相关文章

      网友评论

          本文标题:RGB数组中扣图

          本文链接:https://www.haomeiwen.com/subject/mlwttqtx.html