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的參數中 }