第6讲 Open和Close Camera代码实现 - Android Camera2 API

本讲是Android Camera专题系列的第6讲,我们介绍Android Camera2 API专题的Open、Close Camera代码实现部分。

更多资源:

资源 描述
在线课程 极客笔记在线课程
知识星球 星球名称:深入浅出Android Camera
星球ID: 17296815
Wechat 极客笔记圈

枚举Camera数量

public int getNumberOfCameras() {
    try {
        String[] cameraIdArray = mCameraManager.getCameraIdList();
        if(MyDebug.LOG) {
            Log.d(TAG, "getCameraIdList length:" + cameraIdArray.length);
        }
        for (int i = 0; i < cameraIdArray.length; i++) {
            isLogicalMultiCamera(mContext, i);
            if(MyDebug.LOG) {
                Log.d(TAG, "CameraID:" +i + ", Facing:" + getFacing(i));
            }
        }
        return cameraIdArray.length;
    } catch(Throwable e) {
        if(MyDebug.LOG) {
            Log.e(TAG, "exception trying to get camera ids");
        }
        e.printStackTrace();
    }
    return 0;
}

判断Camera Facing

@Override
public CameraController.Facing getFacing(int cameraId) {
    try {
        String cameraIdS = mCameraManager.getCameraIdList()[cameraId];
        CameraCharacteristics characteristics = mCameraManager.getCameraCharacteristics(cameraIdS);
        switch(characteristics.get(CameraCharacteristics.LENS_FACING)) {
            case CameraMetadata.LENS_FACING_FRONT:
                return CameraController.Facing.FACING_FRONT;
            case CameraMetadata.LENS_FACING_BACK:
                return CameraController.Facing.FACING_BACK;
            case CameraMetadata.LENS_FACING_EXTERNAL:
                return CameraController.Facing.FACING_EXTERNAL;
        }
        Log.e(TAG, "unknown camera_facing: " + characteristics.get(CameraCharacteristics.LENS_FACING));
    } catch(Throwable e) {
        if(MyDebug.LOG)
            Log.e(TAG, "exception trying to get camera characteristics");
        e.printStackTrace();
    }
    return CameraController.Facing.FACING_UNKNOWN;
}

判断是否是Logical MultiCamera

@Override
public boolean isLogicalMultiCamera(Context context, int cameraId) {
    try {
        String cameraIdS = mCameraManager.getCameraIdList()[cameraId];
        CameraCharacteristics characteristics = mCameraManager.getCameraCharacteristics(cameraIdS);
        Set<String> phySicalCameraIds = characteristics.getPhysicalCameraIds();
        int[] capabilities = characteristics.get(CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES);
        List<Integer> capabilitiesList = new ArrayList<>();
        for(Integer capability : capabilities) {
            capabilitiesList.add(capability);
        }
        int logicalMultiCameraId = CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES_LOGICAL_MULTI_CAMERA;
        Log.d(TAG, "(LogicalCamera: " + cameraIdS +
                ",PhysicalCameraIds:" + phySicalCameraIds.toString() +
                ",has LOGICAL_MULTI_CAMERA capability:" +
                capabilitiesList.contains(logicalMultiCameraId) + ")");
        return capabilitiesList.contains(logicalMultiCameraId) && (capabilities.length > 0);
    } catch (Throwable e) {
        if(MyDebug.LOG)
            Log.e(TAG, "exception trying to isLogicalMultiCamera.");
        e.printStackTrace();
    }
    return false;
}

OpenCamera流程代码实现

OpenCamera流程代码实现

实战代码流程

Preview#onSurfaceTextureAvailable
    Preview#mySurfaceCreated
        Preview#openCamera
            Preview#mOpenCameraTask.execute();
                Preview#openCameraCore
                    new CameraController2
                        manager.openCamera

Close Camera代码实现

Close Camera代码实现

实战代码流程

Preview#onSurfaceTextureDestroyed
    Preview#mySurfaceDestroyed
        Preview#closeCamera
            new CloseCameraTask
                CameraController2#release
                    mCameraDevice.close

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程

Android Camera2 API