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

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

视频在线观看:

加入知识星球与更多Camera同学交流
– 星球名称:深入浅出Android Camera
– 星球ID: 17296815

枚举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
赞(2)
未经允许不得转载:极客笔记 » 第6讲 Android Camera2 API Open、Close Camera代码实现
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

Android Camera2 API