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?

2
  • Hi flyzhao, Have you successfully done this? Have you forced the webcam or device camera to focus from within Unity?
    – user285372
    Jun 12, 2017 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, 2017 at 6:16

1 Answer 1

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.

2
  • Hi Kao, Are you sure this is possible? Have you tried this yourself? Mine does not seem to be working...
    – user285372
    Jun 12, 2017 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
    – user285372
    Jun 13, 2017 at 7:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.