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?
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)
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 isFIXED
, 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, settingAUTO
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, callsetFocusMode(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.
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()
}
}
If the problem is in a Unity project, then change the CameraFocusMode parameter to Auto instead of Fixed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With