手电筒控制在棉花糖 [英] Flashlight control in Marshmallow

查看:281
本文介绍了手电筒控制在棉花糖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在最近的Marshmallow版本中有一个相机的问题,更具体地说是手电筒。
在任何前Marshmallow版本,我需要做的是打开/关闭闪光灯是以下:

  private void turnFlashOn(final camera camera,int flashLightDurationMs){
if(!isFlashOn()){
final List< String> supportedFlashModes = camera.getParameters()。getSupportedFlashModes();
if(supportedFlashModes!= null&& supportedFlashModes.contains(Camera.Parameters.FLASH_MODE_TORCH)){
mParams.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(mParams);
}
}
}

  private void turnFlashOff(Camera camera){
if(camera!= null){
final List& supportedFlashModes = camera.getParameters()。getSupportedFlashModes();
if(supportedFlashModes!= null&& supportedFlashModes.contains(Camera.Parameters.FLASH_MODE_OFF)){
mParams.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(mParams);不幸的是,Marshmallow的设备开始了,但是它不是一个非常有用的工具,在野外崩溃。不知何故 camera.getParameters() camera.setParameters()开始失败,如:


RuntimeException:getParameters失败(空参数)



RuntimeException:setParameters失败


我尝试在获取参数之前启动和停止预览,不再抛出错误。但是,当我调用 camera.startPreview()时,预览不会恢复。



我担心发布相机并重新打开它的问题,因为这需要几秒钟,会产生不好的体验。



关于如何可靠地在Marshmallow打开/关闭手电筒的任何建议?

解决方案

Google介绍了torchmode OS 6(Android M)。

如果您的目的只是打开/关闭闪光灯,以下代码可以帮助您:

  private static void handleActionTurnOnFlashLight(Context context){
try {

CameraManager manager =(CameraManager)context.getSystemService(Context.CAMERA_SERVICE );
String [] list = manager.getCameraIdList();
manager.setTorchMode(list [0],true);
}
catch(CameraAccessException cae){
Log.e(TAG,cae.getMessage());
cae.printStackTrace();
}
}

private static void handleActionTurnOffFlashLight(Context context){
try {
CameraManager manager =(CameraManager)context.getSystemService(Context.CAMERA_SERVICE );
manager.setTorchMode(manager.getCameraIdList()[0],false);
}
catch(CameraAccessException cae){
Log.e(TAG,cae.getMessage());
cae.printStackTrace();
}
}



所有你需要做的是:获取cameraid的列表其中相机ID零(0)是您要为其打开/关闭闪光灯的主要相机。只需将cameraID传递给setTochMode API,并使用布尔值将其打开或关闭。



请注意,这段代码只适用于OS 6,因此您需要检查设备操作系统,并根据您需要选择要为预棉花糖设备调用的API。



如果解决您的问题,请将此标记为解决方案。 p>

I have a problem regarding the camera in the most recent Marshmallow build, more specifically the flashlight. On any pre-Marshmallow version all I need to do to turn the flash on/off was the following:

private void turnFlashOn(final Camera camera, int flashLightDurationMs) {
    if (!isFlashOn()) {
        final List<String> supportedFlashModes = camera.getParameters().getSupportedFlashModes();
        if (supportedFlashModes != null && supportedFlashModes.contains(Camera.Parameters.FLASH_MODE_TORCH)) {
            mParams.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
            camera.setParameters(mParams);
        }
    }
}

and

private void turnFlashOff(Camera camera) {
    if (camera != null) {
        final List<String> supportedFlashModes = camera.getParameters().getSupportedFlashModes();
        if (supportedFlashModes != null && supportedFlashModes.contains(Camera.Parameters.FLASH_MODE_OFF)) {
            mParams.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
            camera.setParameters(mParams);
        }
    }
}

Unfortunately, Marshmallow devices began to crash in the wild. Somehow camera.getParameters() and camera.setParameters() began to fail with messages such as:

RuntimeException: getParameters failed (empty parameters)

RuntimeException: setParameters failed

I tried starting and stopping the preview before getting the parameters, which no longer throws errors. However the preview is not resumed when I call camera.startPreview().

I fear releasing the camera and reopening it is out of the question as this takes some seconds and would produce a bad experience.

Any suggestions on how to turn the flashlight on/off in Marshmallow reliably?

解决方案

Google has introduced torchmode in OS 6 (Android M).
if your purpose is only to turn on/off the flash, below code can help you with that:

private static void handleActionTurnOnFlashLight(Context context){
    try{

        CameraManager manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
        String[] list = manager.getCameraIdList();
        manager.setTorchMode(list[0], true);
    }
    catch (CameraAccessException cae){
        Log.e(TAG, cae.getMessage());
        cae.printStackTrace();
    }
}

private static void handleActionTurnOffFlashLight(Context context){
    try{
        CameraManager manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
        manager.setTorchMode(manager.getCameraIdList()[0], false);
    }
    catch (CameraAccessException cae){
        Log.e(TAG, cae.getMessage());
        cae.printStackTrace();
    }
}

All you have to do is: Get cameraid's list out of which camera ID zero(0) is your primary camera for which you want to turn flash on/off. Simply pass the cameraID to setTochMode API with boolean value for turning it on or off.

Do note that this piece of code will work only with OS 6, so you need to check for device OS and based upon that you need to select which API's to call for pre-marshmallow devices.

Kindly mark this as solution if it solves your problem.

这篇关于手电筒控制在棉花糖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆