简体   繁体   English

如何检测手电筒是开还是关?

[英]How to detect whether the flashlight is on or off?

I'm working on a game in which i need to check whether the flashlight is enabled. 我正在开发需要检查手电筒是否已启用的游戏。
So are there any parameters based on which I can check it. 因此,有没有可以根据其检查的参数。

camera.getParameters().getFlashMode().equals(Parameters.FLASH_MODE_TORCH)

This statement used to return true if the flashlight was turned ON, but this doesn't work now. 如果打开手电筒,该语句曾经返回true,但是现在不起作用。

You can check if flash is AUTO, ON, or OFF as: 您可以通过以下方式检查闪光灯是自动,开启还是关闭:

Camera.Parameters p= mCamera.getParameters();

if(p.getFlashMode().equals(android.hardware.Camera.Parameters.FLASH_MODE_ON))
{
 //DO STUFF...
}
else if(p.getFlashMode().equals(android.hardware.Camera.Parameters.FLASH_MODE_OFF))
{
//DO STUFF......
}
else if(p.getFlashMode().equals(android.hardware.Camera.Parameters.FLASH_MODE_TORCH))
{
//DO STUFF......
}
else if(p.getFlashMode().equals(android.hardware.Camera.Parameters.FLASH_MODE_AUTO))
{
//DO STUFF......
}
else
{
//DO STUFF.....
}

With camera2 API use CameraManager to get this information. 通过camera2 API,请使用CameraManager来获取此信息。

First obtain an instance of CameraManager : 首先获取CameraManager的实例:

CameraManager cm = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);

Alternatively, if you target API >= 23 you may use: 或者,如果目标API> = 23,则可以使用:

CameraManager cm = context.getSystemService(CameraManager.class);

Now you need to register a TorchCallback . 现在,您需要注册一个TorchCallback When registered, this callback will immediately return the status of all the torches for all the camera devices that have flash available. 注册后,此回调将立即返回所有具有闪光灯的相机设备的所有手电筒的状态。 This interface has two methods you need to implement: 此接口有两种您需要实现的方法:

void onTorchModeChanged(String cameraId, boolean enabled);
void onTorchModeUnavailable(String cameraId);

The first method will inform you whether the torch is on or off for the given camera. 第一种方法将通知您给定摄像机的割炬是打开还是关闭。 The torch may become completely unavailable, when, for example, an external camera with flash is disconnected. 例如,当外接闪光灯闪光灯断开时,割炬可能会变得完全不可用。

You register the callback as follows: 您注册回调,如下所示:

cameraManager.registerTorchCallback(myTorchCallback, null);

The second parameter of this method is Handler which you may supply if you want your callbacks to be called in a different thread. 此方法的第二个参数是Handler ,如果您希望在其他线程中调用回调,则可以提供它。 If null is supplied, the current thread is used. 如果提供null ,则使用当前线程。

Be aware that your app does not have exclusive access to the torch. 请注意,您的应用没有对割炬的独占访问权。 Other apps and the user themself may turn it on/off. 其他应用程序和用户自己可以打开/关闭它。 The callback allows you to detect these changes. 回调使您可以检测到这些更改。 For example if the user turns the torch off while your app needs it, you may ask them to turn the torch on. 例如,如果用户在应用程序需要时关闭割炬,则可以要求他们打开割炬。

To get all the available cameraIds, as you will receive them in callback, you may call: 要获取所有可用的cameraId,您将在回调中收到它们,则可以调用:

String[] cameraIds = cameraManager.getCameraIdList();

You may also be interested which cameras have flash available. 您可能还对哪些相机有闪光灯可用感兴趣。 For example to make this check for camera with id "0" you do: 例如,要对ID为“ 0”的摄像机进行检查,您可以执行以下操作:

boolean flashAvailable = cameraManager
  .getCameraCharacteristics("0")
  .get(CameraCharacteristics.FLASH_INFO_AVAILABLE);

Finally you may want to turn on/off the torch yourself: 最后,您可能需要自己打开/关闭割炬:

cameraManager.setTorchMode("0", true); // ON
cameraManager.setTorchMode("0", false); // OFF

This method may throw CameraAccessException so be sure to check the documentation for details. 此方法可能会引发CameraAccessException因此请务必检查文档以了解详细信息。

As @Marcin_Jedynak said before, you have to register a callback in order to obtain the state of the flashlight. 正如@Marcin_Jedynak之前所说,您必须注册一个回调才能获取手电筒的状态。

import android.content.Context;
import android.hardware.camera2.CameraManager;
import android.support.annotation.NonNull;

public class CmManager {
    public static boolean isFlashlightOn = false;
    public static void registerFlashlightState (Context context){
        CameraManager cameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
        cameraManager.registerTorchCallback(torchCallback, null);
    }

    public static void unregisterFlashlightState(Context context) {
        CameraManager cameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
        cameraManager.unregisterTorchCallback(torchCallback);
    }

    private static CameraManager.TorchCallback torchCallback = new CameraManager.TorchCallback() {
        @Override
        public void onTorchModeChanged(@NonNull String cameraId, boolean enabled) {
            super.onTorchModeChanged(cameraId, enabled);
            isFlashlightOn = enabled;
        }
    };
}

After calling public static void registerFlashlightState (Context context) , you can then track the state of the flashlight with isFlashlighOn . 调用public static void registerFlashlightState (Context context) ,可以使用isFlashlighOn跟踪手电筒的状态。
Unregister the callback when you don't need it more 当您不再需要回调时,请注销它
More information here 更多信息在这里

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

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