Strange behavior of the Android camera when the torch is turned on

I have the following Android code (written here in pseudocode):

mCamera = configAndInitialize(); //all I want to do before taking picture mCamera.startPreview(); mCamera.torchOn(); //setting parameters with flash mode torch onClick(){ mCamera.stopPreview(); mCamera.takePicture(); mCamera.torchOff(); } 

Sometimes (often when the phone was recently restarted and the camera was not used before this application), this code ends with error 100. The camera server died. If the camera was successfully photographed before it normally works.

I debugged it for a lot of time, and I found out that this works when I comment out the lines with the torch. I see how the torch works in both cases when shooting or not.

TorchOn Code:

 if(mCamera != null){ mCamera.stopPreview(); Camera.Parameters p = mCamera.getParameters(); List<String> supported = p.getSupportedFlashModes(); if (supported.contains(Camera.Parameters.FLASH_MODE_TORCH)) { p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH); } mCamera.setParameters(p); mCamera.startPreview(); } 

Is there a reason why shooting cannot work due to the torch? I noticed that this happened on Motorola Razr and Samsung Galaxy SIII.

I installed two versions of this application on my device (with a different name, etc.). And I do the following:

  • Restart device
  • Trial application with a torch
  • If the torch application is running at point 1.
  • Trial application without torch
  • Trial application with a torch

And the results are as follows:

  • An application without a torch always works
  • An application with a torch in about 80% of attempts does not work at point 2. (after restart)
  • App-with-torch always works at point 5. (after using the application without a torch)

My application starts to work even if I add torchOff () just before shooting.

+4
source share
2 answers

We could call it "Monty Python Dead Parrot Log.d answer": -P I would like for me to have a solution for you, although there are some suggestions. Heisenbugs are hard to catch.

Does the torch have an isOn () test?
Similarly (I don’t remember), does the camera have an isReady () test?

Can you tell from the magazines that the camera dies before, during, or after mCamera.torchOn () or .torchOff ()?

What happens if you extend the time interval between calls? This will not be used for a real application, but can help you monitor and track what is happening. Say something like this in pseudo code:

  try { // Log.d ("cam", "here 1") ; mCamera = configAndInitialize(); // Log.d ("cam", "here 2"); if ( mCamera.isReady() ) { // or isConfigured / initialized // Log.d ("cam", "here 2"); Camera.startPreview(); // Log.d ("cam", "here 2"); thisThread.setDelay (200); // millisecs. try even up to 2000 ms ! // Log.d ("cam", "here 4"); mCamera.torchOn(); // Log.d ("cam", "here 5"); thisThread.setDelay (200); // again up to 2000 ms // Log.d ("cam", "here 6"); } } catch (Exception e) { Log.d ("oops!", e.toString() ); } 

Another thing to keep track of or make sure the camera and torch are really ready before onClick can shoot. Sprinkle some delays like Log.d and take a look. You may need to call back (mCamera.isReady () and then enable onClick).

Another thing to do is see if you can dig out the source code of the camera (or torch) and GREP for error 100 - or is it a common android 100?

I am sure that you know very well how much happens when this camera lights up - it seems like hundreds of calls. Since some of these low-level elements are asynchronous (in any case, the camera is hardware), I suspect you are getting an NPE or an insufficiently initialized object. Not all NPE, etc. They fall into the trap, so he can simply die on one that would not be there if slow or synchronized sequences were used.

(HTH - I feel your pain, ari, I had to take a lot of cameras lately. Debugging on the Samsung SIII is prohibitively time consuming.)

[EDIT] You probably already found this link, but just in case:

How to enable camera flash in Android?

+2
source

I think this is related to every OEM implementation of the Android HAL (Hardware Abstraction Layer) camera. This problem also happened to me, I'm not sure, but I suspect that the HALs camera's torch mode only works in video capture, since where it is most often used. Try recording a video with a torch to test it.

One possible solution to the problem would be to set the camera's flash mode to FLASH_MODE_ON just before shooting, and then return to FLASH_MODE_TORCH after the picture is taken, if you need it again.

+1
source

Source: https://habr.com/ru/post/1498578/


All Articles