简体   繁体   English

Android:如何检查设备上是否有闪光灯?

[英]Android: how to check the flash light is available on device?

How I check the flash light available on device?also want to know how can I On/Off the flash light? 我如何检查设备上的闪光灯?还想知道我如何开/关闪光灯? I have put the code but not working right now? 我已经把代码但现在不能正常工作? I search out this 我搜索出来了
http://gitorious.org/rowboat/frameworks-base/commit/eb9cbb8fdddf4c887004b20b504083035d57a15f http://gitorious.org/rowboat/frameworks-base/commit/eb9cbb8fdddf4c887004b20b504083035d57a15f
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/com/android/server/LightsService.java#LightsService http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/com/android/server/LightsService.java#LightsService

Please can tell which I should use? 请告诉我应该使用哪个?
Thank You. 谢谢。

You can use the following 您可以使用以下内容

context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

which will return true if a flash is available, false if not. 如果闪存可用则返回true,否则返回false。

See http://developer.android.com/reference/android/content/pm/PackageManager.html for more information. 有关详细信息,请参阅http://developer.android.com/reference/android/content/pm/PackageManager.html

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

or 要么

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

        Camera.Parameters parameters = camera.getParameters();

        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;
    }

First you get supported flash modes: 首先,您获得支持的闪存模式:

camera = Camera.open(i);    // Introduced in API level 9
parameters = camera.getParameters();
String[] flashModes = parameters.getSupportedFlashModes();

And then you check if this array contains the correct constants like: "auto", "on", "off". 然后检查此数组是否包含正确的常量,如:“auto”,“on”,“off”。

More info in: http://developer.android.com/reference/android/hardware/Camera.Parameters.html#FLASH_MODE_AUTO 更多信息: http//developer.android.com/reference/android/hardware/Camera.Parameters.html#FLASH_MODE_AUTO

This can help full to turn on/off flash light of device. 这可以帮助完全打开/关闭设备的闪光灯。 This is give me satisfaction, Hope be useful to you also. 这让我感到满意,希望对你也有用。

Turn On camera Flash 打开相机闪光灯

camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();

Turn Off camera Flash 关闭相机闪光灯

camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();

Put this Permission in manifest file 将此权限放在清单文件中

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />

For more detail you can go HERE. 有关更多详细信息,请访问此处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM