一、核心java代码,主要用到Bitmap.createBitmap、setPixels等函数
public Bitmap bitmap;
bitmap = Bitmap.createBitmap(w[0], h[0], Bitmap.Config.ARGB_8888);
int[] irArgbData = getIrARGB(image_buf, w[0], h[0]);
bitmap.setPixels(irArgbData, 0, w[0], 0, 0, w[0], h[0]);
public ImageView rgbImageView;
rgbImageView.setImageBitmap(bitmap);
// add by 16位无符号灰度图数据,转化成argb数据
public static int[] getIrARGB(byte[] irBytes, int irWidth, int irHeight) {
int[] argbData = new int[irWidth * irHeight];
for (int i = 0; i < irWidth * irHeight; i++) {
byte[] shortBytes = new byte[]{irBytes[i * 2], irBytes[i * 2 + 1]};
// 0-65535的灰度数据
int shortData = getShort(shortBytes, true);
// 0-255范围的灰度数据
// int grayData = ((shortData * 255) / 65535) & 0x000000ff;
int grayData = (shortData >> 2) & 0x000000ff;
// R G B A
argbData[i] = grayData | grayData << 8 | grayData << 16 | 0xff000000;
}
return argbData;
}
/**大端、小端方式,两字节数据转short值(有符号)**/
public static short getShort(byte[] b, boolean isBigEdian) {
if (b == null || b.length <= 0 || b.length > 2) {
return 0;
}
if (isBigEdian) {
return (short) (((b[1] << 8) | b[0] & 0xff));
} else {
return (short) (((b[1] & 0xff) | b[0] << 8));
}
}
二、效果图,摄像头上面是一包漫花纸巾
三、参考文章