Android中调用摄像头预览的步骤如下:
1.SurfaceView相关
//定义对象 private SurfaceView photoView; // SurfaceView对象:(视图组件)视频显示 private SurfaceHolder photoHolder; // SurfaceHolder对象:(抽象接口)SurfaceView支持类 private Camera camera; // Camera对象,相机预览
//SurfaceView对象的设置 photoHolder=photoView.getHolder(); // 绑定SurfaceView,取得SurfaceHolder对象 photoHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); //设置显示器类型 photoHolder.setFixedSize(100, 100); photoHolder.addCallback(callback); // SurfaceHolder加入回调接口
2. 实现SurfaceHolder.Callback接口的实现
SurfaceHolder.Callback callback=new SurfaceHolder.Callback() { @Override public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub camera.setPreviewCallback(null); camera.stopPreview(); camera.release(); camera=null; } @Override public void surfaceCreated(SurfaceHolder holder) { // TODO Auto-generated method stub camera=Camera.open(); try { camera.setPreviewDisplay(photoHolder); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub initCamera(); } };
3.Camera属性的设置
//摄像头属性的设置 public void initCamera(){ Camera.Parameters parameters=camera.getParameters(); //得到摄像头属性 camera.setDisplayOrientation(90); //旋转90度(不设置时,会横向显示) camera.setParameters(parameters); //将属性值保存至Camera的参数中 }