首先:系统需要支持对多摄像头的显示
然后上代码:
第一步,是View代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.csht.three.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <SurfaceView android:id="@+id/surfaceview1" android:layout_width="match_parent" android:layout_weight="1" android:layout_marginTop="5dp" android:layout_height="0dp" /> <SurfaceView android:id="@+id/surfaceview2" android:layout_width="fill_parent" android:layout_weight="1" android:layout_marginTop="5dp" android:layout_height="0dp" /> <SurfaceView android:id="@+id/surfaceview3" android:layout_width="fill_parent" android:layout_weight="1" android:layout_marginTop="5dp" android:layout_height="0dp" android:layout_marginBottom="5dp" /> </LinearLayout> </LinearLayout>
第二步:
private int cameraCount;private void DoIt() { cameraCount = Camera.getNumberOfCameras(); for (int i = 0; i<cameraCount; i++){ getSurfaceholder(i); } }private SurfaceHolder getSurfaceholder(int i){ SurfaceHolder surfaceHolder = null; if (i==0){ surfaceHolder = surfaceview1.getHolder(); surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); surfaceHolder.addCallback(new surfaceholderCallbackBack(i,getApplicationContext())); } if (i==1){ surfaceHolder = surfaceview2.getHolder(); surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); surfaceHolder.addCallback(new surfaceholderCallbackBack(i,getApplicationContext())); } if (i==2){ surfaceHolder = surfaceview3.getHolder(); surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); surfaceHolder.addCallback(new surfaceholderCallbackBack(i,getApplicationContext())); } return surfaceHolder; }第三步:/** * Created by Administrator on 2017/7/17. */ public class surfaceholderCallbackBack implements SurfaceHolder.Callback { private Camera camera = null; private int cameraNum; private Context context; public surfaceholderCallbackBack(int cameraNum,Context context){ this.cameraNum = cameraNum; this.context = context; } @Override public void surfaceCreated(SurfaceHolder holder) { // 获取camera对象 //int cameraCount = Camera.getNumberOfCameras(); camera = Camera.open(cameraNum); try { // 设置预览监听 camera.setPreviewDisplay(holder); Camera.Parameters parameters = camera.getParameters(); if (context.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) { parameters.set("orientation", "portrait"); camera.setDisplayOrientation(90); parameters.setRotation(90); } else { parameters.set("orientation", "landscape"); camera.setDisplayOrientation(0); parameters.setRotation(0); } camera.setParameters(parameters); // 启动摄像头预览 camera.startPreview(); System.out.println("camera.startpreview"); } catch (IOException e) { e.printStackTrace(); camera.release(); System.out.println("camera.release"); } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { camera.autoFocus(new Camera.AutoFocusCallback() { @Override public void onAutoFocus(boolean success, Camera camera) { if (success) { initCamera();// 实现相机的参数初始化 camera.cancelAutoFocus();// 只有加上了这一句,才会自动对焦。 } } }); } @Override public void surfaceDestroyed(SurfaceHolder holder) { } // 相机参数的初始化设置 private void initCamera() { Camera.Parameters parameters = camera.getParameters(); parameters.setPictureFormat(PixelFormat.JPEG); parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);// 1连续对焦 setDispaly(parameters, camera); camera.setParameters(parameters); camera.startPreview(); camera.cancelAutoFocus();// 2如果要实现连续的自动对焦,这一句必须加上 } // 控制图像的正确显示方向 private void setDispaly(Camera.Parameters parameters, Camera camera) { if (Integer.parseInt(Build.VERSION.SDK) >= 8) { setDisplayOrientation(camera, 90); } else { parameters.setRotation(90); } } // 实现的图像的正确显示 private void setDisplayOrientation(Camera camera, int i) { Method downPolymorphic; try { downPolymorphic = camera.getClass().getMethod("setDisplayOrientation", new Class[]{int.class}); if (downPolymorphic != null) { downPolymorphic.invoke(camera, new Object[]{i}); } } catch (Exception e) { Log.e("Came_e", "图像出错"); } } }