Android camera preview callback 的使用
来源:互联网 发布:ransac算法原理 编辑:程序博客网 时间:2024/04/28 08:17
android 的camera有一个preview callback,该接口中有一个方法onPreviewFrame:
public void onPreviewFrame(byte[] data, Camera camera) {}
第一个参数data的编码格式默认应该是YCbCr_420_SP (NV21)
该格式的data却无法用BitmapFactory.decodeByteArray方法来解码。在SDK8之后出现了YuvImage的API可以用来处理,但是似乎该API只能用来写入outputStream
以下提供人工解码方式:
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;}
如果使用opencv,则可以直接如下处理:
public Bitmap rawByteArray2RGBABitmap(byte[] data, int width, int height) {Mat originMat = new Mat(height + height / 2, width, CvType.CV_8UC1);originMat.put(0, 0, data);Mat rgbaMat = new Mat();Imgproc.cvtColor(originMat, rgbaMat, Imgproc.COLOR_YUV420sp2RGB, 4);Log.e(TAG, "rgba type is " + rgbaMat.channels() + rgbaMat.type());Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);if (Utils.matToBitmap(rgbaMat, bmp)) {originMat.release();rgbaMat.release(); return bmp;}bmp.recycle(); return null;}
以上来自android opencv 教程源码。
- Android camera preview callback 的使用
- android camera的preview buffer手动管理机制
- Android Camera Preview ANativeWindow的处理
- Android Camera Preview ANativeWindow的处理
- Android Camera Preview ANativeWindow的处理
- Android Camera Preview ANativeWindow的处理
- 【Android Camera】之 Preview
- 【Android Camera】之 Preview
- 【Android Camera】之 Preview
- 【Android Camera】之 Preview
- 【Android Camera】之 Preview
- android Camera preview data 流程
- android camera preview 屏幕拉伸变长的问题
- Android Camera HAL3中预览preview模式下的数据流
- Android Camera HAL3中预览preview模式下的数据流
- Android Camera HAL3中预览preview模式下的数据流
- Android Camera HAL3中预览preview模式下的数据流
- Android camera HAL四个callback
- Disable Back Button in Browser using JavaScript
- Jtable表头换行显示
- 使用UrlRewrite实现URL伪静态化
- 程序员懒人网站
- android检查存储空间小结
- Android camera preview callback 的使用
- 在struts的action创建out对象
- vc中实现控件的隐藏与显示
- CSDN博文精选:最受欢迎的系列专栏博客推荐
- ecTable 导出Excle 科学计数法问题
- 有效控制Android应用程序的耗电量
- Linux的TOP系统实时监控命令
- 海量、多页数据导出到Excel(C#版本)
- sql 事务语句