Opening flashlight in android with different modes

♀尐吖头ヾ 提交于 2019-12-06 05:52:33

问题


Will the code as below work on android devices like Motorola razor who doesn't support torch? 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;

       }

   }

P.S Should i additionally add something like:

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)


回答1:


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




回答2:


Yes, if You check that device supports torch. But You can encounter a Device-Specific issue that is very prevalent in the Android. You can find more information in this post.




回答3:


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

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

Sum of all Flash modes are:

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


来源:https://stackoverflow.com/questions/21674345/opening-flashlight-in-android-with-different-modes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!