简体   繁体   English

用不同的模式在android中打开手电筒

[英]Opening flashlight in android with different modes

Will the code as below work on android devices like Motorola razor who doesn't support torch? 以下代码是否适用于不支持火炬的摩托罗拉剃刀等Android设备? Can someone please be kind enough to test if has any phone like that and please tell me. 如果有任何类似的手机,有人可以请你测试,请告诉我。

Thanks in advance!! 提前致谢!!

       if (!isFlashOn) {
           if (camera == null || params == null) {
               return;
             }     
           List<String> flashModes = params.getSupportedFlashModes();
           if(flashModes.contains(Parameters.FLASH_MODE_TORCH)){

          try { 
                   params = camera.getParameters();
                   params.setFlashMode(Parameters.FLASH_MODE_TORCH);
                   camera.setParameters(params);
                   camera.startPreview();
                   toggleButtonImage();
                   isFlashOn = true;

           }catch (RuntimeException e) {
            }

           }

           else {
                    params = camera.getParameters();
                    params.setFlashMode(Parameters.FLASH_MODE_ON);
                    camera.setParameters(params);
                    camera.startPreview();
                    toggleButtonImage();
                    isFlashOn = true;
           }
           toggleButtonImage();
           isFlashOn = true;

       }

   }

PS Should i additionally add something like: PS我应该另外添加如下内容:

if (flashModes.contains(android.hardware.Camera.Parameters.FLASH_MODE_AUTO))
           {
                params.setFlashMode(Parameters.FLASH_MODE_AUTO);
                camera.setParameters(params);
                camera.startPreview();
           }

It works on Motorola g and galaxy S4(torch supported) 它适用于摩托罗拉g和galaxy S4(支持火炬)

You can open flashlight in different modes if different flash modes are supported in your device. 如果设备支持不同的闪光模式,则可以在不同模式下打开手电筒。 You can get the code from this open source camera code. 您可以从此开源摄像头代码中获取代码。 OPenCamera 开放式摄像头

Yes, if You check that device supports torch. 是的,如果您检查该设备是否支持火炬。 But You can encounter a Device-Specific issue that is very prevalent in the Android. 但是您可能会遇到Android中非常普遍的特定于设备的问题。 You can find more information in this post. 您可以在这篇文章中找到更多信息。

you should check supported flash modes to not have an exception, setFlashMode method checks for supported modes but checking with this method is helpful to set flash mode button or view on UI 你应该检查支持的闪存模式没有异常,setFlashMode方法检查支持的模式,但用这种方法检查有助于在UI上设置闪存模式按钮或视图

public List<String> getSupportedFlashModes() {
    return params.getSupportedFlashModes();
}

Sum of all Flash modes are: 所有Flash模式的总和是:

Camera.Parameters.FLASH_MODE_AUTO, Camera.Parameters.FLASH_MODE_OFF, Camera.Parameters.FLASH_MODE_ON, Camera.Parameters.FLASH_MODE_RED_EYE, Camera.Parameters.FLASH_MODE_TORCH

But some or any of these flash modes may not be available in your device, check before using. 但是您的设备中可能无法使用某些或任何这些闪光模式,请在使用前进行检查。 After selecting from flash modes you can set the flash modes using this method 从闪光模式中选择后,您可以使用此方法设置闪光模式

public synchronized void setFlashMode(String flashMode) {
        Camera.Parameters params = mCamera.getParameters();
        if (cameraId == Camera.CameraInfo.CAMERA_FACING_BACK && params.getSupportedFlashModes() != null
            && params.getSupportedFlashModes().contains(flashMode)) {
        params.setFlashMode(flashMode);
        mCamera.setParameters(params);
    }
}

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

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