• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Manually setting auto-focus using camera2

Using WebCamTexture in your Unity3D program (as a way of cross-platform accessing the camera of devices) unfortunately messes with your Android device focus mode, so it requires a manual fix, hence my trying to write a small Android plug-in for Unity3D, in order to set my Android device camera auto-focus manually for scanning and character recognition (i.e. constant sharp focus).

I am, however, finding it difficult to get used to the new camera2 API. I need a little help turning the old Camera API code (commented out bit at the bottom) into the new camera2 compatible program, just so I can then turn this Android program into a plug-in and then call it from within a Unity3D C# program.

My variable declarations are obviously fine, but the other 3 lines are all underlined red as error. Could someone please help me get this right?

Code:
package com.example.ocrfocusunityplugin;

import android.hardware.camera2.*;
import java.util.List;

public class OcrFocusPluginClass
{
    public void EnableAutofocus()
    {
        String cameraId;                // camera ID -> 0 for Back and 1 for Front
        CameraDevice camera;
        CameraManager manager;
        CameraCaptureSession session;
        CameraDevice.StateCallback callback;

        cameraId = manager.getCameraIdList()[0];
        manager.openCamera(cameraId, callback, null);
        CameraCharacteristics.get(CameraCharacteristics.CONTROL_AF_AVAILABLE_MODES);

        /*
            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);
        */
    }
}
 
I hope you have all the device systems original format backed up somewhere became messing around with the device platform can be irreversible if you're not sure about what you're doing. All of what you asked would fall under Device Development Programming. I would try getting information such as that from Android Studio software.
 
Upvote 0
I hope you have all the device systems original format backed up somewhere became messing around with the device platform can be irreversible if you're not sure about what you're doing. All of what you asked would fall under Device Development Programming. I would try getting information such as that from Android Studio software.

I am not sure if I understand you...
What I am trying to do is achieve the suggested solution in this post:
https://stackoverflow.com/questions...o-focus-with-unity3d#comment76064590_19076316
setting the auto focus of a galaxy tablet via code.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones