Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to set auto focus with ARCore camera?

I am working on an application where I need to put object using ARCore. And then I need to save the frames as an image.

So is there any way to set auto focus for ARCore camera?

like image 998
deeppandya Avatar asked Feb 21 '18 18:02

deeppandya


4 Answers

As of ARCore 1.4, you can enable or disable autofocus through your ARCore Session's Config:

Session session = new Session(context);
Config config = new Config(session);
if (enableAutoFocus) {
  config.setFocusMode(Config.FocusMode.AUTO);
} else {
  config.setFocusMode(Config.FocusMode.FIXED);
}
session.configure(config);

https://developers.google.com/ar/reference/java/com/google/ar/core/Config.html#setFocusMode(com.google.ar.core.Config.FocusMode)

like image 65
MrAlbean Avatar answered Oct 21 '22 16:10

MrAlbean


Here's what Google ARCore engineers say about Config.FocusMode:

public static final enum Config.FocusMode

Config.FocusMode enum selects the desired behaviour of the camera focus subsystem. Currently, the default focus mode is FIXED, but this default might change in the future. Note, on devices where ARCore does not support Auto Focus due to the use of a fixed focus camera, setting AUTO will be ignored. See the ARCore Supported Devices page for a list of affected devices.

For optimal AR tracking performance, use the focus mode provided by the default session config. While capturing pictures or video, use AUTO. For optimal AR tracking, revert to the default focus mode once auto focus behavior is no longer needed. If your app requires fixed focus camera, call setFocusMode(Config.FocusMode)(FIXED) before enabling the AR session. This will ensure that your app always uses fixed focus, even if the default camera config focus mode changes in a future release.

There are two values:

public static final Config.FocusMode AUTO
public static final Config.FocusMode FIXED

So, in Java code:

Config ar_session_config = session.getConfig();
ar_session_config.setFocusMode(Config.FocusMode.AUTO);
session.configure(ar_session_config);

Hope this helps.

like image 30
Andy Jazz Avatar answered Oct 21 '22 17:10

Andy Jazz


Old question, but I thought I'd update the answer with implementation in Sceneform. Sceneform SDK 1.4 added the ability to setup autofocus. I use an extension function to enable it.

Declare the relevant fields globally.

//The AR Core session
private var arSession: Session? = null
private var arConfig: Config? = null

and the extension function definition :

private fun Session.setupAutofocus() {

        //Create the config
        arConfig = Config(this)

        //Check if the configuration is set to fixed
        if (arConfig?.focusMode == Config.FocusMode.FIXED)
            arConfig?.focusMode = Config.FocusMode.AUTO

        //Sceneform requires that the ARCore session is configured to the UpdateMode LATEST_CAMERA_IMAGE. 
        //This is probably not required for just auto focus. I was updating the camera configuration as well
        arConfig?.updateMode = Config.UpdateMode.LATEST_CAMERA_IMAGE

        //Reconfigure the session
        configure(arConfig)

        //Setup the session with ARSceneView
        fragment.arSceneView.setupSession(this)

        //log out if the camera is in auto focus mode
        ARApplication.log("The camera is current in focus mode : ${config.focusMode.name}")
}

and a good place to call this is your activity's onResume

override fun onResume() {
    super.onResume()

    //Check if ARSession is null. If it is, instantiate it
    if(arSession == null) {
        arSession = Session(this@EdgeActivity)
        arSession?.setupAutofocus()
    }
}
like image 2
Clinkz Avatar answered Oct 21 '22 16:10

Clinkz


If the problem is in a Unity project, then change the CameraFocusMode parameter to Auto instead of Fixed.

Unity ARCoreConfig file settings

like image 1
GuruJeya Avatar answered Oct 21 '22 17:10

GuruJeya