Android开启相机预览获取Yuv视频流数据

自定义SurfaceView

主要步骤:

实现SurfaceHolder.Callback接口,创建SurfaceView的生命周期

实现Camera.PreviewCallback接口,创建预览回调接口

创建Camera打开相机

实例:

public class CameraSurfaceViews extends SurfaceView implements SurfaceHolder.Callback, Camera.PreviewCallback{

    Context mContext;
    SurfaceHolder mSurfaceHolder;//surface的控制器
    Camera mCamera;//相机类
    FrameCallback mCb;//数据回调接口

    public void setmCb(FrameCallback mCb) {
        this.mCb = mCb;
    }
  
//获取surfaceView的SurfaceHolder对象和接口 public CameraSurfaceViews(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; mSurfaceHolder = getHolder(); mSurfaceHolder.addCallback(this); }
//寻找相机 private int findCamera(boolean isfront) { int cameraCount = 0; Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); cameraCount = Camera.getNumberOfCameras(); for (int camIdx = 0; camIdx < cameraCount; camIdx++) { Camera.getCameraInfo(camIdx, cameraInfo); if (isfront) { // CAMERA_FACING_FRONT前置 if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { return camIdx; } } else { // CAMERA_FACING_BACK后置 if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) { return camIdx; } } } return -1; } //打开相机 private Camera getCamera() { Camera camera = null; int cameraId = findCamera(false); try { if(cameraId==1){ camera = Camera.open(cameraId ); }else if(cameraId==0)( camera = Camera.open(0); }
        }
catch (Exception e) { camera = null; } return camera; } //surface被创建时调用 @Override public void surfaceCreated(SurfaceHolder holder) { mCamera = getCamera();
     }
//surface大小被改变时调用 @Override public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {     setStartPreview(mCamera,mSurfaceHolder);
}

  //surface被销毁时调用
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        releaseCamera();
    }

    //开启相机预览
    private void setStartPreview(Camera camera, SurfaceHolder holder) {
        try {

            mCamera.setPreviewDisplay(holder);
            mCamera.setPreviewCallback(this);
            camera.startPreview();
        } catch (IOException e) {
        }
    }
//释放Camera public void releaseCamera() { if (mCamera != null) { mCamera.setPreviewCallback(null); mCamera.stopPreview();// 停掉摄像头的预览 mCamera.release(); mCamera = null; } }
//预览回调,传递yuv视频流数据 @Override public void onPreviewFrame(byte[] bytes, Camera camera) { // Log.i("onPreviewFrame",""+ bytes); if(mCb!= null){ mCb.onDecodeFrame(bytes); } } }

 FrameCallback回调接口

public interface FrameCallback {
    void onDecodeFrame(byte[] data) ;
}

MainActivity

public class MainActivity extends AppCompatActivity implements FrameCallback{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  }

    @Override
    public void onDecodeFrame(byte[] data) {
      //实时处理yuv视频流数据
    }

}

相关阅读

了解SurfaceView

Yuv数据格式相关转换方法

posted @ 2018-08-16 09:39  YangBryant  阅读(6756)  评论(0编辑  收藏  举报