1

I'm using google vision to read QR tags. everything has be going fine on the phone that I was developing for. recently I was given an Galaxy Tab A as the target device. I cannot get the Tab A to auto focus close enough to read the qr tags.

I noticed in the camera app that it has a Macro setting. when I turn it on, it focuses up close and reads the tag just fine.

So... in Xamarin how do I access the camera object's parameters when I am using the google vision cameraSource?

I've tried the examples I've found here, and I guess I'm missing something, cause I can't make them work.

Thanks for any help.

update

Here's the only way I have been able to get this java code to convert, and it doesn't work. Obviously I'm doing something wrong...

  private static bool cameraFocus(CameraSource cameraSource, String focusMode)
        {
            Java.Lang.Reflect.Field[] declaredFields =        cameraSource.Class.GetDeclaredFields();

            foreach (Java.Lang.Reflect.Field field in declaredFields) { 
                if (field.GetType() == typeof(Android.Hardware.Camera)) { 
                    field.Accessible = true; 
                    try {
                        Android.Hardware.Camera camera = (Android.Hardware.Camera)field.Get(cameraSource);
                        if (camera != null) {
                            Android.Hardware.Camera.Parameters parameters = camera.GetParameters();
                                    parameters.FocusMode = Android.Hardware.Camera.Parameters.FocusModeMacro;
                            camera.SetParameters(parameters); 
                            return true; 
                        } 

                        return false; 
                    } catch  {
                
                    } 

                    break; 
                } 
            } 

            return false; 
        }
Community
  • 1
  • 1
Jim
  • 11
  • 2
  • Jim, I checked your Xamarin.Android code; the following line is wrong: `if (field.GetType() == typeof(Android.Hardware.Camera)) { ` it should be: `if (field.Type.Name == "android.hardware.Camera")` The reason is that Java reflection returns Java objects, not the proper C# wrapper objects (like Android.Hardware.Camera) – vividos Jul 19 '18 at 08:35

1 Answers1

0

Please read this, look cameraFocus method:

private static boolean cameraFocus(@NonNull CameraSource cameraSource, @NonNull String focusMode) {
    Field[] declaredFields = CameraSource.class.getDeclaredFields();

    for (Field field : declaredFields) { 
        if (field.getType() == Camera.class) { 
            field.setAccessible(true); 
            try { 
                Camera camera = (Camera) field.get(cameraSource);
                if (camera != null) { 
                    Camera.Parameters params = camera.getParameters(); 
                    params.setFocusMode(focusMode);
                    camera.setParameters(params); 
                    return true; 
                } 

                return false; 
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } 

            break; 
        } 
    } 

    return false; 
} 

You need use reflection to get your camera to focused.

Robbit
  • 4,300
  • 1
  • 13
  • 29
  • I've tried the reflection code examples found here, but I can never find the camera. it always returns null. I'll try your code though. is it written in java or c#? thanks for the help. – Jim Jun 07 '18 at 13:56
  • I uploaded my attempt to convert it above. NO luck. Any help you can give is greatly appreciated. – Jim Jun 07 '18 at 19:27