ocr open Flash with Google Vision

ahmedAlie 161 Reputation points
2022-02-04T07:29:43.033+00:00

hi

Use a library Google Vision. When I open the text reading window, I can't turn on the flash at all to improve the quality of the image in the dark.

What is the method used to turn on the flash when reading texts??

i read this but nothing

https://stackoverflow.com/questions/35811411/accessing-autofocus-flash-with-google-vision-barcode-reader

all code //

  protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            cameraView = FindViewById<SurfaceView>(Resource.Id.surface_view);
            txtView = FindViewById<TextView>(Resource.Id.txtview);

            TextRecognizer txtRecognizer = new TextRecognizer.Builder(ApplicationContext).Build();
            if (!txtRecognizer.IsOperational)
            {
                Log.Error("Main Activity", "Detector dependencies are not yet available");
            }
            else
            {
                cameraSource = new CameraSource.Builder(ApplicationContext, txtRecognizer)
                    .SetFacing(CameraFacing.Back)
                    .SetRequestedPreviewSize(1280, 1024)
                    .SetRequestedFps(2.0f)
                    .SetAutoFocusEnabled(true)
                    .Build();

                cameraView.Holder.AddCallback(this);
                txtRecognizer.SetProcessor(this);
            }
        }

        public void SurfaceChanged(ISurfaceHolder holder, [GeneratedEnum] Format format, int width, int height)
        {

        }

        public void SurfaceCreated(ISurfaceHolder holder)
        {

            if (ActivityCompat.CheckSelfPermission(ApplicationContext, Manifest.Permission.Camera) != Android.Content.PM.Permission.Granted)
            {
                //Request permission
                ActivityCompat.RequestPermissions(this, new string[] {
                    Android.Manifest.Permission.Camera
                }, RequestCameraPermissionID);
                return;
            }

            cameraSource.Start(cameraView.Holder);
        }

        public void SurfaceDestroyed(ISurfaceHolder holder)
        {
            cameraSource.Stop();
        }
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,357 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 76,001 Reputation points Microsoft Vendor
    2022-02-07T06:04:27.477+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    Flashlight is not include in the Xamarin.GooglePlayServices.Vision API, Google implemented those features (CameraSource.Builder) in native android CameraSource.Builder setFlashMode.

    This feature request is opened like this thread: Support getCamera() instance in CameraSource.

    Here is two ways to enable the flash mode.

    1. Download this native android library from Google, binding this library. Here's a good article on how to get started: Binding a Java Library.
    2. Using Reflection. You can get the Camera Object from CameraSouce and add the flash parameter then set the updated parameters to the camera. private void Button1_Click(object sender, System.EventArgs e)
      {
      //call it to open the Flash
      setFlash(true);
      }
            public Android.Hardware.Camera getCameraObject(CameraSource _camSource)  
            {  
                Field[] cFields = _camSource.Class.GetDeclaredFields();  
                Android.Hardware.Camera _cam = null;  
                try  
                {  
                    foreach (Field item in cFields)  
                    {  
                        if (item.Type.CanonicalName == "android.hardware.Camera")  
                        {  
                            Console.WriteLine("Camera");  
                            item.Accessible = true;  
                            try  
                            {  
                                _cam = (Android.Hardware.Camera)item.Get(_camSource);  
                            }  
                            catch (Exception e)  
                            {  
      
                            }  
                        }  
                    }  
                }  
                catch (Exception e)  
                {  
      
                }  
                return _cam;  
            }  
            bool isTorch;  
            public void setFlash(bool isEnable)  
            {  
                try  
                {  
                    isTorch = !isEnable;  
                    Android.Hardware.Camera _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)  
                {  
      
                }  
            }  
      

    And do not forget to add android.permission.FLASHLIGHT permission in the AndroidManifest.xml.

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.