24

如何检查设备是否有 LED 摄像头(手电筒)。我说的是安卓操作系统的设备?

我已经看到了一些解决方案,这些解决方案讨论了如何打开和关闭 LED,但是如果设备甚至没有 LED 会发生什么。

打开我正在使用的相机camera.open()

4

5 回答 5

47

其他答案

boolean hasFlash = this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

不适用于新的 2013 Nexus 7。以下代码将起作用:

public boolean hasFlash() {
        if (camera == null) {
            return false;
        }

        Camera.Parameters parameters;
        try {
            parameters = camera.getParameters();
        } catch (RuntimeException ignored)  {
            return false;
        }

        if (parameters.getFlashMode() == null) {
            return false;
        }

        List<String> supportedFlashModes = parameters.getSupportedFlashModes();
        if (supportedFlashModes == null || supportedFlashModes.isEmpty() || supportedFlashModes.size() == 1 && supportedFlashModes.get(0).equals(Camera.Parameters.FLASH_MODE_OFF)) {
            return false;
        }

        return true;
    }
于 2013-10-25T21:01:28.083 回答
6

getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)如果设备有闪存,则返回 true。有关更多详细信息,请参阅

于 2012-11-16T09:44:19.610 回答
3

您应该能够通过检查系统功能来检查闪存是否可用:

boolean hasFlash = this.getPackageManager()
                       .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

(前提是你在一个Activity)。如果不是,则使用某种context代替this.

PS 请注意,如果您实际尝试搜索,则很容易找到此信息。

于 2012-11-16T09:44:29.297 回答
2
PackageManager pm = context.getPackageManager();
        if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
            Log.e("err", "Device has no camera!");
            return;
        }       
        camera = Camera.open();
        p = camera.getParameters();
        flashModes = p.getSupportedFlashModes();
if(flashModes==null){
                        Toast.makeText(getApplicationContext(), "LED Not Available",Toast.LENGTH_LONG).show();
                }else
                {
Toast.makeText(getApplicationContext(), "LED  Available",Toast.LENGTH_LONG).show();
}
于 2012-11-16T09:50:46.693 回答
1

这就是我检查 LED 闪光灯是否可用的方法。此外,您不需要拥有相机权限即可运行此方法。

private fun isLedFlashAvailable(context: Context): Boolean {
    // method 1
    if (context.packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
        return true
    }

    // method 2
    val cameraManager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
    for (id in cameraManager.cameraIdList) {
        if (cameraManager.getCameraCharacteristics(id).get(CameraCharacteristics.FLASH_INFO_AVAILABLE) == true) {
            return true
        }
    }

    return false
}
于 2020-06-06T12:28:45.660 回答