1. 程式人生 > >多個攝像頭在一個Activity裡顯示

多個攝像頭在一個Activity裡顯示

首先:系統需要支援對多攝像頭的顯示

然後上程式碼:

第一步,是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", "影象出錯");
        }
    }
}