Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ask webcam to auto focus with Unity3D

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?

like image 945
flyzhao Avatar asked Sep 29 '13 09:09

flyzhao


1 Answers

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.

like image 65
Kao Avatar answered Sep 18 '22 23:09

Kao