0

I am testing an android application on a samsung gt i8260, a samsung s4 and a sony ericsson xperia mini st15i.

The application at some point of the code while on preview mode and autofocusing constantly (callback calls camera.autoFocus again every time) calls camera.cancelAutoFocus(), then sets some parameters about flashlight (in order to start or stop torch mode) and finally recalling camera.autoFocus.

Both S4 and Xperia work fine. But gt stops responding after calling camera.cancelAutoFocus which neither returns nor throws an exception. It just hangs.

The documentation for cancelAutoFocus:

Cancels any auto-focus function in progress. Whether or not auto-focus is currently in progress, this function will return the focus position to the default. If the camera does not support auto-focus, this is a no-op.

does not explains this behavior.

Removing cancelAutoFocus works for S4, and GT but not for Xperia which throws an exception on setParameters.

Has anyone face the same or any similar problem? How can I overcome this issue? Is it hardware specific or bug?

Stavros Zotalis
  • 718
  • 4
  • 24

2 Answers2

0

Though this doesn't explain your app hang, you should consider using continuous focus, i.e. Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE or FOCUS_MODE_CONTINUOUS_VIDEO.

http://developer.android.com/reference/android/hardware/Camera.Parameters.html#FOCUS_MODE_CONTINUOUS_PICTURE

Make sure to check if its supported by the hardware first with getSupportedFocusModes().

This will give you a smooth and continuous focus experience.

CSmith
  • 13,318
  • 3
  • 39
  • 42
  • I used FOCUS_MODE_CONTINUOUS_VIDEO which does not give the desired result. CONTINUOUS_PICTURE cannot be used due to API version constrain (9) – Stavros Zotalis Jan 14 '14 at 23:02
0

This is how I am doing and its working for all devices

  1. I am starting autofocus in surfaceChanged
  2. Start Flash Light if needed
  3. Removing in surfaceDestroyed method

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)
{
    if(mCamera==null)
        return;

    Camera.Parameters parameters = null;
    parameters = mCamera.getParameters();
    if (Holder.getSurface() == null)
    {    // preview surface does not exist
        //mCamera = null;
        return;
    }

    // Stopping the camera preview so as to set the new params
    try
    {
        mCamera.stopPreview();// why the application is crashing here
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

    try
    {
        mCamera.setParameters(parameters);
        mCamera.startPreview();

        //Check Whether device supports AutoFlsh, If you YES then Enable AutoFlash
        if (parameters.getSupportedFlashModes().contains(android.hardware.Camera.Parameters.FLASH_MODE_AUTO))
        {
            parameters.setFlashMode(Parameters.FLASH_MODE_AUTO);
        }

        if (parameters.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
            parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);//FOCUS_MODE_CONTINUOUS_VIDEO
        }
        else if (parameters.getSupportedFocusModes().contains(android.hardware.Camera.Parameters.FOCUS_MODE_AUTO))
        {
            mCamera.autoFocus(myAutoFocusCallback);
            //parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
        }
    }
    catch(Exception e1)
    {
        e1.printStackTrace();
    }   
}


public void surfaceDestroyed(SurfaceHolder holder) 
{
    try
    {
        mCamera.cancelAutoFocus();
        mCamera.stopPreview();      
        mCamera.release();
        mCamera = null;
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}

// --------------- AutoFocusCallback methods implementations ----------//
AutoFocusCallback myAutoFocusCallback = new AutoFocusCallback()
{
   @Override
   public void onAutoFocus(boolean arg0, Camera arg1) 
   {                   
       isAutofoucsed =true;
   }
};
swiftBoy
  • 35,607
  • 26
  • 136
  • 135
  • The thing is that I want to turn the torch on while the camera is previewing. As far as i know in some devices (xperia mini) if you try to set the parameters to use the torch while the camera is autofocusing the app crashes. So i try to cancelAutofocus on a random time while autofocusing. On my samsung devices not calling cancelAutofocus devices does not affect the setting of parameters... – Stavros Zotalis Jan 15 '14 at 18:22
  • @Blim Please check now I have added Flash light as well, btw with this code snip I have autofocus also when I move camera to dark object it starts flash light while previewing. let me know still you have trouble in this. – swiftBoy Jan 16 '14 at 04:39