Android bitmap 转RGB数组(三通道)、RGB数组(单、三通道)转回Bitmap、bitmap Resize 512

直接上工具类,简单易懂



import android.graphics.Bitmap;
import android.graphics.Matrix;


/**
 * bitmap and rgb bytes dual transfer
 *
 * @author guochao
 * @version 1.0
 * @since 2019/12/12
 */
public class BitmapAndRgbByteUtil {

    private static final String TAG = BitmapAndRgbByteUtil.class.getSimpleName();
    public static final int RGB_DATA_WIDTH_OR_HEIGHT = 512;
    private static int mOriginWidth;
    private static int mOriginHeight;

    /**
     * 单通道数组转bitmap
     */
    public static Bitmap rgb2BitmapFor123(byte[] data, int width, int height) {
        long startTime = System.currentTimeMillis();
        try {

            int[] colors = convertByteToColor123(data);//取RGB值转换为int数组
            if (colors == null) {
                return null;
            }
            return Bitmap.createBitmap(colors, 0, width, width, height,
                    Bitmap.Config.ARGB_8888);

        } catch (Exception e) {
          
        } finally {
          
        }
        return null;
    }


    /**
     * 三通道数组转3通道bitmap
     */
    public static Bitmap rgb2BitmapFor323(byte[] data, int width, int height) {
        long startTime = System.currentTimeMillis();
        try {

            int[] colors = convertByteToColor323(data);//取RGB值转换为int数组
            if (colors == null) {
                return null;
            }
            return Bitmap.createBitmap(colors, 0, width, width, height,
                    Bitmap.Config.ARGB_8888);

        } catch (Exception e) {
            
        } finally {
           
        }
        return null;
    }

    /**
     * 将一个byte数转成int
     * 实现这个函数的目的是为了将byte数当成无符号的变量去转化成int
     *
     * @param data byte字节
     */
    private static int convertByteToInt(byte data) {
        int heightBit = (int) ((data >> 4) & 0x0F);
        int lowBit = (int) (0x0F & data);
        return heightBit * 16 + lowBit;
    }


    /**
     * 将纯RGB数据数组转化成int像素数组
     *
     * @param data rgb数组 输入为单通道,输出为3通道
     */
    private static int[] convertByteToColor123(byte[] data) {
        int size = -1;
        if (data != null) {
            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 + arg];
        int red, green, blue;
        int colorLen = color.length;
        if (arg == 0) {
            for (int i = 0; i < colorLen; ++i) {
                red = convertByteToInt(data[i]);
                green = convertByteToInt(data[i]);
                blue = convertByteToInt(data[i]);
                // 获取RGB分量值通过按位或生成int的像素值
                color[i] = (red << 16) | (green << 8) | blue | 0xFF000000;
            }
        } else {
            for (int i = 0; i < colorLen - 1; ++i) {
                red = convertByteToInt(data[i]);
                green = convertByteToInt(data[i]);
                blue = convertByteToInt(data[i]);
                color[i] = (red << 16) | (green << 8) | blue | 0xFF000000;
            }
            color[colorLen - 1] = 0xFF000000;
        }
        return color;
    }


    /**
     * 将纯RGB数据数组转化成int像素数组,转三通道
     *
     * @param data rgb数组 输入为三通道
     */
    private static int[] convertByteToColor323(byte[] data) {
        int size = -1;
        if (data != null) {
            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 red, green, blue;
        int colorLen = color.length;
        if (arg == 0) {
            for (int i = 0; i < colorLen; ++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 < colorLen - 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[colorLen - 1] = 0xFF000000;
        }
        return color;
    }


    /**
     * 将bitmap 转换为RGB数组(三通道)
     * @param bitmap
     * @return
     */
    public static byte[] bitmap2RGBData(Bitmap bitmap) {

        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int[] intValues = new int[width * height];
        bitmap.getPixels(intValues, 0, width, 0, 0, width,
                height);
        byte[] rgb = new byte[width * height * 3];
        for (int i = 0; i < intValues.length; ++i) {
            final int val = intValues[i];
            rgb[i * 3] = (byte) ((val >> 16) & 0xFF);//R
            rgb[i * 3 + 1] = (byte) ((val >> 8) & 0xFF);//G
            rgb[i * 3 + 2] = (byte) (val & 0xFF);//B
        }
        return rgb;
    }


    public static Bitmap resizeTo512(Bitmap bm) {
        mOriginWidth = bm.getWidth();
        mOriginHeight = bm.getHeight();
        AieLogger.d(TAG,
                "resizeTo512 origin width: " + mOriginWidth + ", height:" + mOriginHeight);
        // 设置想要的大小
        int newWidth = RGB_DATA_WIDTH_OR_HEIGHT;
        int newHeight = RGB_DATA_WIDTH_OR_HEIGHT;
        // 计算缩放比例
        float scaleWidth = ((float) newWidth) / mOriginWidth;
        float scaleHeight = ((float) newHeight) / mOriginHeight;
        // 取得想要缩放的matrix参数
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        // 得到新的图片
        return Bitmap.createBitmap(bm, 0, 0, mOriginWidth, mOriginHeight, matrix, true);
    }

    public static Bitmap resize512ToOrigin(Bitmap bm) {

        // 获取之前图片的原始比例
        int newWidth = mOriginWidth;
        int newHeight = mOriginHeight;
        // 计算缩放比例
        float scaleWidth = ((float) newWidth) / RGB_DATA_WIDTH_OR_HEIGHT;
        float scaleHeight = ((float) newHeight) / RGB_DATA_WIDTH_OR_HEIGHT;
        // 取得想要缩放的matrix参数
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        // 得到新的图片
        return Bitmap.createBitmap(bm, 0, 0, RGB_DATA_WIDTH_OR_HEIGHT,
                RGB_DATA_WIDTH_OR_HEIGHT, matrix, true);
    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值