前段时间,一个项目需要生成bmp格式的图片,对图片进行检测,看设备的相机是否有问题。这就要Camera捕获的图像不能进行压缩处理,这就需要保存为bmp格式的图片。
首先,相机在预览的情况下可以获取到YUV格式的帧数据,然后转成RGB格式的数据,然后再转成bmp格式的数据,有了这些数据还需要保存到本地,实现图片的保存。
这里假设数据为NV21格式,它是YUV格式的一种。直接上代码:
1、NV21格式转成RGB格式
public static int[] yuv2rgb(byte[] yuv, int width, int height) {
int total = width * height;
int[] rgb = new int[total];
int Y, Cb = 0, Cr = 0, index = 0;
int R, G, B;
//这个地方改过y的顺序,由从0到height - 1,改为height - 1到0。原因是前者让图像上下颠倒。
for (int y = height - 1; y >= 0; y--) {
for (int x = 0; x < width; x++) {
Y = yuv[y * width + x];
if (Y < 0) Y += 255;
if ((x & 1) == 0) {
Cr = yuv[(y >> 1) * (width) + x + total];
Cb = yuv[(y >> 1) * (width) + x + total + 1];
if (Cb < 0) Cb += 127; else Cb -= 128;
if (Cr < 0) Cr += 127; else Cr -= 128;
}
R = Y + Cr + (Cr >> 2) + (Cr >> 3) + (Cr >> 5);
G = Y - (Cb >> 2) + (Cb >> 4) + (Cb >> 5) - (Cr >> 1) + (Cr >> 3) + (Cr >> 4) + (Cr >> 5);
B = Y + Cb + (Cb >> 1) + (Cb >> 2) + (Cb >> 6);
// Approximation
// R = (int) (Y + 1.40200 * Cr);
// G = (int) (Y - 0.34414 * Cb - 0.71414 * Cr);
// B = (int) (Y + 1.77200 * Cb);
if (R < 0) R = 0; else if (R > 255) R = 255;
if (G < 0) G = 0; else if (G > 255) G = 255;
if (B < 0) B = 0; else if (B > 255) B = 255;
rgb[index++] = 0xff000000 + (R << 16) + (G << 8) + B;
}
}
return rgb;
}
2、RGB格式再转成bmp图像
public class BMP {
// BMP Header
private byte[] id = { 0x42, 0x4D };
private int fileSize = 0;
private short spec1 = 0, spec2 = 0;
private int offset = 54;
// DIB Header
private int biSize = 40;
private int biWidth, biHeight;
private short biPlanes = 0, biBitCount = 32;
private int biCompression, biSizeImage, biXPelsPerMeter, biYPelsPerMeter, biClrUsed, biClrImportant;
// bitmap data
private int[] data;
public BMP(int width, int height, short pixelBits, int[] pixels) {
biWidth = width;
biHeight = height;
biBitCount = pixelBits;
data = pixels;
fileSize = width * height * pixelBits / 8 + offset;
}
public int getFileSize() {
return fileSize;
}
private byte[] getFile() {
ByteBuffer buffer = ByteBuffer.allocate(fileSize);
buffer.order(ByteOrder.LITTLE_ENDIAN);
buffer.put(id);
buffer.putInt(fileSize);
buffer.putShort(spec1);
buffer.putShort(spec2);
buffer.putInt(offset);
buffer.putInt(biSize);
buffer.putInt(biWidth);
buffer.putInt(biHeight);
buffer.putShort(biPlanes);
buffer.putShort(biBitCount);
buffer.putInt(biCompression);
buffer.putInt(biSizeImage);
buffer.putInt(biXPelsPerMeter);
buffer.putInt(biYPelsPerMeter);
buffer.putInt(biClrUsed);
buffer.putInt(biClrImportant);
for (int i = 0; i < data.length; i++) {
buffer.putInt(data[i]);
}
return buffer.array();
}
private byte[] getHeader() {
ByteBuffer buffer = ByteBuffer.allocate(54);
buffer.order(ByteOrder.LITTLE_ENDIAN);
buffer.put(id);
buffer.putInt(fileSize);
buffer.putShort(spec1);
buffer.putShort(spec2);
buffer.putInt(offset);
buffer.putInt(biSize);
buffer.putInt(biWidth);
buffer.putInt(biHeight);
buffer.putShort(biPlanes);
buffer.putShort(biBitCount);
buffer.putInt(biCompression);
buffer.putInt(biSizeImage);
buffer.putInt(biXPelsPerMeter);
buffer.putInt(biYPelsPerMeter);
buffer.putInt(biClrUsed);
buffer.putInt(biClrImportant);
return buffer.array();
}
public void saveBMP(String fileName) {
FileOutputStream out;
try {
out = new FileOutputStream(fileName);
out.write(getFile());
out.flush();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void saveBMPFileWithDataOutPutStream(int[] data) {
DataOutputStream output;
try {
output = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("/sdcard/big-endian.bmp")));
output.write(getHeader());
for (int i = 0; i < data.length; i++) {
output.writeInt(data[i]);
}
output.flush();
output.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void saveBMPFile(int[] data) {
byte[] result = new byte[data.length * 4];
;
int j = 0;
for (int i = 0; i < data.length; i++) {
result[j + 3] = (byte) (data[i] >> 24);
result[j + 2] = (byte) (data[i] >> 16 & 0x00FF);
result[j + 1] = (byte) (data[i] >> 8 & 0x0000FF);
result[j] = (byte) (data[i] & 0x000000FF);
j += 4;
}
FileOutputStream out;
try {
out = new FileOutputStream("/sdcard/little-endian.bmp");
out.write(getHeader());
out.write(result);
out.flush();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3、然后,传入原始的byte数组的数据
private void saveAsBmp(byte[] data){
//这里是1920,1080可能需要动态修改
int[] data_rgb = NV21.yuv2rgb(data,1920,1080);
BMP bmp = new BMP(1920,1080, (short) 32, data_rgb);
bmp.saveBMP("/sdcard/nv21.bmp"); // The output BMP file
bmp.saveBMPFileWithDataOutPutStream(data_rgb);
bmp.saveBMPFile(data_rgb);
}
以上就完成了数据的转化以及保存