【流媒體】Android 实时视频采集—Camera预览采集


【流媒體】Android 实时视频采集—Cameara预览采集

SkySeraph Mar 26th 2012  SZ

Email:skyseraph00@163.com 


 更多精彩请直接访问SkySeraph个人站点www.skyseraph.com 

 流媒体系列: http://skyseraph.com/2012/04/11/Media/流媒体专题/ 

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

 核心子函数:  相机预览

 1 /*【2】【相机预览】*/
2 private void initCamera()//surfaceChanged中调用
3 {
4 Log.i(TAG, "going into initCamera");
5 if (bIfPreview)
6 {
7 mCamera.stopPreview();//stopCamera();
8 }
9 if(null != mCamera)
10 {
11 try
12 {
13 /* Camera Service settings*/
14 Camera.Parameters parameters = mCamera.getParameters();
15 // parameters.setFlashMode("off"); // 无闪光灯
16 parameters.setPictureFormat(PixelFormat.JPEG); //Sets the image format for picture 设定相片格式为JPEG,默认为NV21
17 parameters.setPreviewFormat(PixelFormat.YCbCr_420_SP); //Sets the image format for preview picture,默认为NV21
18 /*【ImageFormat】JPEG/NV16(YCrCb format,used for Video)/NV21(YCrCb format,used for Image)/RGB_565/YUY2/YU12*/
19
20 // 【调试】获取caera支持的PictrueSize,看看能否设置??
21 List<Size> pictureSizes = mCamera.getParameters().getSupportedPictureSizes();
22 List<Size> previewSizes = mCamera.getParameters().getSupportedPreviewSizes();
23 List<Integer> previewFormats = mCamera.getParameters().getSupportedPreviewFormats();
24 List<Integer> previewFrameRates = mCamera.getParameters().getSupportedPreviewFrameRates();
25 Log.i(TAG+"initCamera", "cyy support parameters is ");
26 Size psize = null;
27 for (int i = 0; i < pictureSizes.size(); i++)
28 {
29 psize = pictureSizes.get(i);
30 Log.i(TAG+"initCamera", "PictrueSize,width: " + psize.width + " height" + psize.height);
31 }
32 for (int i = 0; i < previewSizes.size(); i++)
33 {
34 psize = previewSizes.get(i);
35 Log.i(TAG+"initCamera", "PreviewSize,width: " + psize.width + " height" + psize.height);
36 }
37 Integer pf = null;
38 for (int i = 0; i < previewFormats.size(); i++)
39 {
40 pf = previewFormats.get(i);
41 Log.i(TAG+"initCamera", "previewformates:" + pf);
42 }
43
44 // 设置拍照和预览图片大小
45 parameters.setPictureSize(640, 480); //指定拍照图片的大小
46 parameters.setPreviewSize(mPreviewWidth, mPreviewHeight); // 指定preview的大小
47 //这两个属性 如果这两个属性设置的和真实手机的不一样时,就会报错
48
49 // 横竖屏镜头自动调整
50 if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE)
51 {
52 parameters.set("orientation", "portrait"); //
53 parameters.set("rotation", 90); // 镜头角度转90度(默认摄像头是横拍)
54 mCamera.setDisplayOrientation(90); // 在2.2以上可以使用
55 } else// 如果是横屏
56 {
57 parameters.set("orientation", "landscape"); //
58 mCamera.setDisplayOrientation(0); // 在2.2以上可以使用
59 }
60
61 /* 视频流编码处理 */
62 //添加对视频流处理函数
63
64
65 // 设定配置参数并开启预览
66 mCamera.setParameters(parameters); // 将Camera.Parameters设定予Camera
67 mCamera.startPreview(); // 打开预览画面
68 bIfPreview = true;
69
70 // 【调试】设置后的图片大小和预览大小以及帧率
71 Camera.Size csize = mCamera.getParameters().getPreviewSize();
72 mPreviewHeight = csize.height; //
73 mPreviewWidth = csize.width;
74 Log.i(TAG+"initCamera", "after setting, previewSize:width: " + csize.width + " height: " + csize.height);
75 csize = mCamera.getParameters().getPictureSize();
76 Log.i(TAG+"initCamera", "after setting, pictruesize:width: " + csize.width + " height: " + csize.height);
77 Log.i(TAG+"initCamera", "after setting, previewformate is " + mCamera.getParameters().getPreviewFormat());
78 Log.i(TAG+"initCamera", "after setting, previewframetate is " + mCamera.getParameters().getPreviewFrameRate());
79 } catch (Exception e)
80 {
81 e.printStackTrace();
82 }
83 }
84 }

 说明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);  再在这函数中实现上述三个回调函数  
 

③视频帧回调接口

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

 


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

  

posted @ 2012-03-26 23:41 SkySeraph 阅读(...) 评论(...) 编辑 收藏