Android 实时视频采集—Cameara

时间:2022-08-08 15:34:54

1 概述

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


2 知识点

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

Camera 支持格式

Android 实时视频采集—Cameara

拍照流程

Android 实时视频采集—Cameara


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

③ Camera权限

Android 实时视频采集—Cameara


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必须设置
}


Android 实时视频采集—Cameara

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

复制代码


public void surfaceCreated(SurfaceHolder holder) <span style="font-family: Arial, Helvetica, sans-serif;">{</span>
// 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) {
<span style="white-space:pre"></span>if(null != mCamera){
<span style="white-space:pre"></span>mCamera.release();
<span style="white-space:pre"></span>mCamera = null;
<span style="white-space:pre"></span>}
<span style="white-space:pre"></span>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;
}
}
Android 实时视频采集—Cameara

 核心子函数:  相机预览

private void initCamera() <a target=_blank title="复制代码" style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"></a><pre name="code" class="java" style="display: inline !important;">{

//surfaceChanged中调用 Log.i(TAG, "going into initCamera"); if (bIfPreview) { mCamera.stopPreview();//stopCamera(); } if(null != mCamera) { try{

<span style="white-space:pre"></span>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
// 【调试】获取caera支持的PictrueSize,看看能否设置??
List pictureSizes = mCamera.getParameters().getSupportedPictureSizes();
List previewSizes = mCamera.getParameters().getSupportedPreviewSizes();
List previewFormats = mCamera.getParameters().getSupportedPreviewFormats();
List 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++) <a target=_blank title="复制代码" style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"></a><pre name="code" class="java" style="display: inline !important;">{

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(); } } }


Android 实时视频采集—Cameara

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

③视频帧回调接

  // 【获取视频预览帧的接口】
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
};

Android 实时视频采集—Cameara