3

I have been trying to implement the flashlight/torch feature of the camera using the GooglePlay Services Vision API (using Nuget from Visual Studio) for the past few days without success. I have noticed that there is a GitHub implementation of this API which has such functionality but that is only available to Java users. I was wondering if there is anything related to C# Xamarin users.

The Camera object is not made available on this API therefore I am not able to alter the Camera parameters needed to activate the flashlight.

I would like to be sure if that functionality is not available so I don't waste more time over this. It just might be the case that the Xamarin developers have not attended to this functionality and they might in a near future.


UPDATE

https://github.com/googlesamples/android-vision/blob/master/visionSamples/barcode-reader/app/src/main/java/com/google/android/gms/samples/vision/barcodereader/BarcodeCaptureActivity.java

In there you can see that on line 214 we have such method call:

mCameraSource = builder.setFlashMode(useFlash ? Camera.Parameters.FLASH_MODE_TORCH : null).build();

SetFlashMode is not a method of the CameraSource in Nuget, but it is on the GitHub (open source version).

Gustavo Baiocchi Costa
  • 1,379
  • 3
  • 16
  • 34
  • Do you have a link to the Google Vision API for flashlight support? I do not think that is an exposed in the Vision API....? If you have a link to the Java source that does it, is that source accessing the Vision API or is it doing it via OS API calls? – SushiHangover Feb 23 '17 at 15:00
  • I don't know what you mean but here is the github I am talking about: https://github.com/googlesamples/android-vision. I edited my question to address your question. – Gustavo Baiocchi Costa Feb 23 '17 at 15:29
  • 1
    Flashlight is **not** include in the Vision API, Google implemented those features (`CameraSource.Builder`) within the sample code as additions to the Vision API in order to take images and feed the barcode decoder as a demo, you would need port that Java to C#: https://github.com/googlesamples/android-vision/blob/master/visionSamples/barcode-reader/app/src/main/java/com/google/android/gms/samples/vision/barcodereader/ui/camera/CameraSource.java#L207 – SushiHangover Feb 23 '17 at 15:55
  • I see. I will try to implement that. Hopefully I will manage to get it to work then. – Gustavo Baiocchi Costa Feb 23 '17 at 16:48
  • @GustavoBaiocchiCosta Did you find the solution? – Manikandan Selvanathan Jul 16 '17 at 04:23
  • @ManiKandanSelvanathan are you working with xamarin or android? if android just use the github version and you are sorted. But answering: NO, it did not work for me. I think the only solution would be to create a java binding, I found it an overkill and dropped the flash function off the app when using this library. – Gustavo Baiocchi Costa Jul 17 '17 at 10:44
  • @GustavoBaiocchiCosta Yes, I'm using Xamarin. OMG!! We need flash functionality. Lemme try java binding. – Manikandan Selvanathan Jul 17 '17 at 12:32
  • @ManiKandanSelvanathan Good luck with that, you gonna need it :D – Gustavo Baiocchi Costa Jul 17 '17 at 12:55
  • @GustavoBaiocchiCosta Found the solution. See my answer – Manikandan Selvanathan Jul 20 '17 at 06:00

2 Answers2

1

Xamarin Vision Library Didn't expose the method to set Flash Mode.

WorkAround. Using Reflection. You can get the Camera Object from CameraSouce and add the flash parameter then set the updated parameters to the camera.

This should be called after surfaceview has been created

Code

  public Camera getCameraObject (CameraSource _camSource)
        {
            Field [] cFields = _camSource.Class.GetDeclaredFields ();
            Camera _cam = null;
            try {
                foreach (Field item in cFields) {
                    if (item.Name.Equals ("zzbNN")) {
                        Console.WriteLine ("Camera");
                        item.Accessible = true;
                        try {
                            _cam = (Camera)item.Get (_camSource);
                        } catch (Exception e) {
                            Logger.LogException (this, e);
                        }
                    }
                }
            } catch (Exception e) {
                Logger.LogException (this, e);
            }
            return _cam;
        }

        public void setFlash (bool isEnable)
        {
            try {
                isTorch = !isEnable;
                var _cam = getCameraObject (mCameraSource);
                if (_cam == null) return;
                var _pareMeters = _cam.GetParameters ();
                var _listOfSuppo = _cam.GetParameters ().SupportedFlashModes;
                _pareMeters.FlashMode = isTorch ? _listOfSuppo [0] : _listOfSuppo [3];
                _cam.SetParameters (_pareMeters);
            } catch (Exception e) {
                Logger.LogException (this, e);
            }
        }
  • Thanks for posting, I am working in a different project at moment. I will get back to it soon enough, if it works I will mark your answer as accepted, thanks! I remember when I was doing something similar, you can't get the camera object if you already is using it to display the camera preview. Gotta test that! – Gustavo Baiocchi Costa Jul 20 '17 at 08:01
  • Yeah. Exactly what you said. Good Luck! – Manikandan Selvanathan Jul 20 '17 at 08:33
  • returns null for me, even after the surface has been created – Le-roy Staines May 23 '21 at 09:37
  • NULL CAMERA SOLVED: As the version I'm using hasn't obfuscated the camera property name to "zzbNN", the better solution is to instead check item.Type.CanonicalName == "android.hardware.Camera". – Le-roy Staines May 23 '21 at 09:44
0

Basically, anything you can do with Android can be done with Xamarin.Android. All the underlying APIs area available.

Since you have existing Java code, you can create a binding project that enables you to call the code from your Xamarin.Android project. Here's a good article on how to get started: Binding a Java Library

On the other hand, I don't think you need a library to do what you want to. If you only want torch/flashlight functionality, you just need to adapt the Java code from this answer to work in Xamarin.Android with C#.

Community
  • 1
  • 1
Timo Salomäki
  • 7,099
  • 3
  • 25
  • 40
  • I do use the camera as well. So if I use the flashlight the camera become unavailable. I will have a look at binding a java library. I will come back to you on this, thanks. Just don't understand why the nuget version does not support flashlight paramenters :( – Gustavo Baiocchi Costa Feb 23 '17 at 14:38
  • The Nuget version of Google's Vision API binds just the Vision API Java `.jars`. That other functionality, such as the Camera Builder, in those Google samples were written in Java and access the native OS APIs to provide full demos of what the Vision API can do. – SushiHangover Feb 23 '17 at 16:01