How to ask webcam to auto focus with Unity3D

2019-04-27 03:34发布

Currently, I am working on some Augmented Reality mobile app with Unity3D. The performance is impacted by the image quality.

Is there some way to ask webcam to auto focus with Unity3D?

1条回答
聊天终结者
2楼-- · 2019-04-27 04:12

As far as I know it is not possible in pure Unity3D.

However, if you are developing this on Android, you can write a plugin in java, which sets autofocus and call it from Unity3D.

public void enableAutofocus() {
    camera = camera.open();
    Camera.Parameters parameters = camera.getParameters();
    List<String> focusModes = parameters.getSupportedFocusModes();
    if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
        parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
    }
    camera.setParameters(parameters);
}

And then, you have to call your class from Unity3D:

public class ExampleClass : MonoBehaviour {
    void Start() {
        AndroidJavaObject jo = new AndroidJavaObject("com.mypackage.Autofocus");
        jo.Call("enableAutofocus");
    }
}

You can find more info about creating Java plugins for Unity3D here.

查看更多
登录 后发表回答