http://blog.csdn.net/yanzi1225627/article/details/8626411/

Android开发:实时处理摄像头预览帧视频------浅析PreviewCallback,onPreviewFrame,AsyncTask的综合应用 这里将大致框架介绍了,但很多人对onPreviewFrame()里的处理提出质疑。认为下面的转换是多余的:

[java]  view plain copy print ?
  1. final YuvImage image = new YuvImage(mData, ImageFormat.NV21, w, h, null);  
  2. ByteArrayOutputStream os = new ByteArrayOutputStream(mData.length);  
  3. if(!image.compressToJpeg(new Rect(00, w, h), 100, os)){  
  4.     return null;  
  5. }  
  6. byte[] tmp = os.toByteArray();  
  7. Bitmap bmp = BitmapFactory.decodeByteArray(tmp, 0,tmp.length);   


因为这个mData是byte[ ]格式,转换流程是:byte[ ]---YuvImage----ByteArrayOutputStream---byte[ ]-----Bitmap。乍一看这个转换还真是多余了。看看看goolge的api:

[html]  view plain copy print ?
  1. public abstract void onPreviewFrame (byte[] data, Camera camera)  
  2.   
  3. Added in API level 1  
  4. Called as preview frames are displayed. This callback is invoked on the event thread open(int) was called from.  
  5.   
  6. If using the YV12 format, refer to the equations in setPreviewFormat(int) for the arrangement of the pixel data in the preview callback buffers.  
  7.   
  8. Parameters  
  9. data    the contents of the preview frame in the format defined by ImageFormat, which can be queried with getPreviewFormat(). If setPreviewFormat(int) is never called, the default will be the YCbCr_420_SP (NV21) format.  
  10. camera  the Camera service object.  


大致意思是:可以用getPreviewFormat()查询支持的预览帧格式。如果setPreviewFormat(INT) 从未被调用,默认将使用YCbCr_420_SP的格式(NV21)。

setPreviewFormat里,它又说:

[html]  view plain copy print ?
  1. public void setPreviewFormat (int pixel_format)  
  2.   
  3. Added in API level 1  
  4. Sets the image format for preview pictures.  
  5.   
  6. If this is never called, the default format will be NV21, which uses the NV21 encoding format.  
  7.   
  8. Use getSupportedPreviewFormats() to get a list of the available preview formats.  
  9.   
  10. It is strongly recommended that either NV21 or YV12 is used, since they are supported by all camera devices.  
  11.   
  12. For YV12, the image buffer that is received is not necessarily tightly packed, as there may be padding at the end of each row of pixel data, as described in YV12. For camera callback data, it can be assumed that the stride of the Y and UV data is the smallest possible that meets the alignment requirements. That is, if the preview size is width x height, then the following equations describe the buffer index for the beginning of row y for the Y plane and row c for the U and V planes:   
  13.   
  14.  yStride   = (int) ceil(width / 16.0) * 16;  
  15.  uvStride  = (int) ceil( (yStride / 2) / 16.0) * 16;  
  16.  ySize     = yStride * height;  
  17.  uvSize    = uvStride * height / 2;  
  18.  yRowIndex = yStride * y;  
  19.  uRowIndex = ySize + uvSize + uvStride * c;  
  20.  vRowIndex = ySize + uvStride * c;  
  21.  size      = ySize + uvSize * 2;  

强烈建议使用NV21格式和YV21格式,而默认情况下是NV21格式,也就是YUV420SP的。因此不经过转换,直接用BitmapFactory解析是不能成功的。事实也是如此。直接解析mData将会得到如下的错误:

 

另外下面也提到NV21是通用的。

getSupportedPreviewFormats ()

 

Added in  API level 5

Gets the supported preview formats. NV21 is always supported. YV12 is always supported since API level 12.

 

 

如果嫌YuvImage进行压缩解析的慢,只能自己写转换函数了,网上常见的有三种:

一:这里只是一个编码框架

参考这里:Android 实时视频采集—Camera预览采集

[java]  view plain copy print ?
  1. // 【获取视频预览帧的接口】  
  2.    mJpegPreviewCallback = new Camera.PreviewCallback()  
  3.    {  
  4.     @Override  
  5.     public void onPreviewFrame(byte[] data, Camera camera)   
  6.     {  
  7.      //传递进来的data,默认是YUV420SP的  
  8.  // TODO Auto-generated method stub      
  9.      try  
  10.      {  
  11.       Log.i(TAG, "going into onPreviewFrame");  
  12.       //mYUV420sp = data;   // 获取原生的YUV420SP数据  
  13.       YUVIMGLEN = data.length;  
  14.         
  15.       // 拷贝原生yuv420sp数据  
  16.       mYuvBufferlock.acquire();  
  17.       System.arraycopy(data, 0, mYUV420SPSendBuffer, 0, data.length);  
  18.       //System.arraycopy(data, 0, mWrtieBuffer, 0, data.length);  
  19.       mYuvBufferlock.release();  
  20.         
  21.       // 开启编码线程,如开启PEG编码方式线程  
  22.       mSendThread1.start();  
  23.         
  24.      } catch (Exception e)  
  25.      {  
  26.       Log.v("System.out", e.toString());  
  27.      }// endtry      
  28.     }// endonPriview    
  29.    };  

 

二、下面是将yuv420sp转成rgb参考这里:android视频采集 

[html]  view plain copy print ?
  1. private void updateIM() {  
  2. try {  
  3. // 解析YUV成RGB格式  
  4. decodeYUV420SP(byteArray, yuv420sp, width, height);  
  5. DataBuffer dataBuffer = new DataBufferByte(byteArray, numBands);  
  6. WritableRaster wr = Raster.createWritableRaster(sampleModel,  
  7. dataBuffer, new Point(0, 0));  
  8. im = new BufferedImage(cm, wr, false, null);  
  9. } catch (Exception ex) {  
  10. ex.printStackTrace();  
  11. }  
  12. }  
  13.   
  14. private static void decodeYUV420SP(byte[] rgbBuf, byte[] yuv420sp,  
  15. int width, int height) {  
  16. final int frameSize = width * height;  
  17. if (rgbBuf == null)  
  18. throw new NullPointerException("buffer 'rgbBuf' is null");  
  19. if (rgbBuf.length < frameSize * 3)  
  20. throw new IllegalArgumentException("buffer 'rgbBuf' size "  
  21. + rgbBuf.length + " < minimum " + frameSize * 3);  
  22.   
  23. if (yuv420sp == null)  
  24. throw new NullPointerException("buffer 'yuv420sp' is null");  
  25.   
  26. if (yuv420sp.length < frameSize * 3 / 2)  
  27. throw new IllegalArgumentException("buffer 'yuv420sp' size "  
  28. + yuv420sp.length + " < minimum " + frameSize * 3 / 2);  
  29.   
  30. int i = 0y = 0;  
  31. int uvp = 0u = 0v = 0;  
  32. int y1192 = 0r = 0g = 0b = 0;  
  33.   
  34. for (int j = 0yp = 0; j < height; j++) {  
  35. uvp = frameSize + (j >> 1) * width;  
  36. u = 0;  
  37. v = 0;  
  38. for (i = 0; i < width; i++, yp++) {  
  39. y = (0xff & ((int) yuv420sp[yp])) - 16;  
  40. if (y < 0)  
  41. y = 0;  
  42. if ((i & 1) == 0) {  
  43. v = (0xff & yuv420sp[uvp++]) - 128;  
  44. u = (0xff & yuv420sp[uvp++]) - 128;  
  45. }  
  46.   
  47. y1192 = 1192 * y;  
  48. r = (y1192 + 1634 * v);  
  49. g = (y1192 - 833 * v - 400 * u);  
  50. b = (y1192 + 2066 * u);  
  51.   
  52. if (r < 0)  
  53. r = 0;  
  54. else if (r > 262143)  
  55. r = 262143;  
  56. if (g < 0)  
  57. g = 0;  
  58. else if (g > 262143)  
  59. g = 262143;  
  60. if (b < 0)  
  61. b = 0;  
  62. else if (b > 262143)  
  63. b = 262143;  
  64.   
  65. rgbBuf[yp * 3] = (byte) (r >> 10);  
  66. rgbBuf[yp * 3 + 1] = (byte) (g >> 10);  
  67. rgbBuf[yp * 3 + 2] = (byte) (b >> 10);  
  68. }  
  69. }  
  70. }  
  71.   
  72. public static void main(String[] args) {  
  73. Frame f = new FlushMe();  
  74. }  
  75. }  

 

三、将YUV420SP转成YUV420格式

    参考这里:Android如何实现边采集边上传

 

[java]  view plain copy print ?
  1. private byte[] changeYUV420SP2P(byte[]data,int length){  
  2.   
  3. int width = 176;  
  4.   
  5. int height = 144;  
  6.   
  7. byte[] str = new byte[length];  
  8.   
  9. System.arraycopy(data, 0, str, 0,width*height);  
  10.   
  11.   
  12.   
  13. int strIndex = width*height;  
  14.   
  15.   
  16.   
  17. for(int i = width*height+1; i < length ;i+=2)  
  18.   
  19. {  
  20.   
  21. str[strIndex++] = data[i];  
  22.   
  23. }  
  24.   
  25.   
  26.   
  27.   
  28.   
  29. for(int i = width*height;i2)  
  30.   
  31. {  
  32.   
  33. str[strIndex++] = data[i];  
  34.   
  35. }  
  36.   
  37. return str;  
  38.   
  39. }   


至于怎么从YUV420SP中直接提取出Y分量进行后续检测,这个还要研究一番。有知道的大神多赐教。
  

----------------------------------------------------------------------------------------本文系原创,转载请注明作者:yanzi1225627

欢迎android爱好者加群248217350,备注:yanzi

 

更多相关文章

  1. Android中string.xml使用总结
  2. JAVA的整型与字符串相互转换 android
  3. Android自带倒计时控件Chronometer使用方法详解
  4. android initlogo.rle 在32位LCD上显示
  5. 【Android】【opencv】实现摄像头拍照和录像
  6. Android(安卓)百度地图经纬度转换成地址
  7. Android(安卓)studio将项目转换为jar文件
  8. android使用libyuv
  9. Android下保存简单网页到本地(包括简单图片链接转换)

随机推荐

  1. SQL Server 数据库基本操作语句总结
  2. SQL Server 数据库分离与附加(图文教程)
  3. CMD命令操作MSSQL2005数据库(命令整理)
  4. SQL有外连接的时候注意过滤条件位置否则
  5. sql to sqlalchemy 转换的小例子
  6. android 相关2
  7. android webview点击返回键回到上一个htm
  8. 项目整合ActionBarSherlock,no android:ac
  9. VideoView控件可以手动改变大小
  10. 我的第一个安卓程序