如何使用camera2 API控制相机闪光灯



我想使用camera2 API控制智能手机(Galaxy s6)的闪光灯。

我检查了它是可用的代码像这样,

try {
    CameraManager mManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
    String [] cameraId = mManager.getCameraIdList();
    CameraCharacteristics cameraCharacteristics = mManager.getCameraCharacteristics(cameraId[1]);
    Toast.makeText(getApplicationContext(),cameraId[0]+cameraId[1],Toast.LENGTH_LONG).show();
    boolean flashAvailable = cameraCharacteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
    if (flashAvailable) {
        mManager.openCamera(cameraId[0], new MyStateCallback(), null);
        Toast.makeText(getApplicationContext(),"Flash is available",Toast.LENGTH_LONG).show();
    } else {
        //todo: throw Exception
        Toast.makeText(getApplicationContext(),"Flash is not available",Toast.LENGTH_LONG).show();
    }
} catch (Exception e) {
    e.printStackTrace();
}

但是,Toast消息:Flash is not available.

当然,我在manifest中声明了权限和硬件特性。

该设备或相机a2 API是否无法控制闪光灯?

您正在检查第二个摄像头(可能是前置摄像头)是否有闪光灯,因为您在第4行上获得了cameraId[1]而不是cameraId[0]的相机特性。

通常,后置摄像头是第一个,并且是有闪光灯的。为了更健壮,迭代所有的相机id并检查它们是否有闪光灯。

最新更新