How does one retrieve the cameraID and use setTorchMode?

前端 未结 2 1605
孤城傲影
孤城傲影 2021-01-22 17:53

So Android M recently came out and it now has a built in cameralight function called setTorchMode. I was curious as to how this worked as the parameters are (String cameraID, Bo

相关标签:
2条回答
  • 2021-01-22 18:05
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            mCameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
            try {
                for (String camID : mCameraManager.getCameraIdList()) {
                    CameraCharacteristics cameraCharacteristics = mCameraManager.getCameraCharacteristics(camID);
                    int lensFacing = cameraCharacteristics.get(CameraCharacteristics.LENS_FACING);
                    if (lensFacing == CameraCharacteristics.LENS_FACING_FRONT && cameraCharacteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE)) {
                        mCameraId = camID;
                        break;
                    } else if (lensFacing == CameraCharacteristics.LENS_FACING_BACK && cameraCharacteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE)) {
                        mCameraId = camID;
                    }
                }
                if (mCameraId != null) {
                    mCameraManager.setTorchMode(mCameraId, true);
                }
            } catch (CameraAccessException e) {
                e.printStackTrace();
            }
        }
    

    mCameraId will turn on front camera flash if available or else back camera flash if available. If no flash is available then mCameraId will be null and setTorchMode will not be called.

    0 讨论(0)
  • 2021-01-22 18:21

    You should use CameraManager "getCameraIdList" function which will retrieve you a list of strings where each represent an active camera.

    0 讨论(0)
提交回复
热议问题