当前位置: 代码迷 >> Android >> (转)【源媒體】Android 实时视频采集—Cameara预览采集
  详细解决方案

(转)【源媒體】Android 实时视频采集—Cameara预览采集

热度:47   发布时间:2016-05-01 13:31:15.0
(转)【流媒體】Android 实时视频采集—Cameara预览采集

1 概述

通过Android Camera拍摄预览中设置setPreviewCallback实现onPreviewFrame接口,实时截取每一帧视频流数据

2 知识点

① Android Camera使用:    参考 Refs/Related 0-4

Camera 支持格式

拍照流程

② Android SurfaceView使用:  参考 Refs/Related 5-10

③ Camera权限

3 核心源码

① SurfaceView相关

// 定义对象 private SurfaceView mSurfaceview = null;  // SurfaceView对象:(视图组件)视频显示  private SurfaceHolder mSurfaceHolder = null;  // SurfaceHolder对象:(抽象接口)SurfaceView支持类  private Camera mCamera =null;     // Camera对象,相机预览
?
// InitSurfaceView private void initSurfaceView() {  mSurfaceview = (SurfaceView) this.findViewById(R.id.Surfaceview);  mSurfaceHolder = mSurfaceview.getHolder(); // 绑定SurfaceView,取得SurfaceHolder对象  mSurfaceHolder.addCallback(mainActivity.this); // SurfaceHolder加入回调接口  // mSurfaceHolder.setFixedSize(176, 144); // 预览大小設置  mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);// 設置顯示器類型,setType必须设置 }
?

② 主Activity实现SurfaceHolder.Callback接口,编写回调函数

/*【SurfaceHolder.Callback 回调函数】*/ public void surfaceCreated(SurfaceHolder holder) // SurfaceView启动时/初次实例化,预览界面被创建时,该方法被调用。 {  // TODO Auto-generated method stub    mCamera = Camera.open();// 开启摄像头(2.3版本后支持多摄像头,需传入参数)   try  {      Log.i(TAG, "SurfaceHolder.Callback:surface Created");   mCamera.setPreviewDisplay(mSurfaceHolder);//set the surface to be used for live preview  } catch (Exception ex)  {   if(null != mCamera)   {    mCamera.release();    mCamera = null;        }   Log.i(TAG+"initCamera", ex.getMessage());  } } public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) // 当SurfaceView/预览界面的格式和大小发生改变时,该方法被调用 {  // TODO Auto-generated method stub    Log.i(TAG, "SurfaceHolder.Callback:Surface Changed");  //mPreviewHeight = height;  //mPreviewWidth = width;  initCamera();   } public void surfaceDestroyed(SurfaceHolder holder)  // SurfaceView销毁时,该方法被调用 {  // TODO Auto-generated method stub  Log.i(TAG, "SurfaceHolder.Callback:Surface Destroyed");  if(null != mCamera)  {   mCamera.setPreviewCallback(null); //!!这个必须在前,不然退出出错   mCamera.stopPreview();    bIfPreview = false;    mCamera.release();   mCamera = null;       } }
?

核心子函数:  相机预览

/*【2】【相机预览】*/  private void initCamera()//surfaceChanged中调用  {   Log.i(TAG, "going into initCamera");   if (bIfPreview)   {    mCamera.stopPreview();//stopCamera();   }   if(null != mCamera)   {    try    {     /* Camera Service settings*/         Camera.Parameters parameters = mCamera.getParameters();     // parameters.setFlashMode("off"); // 无闪光灯     parameters.setPictureFormat(PixelFormat.JPEG); //Sets the image format for picture 设定相片格式为JPEG,默认为NV21         parameters.setPreviewFormat(PixelFormat.YCbCr_420_SP); //Sets the image format for preview picture,默认为NV21     /*【ImageFormat】JPEG/NV16(YCrCb format,used for Video)/NV21(YCrCb format,used for Image)/RGB_565/YUY2/YU12*/          // 【调试】获取caera支持的PictrueSize,看看能否设置??     List<Size> pictureSizes = mCamera.getParameters().getSupportedPictureSizes();     List<Size> previewSizes = mCamera.getParameters().getSupportedPreviewSizes();     List<Integer> previewFormats = mCamera.getParameters().getSupportedPreviewFormats();     List<Integer> previewFrameRates = mCamera.getParameters().getSupportedPreviewFrameRates();     Log.i(TAG+"initCamera", "cyy support parameters is ");     Size psize = null;     for (int i = 0; i < pictureSizes.size(); i++)     {      psize = pictureSizes.get(i);      Log.i(TAG+"initCamera", "PictrueSize,width: " + psize.width + " height" + psize.height);     }     for (int i = 0; i < previewSizes.size(); i++)     {      psize = previewSizes.get(i);      Log.i(TAG+"initCamera", "PreviewSize,width: " + psize.width + " height" + psize.height);     }     Integer pf = null;     for (int i = 0; i < previewFormats.size(); i++)     {      pf = previewFormats.get(i);      Log.i(TAG+"initCamera", "previewformates:" + pf);     }          // 设置拍照和预览图片大小     parameters.setPictureSize(640, 480); //指定拍照图片的大小     parameters.setPreviewSize(mPreviewWidth, mPreviewHeight); // 指定preview的大小  //这两个属性 如果这两个属性设置的和真实手机的不一样时,就会报错      // 横竖屏镜头自动调整     if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE)      {      parameters.set("orientation", "portrait"); //      parameters.set("rotation", 90); // 镜头角度转90度(默认摄像头是横拍)       mCamera.setDisplayOrientation(90); // 在2.2以上可以使用     } else// 如果是横屏     {      parameters.set("orientation", "landscape"); //      mCamera.setDisplayOrientation(0); // 在2.2以上可以使用     }           /* 视频流编码处理 */      //添加对视频流处理函数           // 设定配置参数并开启预览     mCamera.setParameters(parameters); // 将Camera.Parameters设定予Camera         mCamera.startPreview(); // 打开预览画面     bIfPreview = true;          // 【调试】设置后的图片大小和预览大小以及帧率     Camera.Size csize = mCamera.getParameters().getPreviewSize();     mPreviewHeight = csize.height; //     mPreviewWidth = csize.width;     Log.i(TAG+"initCamera", "after setting, previewSize:width: " + csize.width + " height: " + csize.height);     csize = mCamera.getParameters().getPictureSize();     Log.i(TAG+"initCamera", "after setting, pictruesize:width: " + csize.width + " height: " + csize.height);     Log.i(TAG+"initCamera", "after setting, previewformate is " + mCamera.getParameters().getPreviewFormat());     Log.i(TAG+"initCamera", "after setting, previewframetate is " + mCamera.getParameters().getPreviewFrameRate());    } catch (Exception e)    {      e.printStackTrace();    }   }  }

?说明1:  在/* 视频流编码处理 */?中通过setPreviewCallback添加对视频流进行处理,如
???  mCamera.setPreviewCallback(new encoderVideo(mCamera.getParameters().getPreviewSize().width,?
?????    mCamera.getParameters().getPreviewSize().height,(ImageView) findViewById(R.id.ImageView2)));//①原生yuv420sp视频存储方式
???  mCamera.setPreviewCallback(new encoderH264(mCamera.getParameters().getPreviewSize().width,?
?????    mCamera.getParameters().getPreviewSize().height));?//②x264编码方式?
??  ?mCamera.setPreviewCallback(mJpegPreviewCallback);??//③JPEG压缩方式
?
?说明2:?当然也可以不在主Acitivity中实现SurfaceHolder.Callback接口,而是在①的initSurfaceView中的addCallback函数,修改为
?mSurfaceHolder.addCallback(new MyCallback);  再在这函数中实现上述三个回调函数

?

③视频帧回调接口

// 【获取视频预览帧的接口】   mJpegPreviewCallback = new Camera.PreviewCallback()   {    @Override    public void onPreviewFrame(byte[] data, Camera camera)     {     //传递进来的data,默认是YUV420SP的 // TODO Auto-generated method stub         try     {      Log.i(TAG, "going into onPreviewFrame");      //mYUV420sp = data;   // 获取原生的YUV420SP数据      YUVIMGLEN = data.length;            // 拷贝原生yuv420sp数据      mYuvBufferlock.acquire();      System.arraycopy(data, 0, mYUV420SPSendBuffer, 0, data.length);      //System.arraycopy(data, 0, mWrtieBuffer, 0, data.length);      mYuvBufferlock.release();            // 开启编码线程,如开启PEG编码方式线程      mSendThread1.start();           } catch (Exception e)     {      Log.v("System.out", e.toString());     }// endtry        }// endonPriview     };
?

Refs/Related

0 http://developer.android.com/reference/android/graphics/Camera.html
1 http://wenku.baidu.com/view/5551eb59be23482fb4da4c89.html
2 http://www.cnblogs.com/franksunny/archive/2011/11/17/2252926.html
3 http://blog.csdn.net/yf210yf/article/details/7304437
4 http://topic.csdn.net/u/20111104/13/fdf356b5-ea77-4ee9-ba8e-c44d0f1b7a19.html??
5 http://developer.android.com/reference/android/view/SurfaceView.html
6 http://developer.android.com/reference/android/view/SurfaceHolder.html
7 http://blog.csdn.net/xiaominghimi/article/details/6089594
8 http://blog.csdn.net/xiaominghimi/article/details/6090575
9 http://www.cnblogs.com/emily1314/archive/2012/03/03/2378147.html
10 http://blog.csdn.net/hellogv/article/details/5985090

  相关解决方案