本文主要是介绍Androd Camera Yuv Jepg bmp,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
从 摄像头 预览 图像中获取yuv(或者格式数据) 转换成 jpeg bmp格式
转成bmp 格式,网上有人说,用 rawByteArray2RGBABitmap2 直接转的效率更高
Camera.PreviewCallback mPreviewCallback = new Camera.PreviewCallback() {
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
decodeToJpeg(data,camera);
}
}
public static void decodeToJpeg(byte[] data,Camera camera){
try {
Size size = camera.getParameters().getPreviewSize();
int format = camera.getParameters().getPreviewFormat();
log(“>>>>>>”+”format = “+ format+”size=”+size.width+”,”+size.height);
YuvImage image = new YuvImage(data, ImageFormat.NV21, size.width,
size.height, null);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compressToJpeg(new Rect(0, 0, size.width, size.height),
80, stream);
byte[] buf = stream.toByteArray();
log(">>>>>>" + "yuv Size="+data.length+",jpeg Size = "+stream.size()+","+buf.length);stream.close();} catch (Exception e) {e.printStackTrace();}
}public static void yuvToBmp(byte[] data,Camera camera){try {Size size = camera.getParameters().getPreviewSize();int format = camera.getParameters().getPreviewFormat();log(">>>>>>"+"format = "+ format+"size="+size.width+","+size.height);YuvImage image = new YuvImage(data, ImageFormat.NV21, size.width,size.height, null);ByteArrayOutputStream stream = new ByteArrayOutputStream();image.compressToJpeg(new Rect(0, 0, size.width, size.height),80, stream);Bitmap bmp = BitmapFactory.decodeByteArray(stream.toByteArray(), 0, stream.size());log(bmp.getWidth() + " " + bmp.getHeight());log(">>>>>>" + "yuv Size="+data.length+",jpeg Size = "+stream.size());stream.close();bmp.recycle();} catch (Exception e) {e.printStackTrace();}
}public Bitmap rawByteArray2RGBABitmap2(byte[] data, int width, int height) {int frameSize = width * height;int[] rgba = new int[frameSize];for (int i = 0; i < height; i++)for (int j = 0; j < width; j++) {int y = (0xff & ((int) data[i * width + j]));int u = (0xff & ((int) data[frameSize + (i >> 1) * width + (j & ~1) + 0]));int v = (0xff & ((int) data[frameSize + (i >> 1) * width + (j & ~1) + 1]));y = y < 16 ? 16 : y;int r = Math.round(1.164f * (y - 16) + 1.596f * (v - 128));int g = Math.round(1.164f * (y - 16) - 0.813f * (v - 128) - 0.391f * (u - 128));int b = Math.round(1.164f * (y - 16) + 2.018f * (u - 128));r = r < 0 ? 0 : (r > 255 ? 255 : r);g = g < 0 ? 0 : (g > 255 ? 255 : g);b = b < 0 ? 0 : (b > 255 ? 255 : b);rgba[i * width + j] = 0xff000000 + (b << 16) + (g << 8) + r;}Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);bmp.setPixels(rgba, 0 , width, 0, 0, width, height);return bmp;
}
这篇关于Androd Camera Yuv Jepg bmp的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!