简体   繁体   中英

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.

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.

First obtain an instance of CameraManager :

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

Alternatively, if you target API >= 23 you may use:

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

Now you need to register a 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. If null is supplied, the current thread is used.

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:

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:

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.

As @Marcin_Jedynak said before, you have to register a callback in order to obtain the state of the flashlight.

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 .
Unregister the callback when you don't need it more
More information here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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