6

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?

Kao
  • 7,225
  • 9
  • 41
  • 65
flyzhao
  • 348
  • 4
  • 15
  • Hi flyzhao, Have you successfully done this? Have you forced the webcam or device camera to focus from within Unity? –  Jun 12 '17 at 13:05
  • 1
    @Joshua I am not working on it now. But u can find some android native plugins from asset store. It may resolve this problem. Also, i think kao 's answer is well. It would be the best method, if you know some native knowledge about android. – flyzhao Jun 14 '17 at 06:16

1 Answers1

6

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.

Kao
  • 7,225
  • 9
  • 41
  • 65
  • Hi Kao, Are you sure this is possible? Have you tried this yourself? Mine does not seem to be working... –  Jun 12 '17 at 13:08
  • Can you please help me re-write this snippet for the new `camera2`? I am really confused... I just need the `enableAutofocus` method to open the camera and set its focus. Thanks –  Jun 13 '17 at 07:36