Switching on the camera Flash light dynamically in android
I am doing a barcode scanning app in android. In that, I have to turn on the camera flash light dynamically and I am doing this by using the following code
mCamera = Camera.open();
Camera.Parameters params = mCamera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(params);
mCamera.startPreview();
But it's showing an Exception like:
ERROR/AndroidRuntime(17291): java.lang.RuntimeException: Fail to connect to camera service
at line
mCamera 开发者_StackOverflow= Camera.open();
I think the exception is because while I am scanning the barcode the camera is already in open mode and here I am trying to open the camera again.
Is there any way to turn on the Flash light without open the camera again ? and how?
You can reuse your existing camera object and just setParameters on that. Note that not all devices support flash mode torch. Also some, like the fascinate/epic (who actually use flash mode on) cannot change the state without disconnecting and reconnecting to the camera.
You are accessing camera settings and camera. Check if you have a android.permission.CAMERA
in AndroidMainfest.xml uses-permissions.
Have you used surface view to turn on the flash light, which device are you using as some of the devices do require surface view to make flash run in it.
Here's a link!
精彩评论