直接上工具类,简单易懂
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);
}
}