我正在开发一个应用程序,我需要使用ARCore放置对象。然后我需要将帧保存为图像。
那么有没有办法为ARCore相机设置自动对焦?
从ARCore 1.4开始,您可以通过ARCore Session的配置启用或禁用自动对焦:
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);
老问题,但我想我会在Sceneform
中实现更新答案。 Sceneform SDK 1.4增加了设置自动对焦的功能。我使用扩展功能来启用它。
全局声明相关字段。
//The AR Core session
private var arSession: Session? = null
private var arConfig: Config? = null
和扩展函数定义:
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}")
}
并且这是一个好地方,这是你的活动的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()
}
}
以下是Google ARCore工程师对Config.FocusMode
的评价:
public static final enum Config.FocusMode
Config.FocusMode
枚举选择相机焦点子系统的所需行为。目前,默认焦点模式是FIXED
,但此默认值可能在将来发生变化。请注意,在由于使用固定焦距相机而导致ARCore不支持自动对焦的设备上,将忽略设置AUTO
。有关受影响设备的列表,请参阅“ARCore支持的设备”页面。要获得最佳AR跟踪性能,请使用默认会话配置提供的焦点模式。在捕捉图片或视频时,请使用
AUTO
。要获得最佳AR跟踪,请在不再需要自动对焦行为后恢复默认对焦模式。如果您的应用需要固定焦距相机,请在启用AR会话之前调用setFocusMode(Config.FocusMode)(FIXED)
。这将确保您的应用始终使用固定焦点,即使默认的相机配置焦点模式在将来的版本中更改。
有两个值:
public static final Config.FocusMode AUTO
public static final Config.FocusMode FIXED
所以,在Java代码中:
Config ar_session_config = session.getConfig();
ar_session_config.setFocusMode(Config.FocusMode.AUTO);
session.configure(ar_session_config);
希望这可以帮助。