1

I'm trying to write an app with an integrated barcode scanner. I followed this tutorial: https://www.c-sharpcorner.com/article/xamarin-android-qr-code-reader-by-mobile-camera/

The scan works fine and very fast (before I used ZXing.Net.Mobile and it is horrible slow). Now I need some help to integrate that the app only detects one barcode when the user presses a button and not the whole time. Maybe a delay would solve the problem too.

protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            // Set our view from the "main" layout resource  
            SetContentView(Resource.Layout.ScannerTest);
            surfaceView = FindViewById<SurfaceView>(Resource.Id.cameraView);
            txtResult = FindViewById<TextView>(Resource.Id.txtResult);
            barcodeDetector = new BarcodeDetector.Builder(this)
                .SetBarcodeFormats(BarcodeFormat.Code128 | BarcodeFormat.Ean13 | BarcodeFormat.QrCode)
                .Build();
            cameraSource = new CameraSource
                .Builder(this, barcodeDetector)
                .SetRequestedPreviewSize(320, 480)
                .SetAutoFocusEnabled(true)
                .Build();

            surfaceView.Click += StartScanning;
            surfaceView.Holder.AddCallback(this);
            //barcodeDetector.SetProcessor(this);
        }
private void StartScanning(object sender, EventArgs e)
{
        barcodeDetector.SetProcessor(this);
}
public void ReceiveDetections(Detections detections)
        {
            SparseArray qrcodes = detections.DetectedItems;
            if (qrcodes.Size() != 0)
            {
                txtResult.Post(() => {
                    //Vibrator vibrator = (Vibrator)GetSystemService(Context.VibratorService);
                    //vibrator.Vibrate(1000);
                    txtResult.Text = ((Barcode)qrcodes.ValueAt(0)).RawValue;
                });
            }
        }

At the moment the user presses the SurfaceView and the scanner starts and never stops.

Is it possible, that it just scans one after pressing the "button"?

r3d007

r3d007
  • 43
  • 9
  • Have a look at this [post](https://stackoverflow.com/questions/39641111/android-camerasource-stop-causing-app-to-freeze) may help. – nevermore Mar 29 '19 at 06:06

2 Answers2

1

1 Uncomment this line in OnCreate method

barcodeDetector.SetProcessor(this);

2 Remove or comment this line from SurfaceCreated and OnRequestPermissionsResult methods

cameraSource.Start(surfaceView.Holder);

3 Your StartScanning method should call the Start

private void StartScanning(object sender, EventArgs e)
{
    cameraSource.Start(surfaceView.Holder);
}

4 Once you read and validate a code, stop the scanner

public void ReceiveDetections(Detections detections)
{
    SparseArray qrcodes = detections.DetectedItems;
    if (qrcodes.Size() != 0)
    {
        txtResult.Post(() => {
            //Vibrator vibrator = (Vibrator)GetSystemService(Context.VibratorService);
            //vibrator.Vibrate(1000);
            txtResult.Text = ((Barcode)qrcodes.ValueAt(0)).RawValue;
        });

        using (var h = new Handler (Looper.MainLooper))
        h.Post (() => {
            cameraSource.Stop();
        });

    }
}

To prevent crashes, consider also to hide or disable the button until you get the camera permissions, and when the scanner is already started.

Daniel Brughera
  • 1,641
  • 1
  • 7
  • 14
  • Tried it this way and the first time i click the button, it scans and stops scanning after it found a result. But when I click the button again, it doesn't start scanning and no other button is working. Not even the Android Back button is working then. – r3d007 Mar 28 '19 at 14:41
  • 1
    @r3d007 Check the update, the crash is caused when it stops, not when you try to start again, the code inside the receiveDetections method is running on a different thread so, you need to post the cameraSource.Stop() from a handler – Daniel Brughera Mar 28 '19 at 15:09
  • I my case i have called multiple api into the txtResult.Post(()={}) method.The problem i'm having is ReceiveDetections method is calling multiple times and my API is also calling multiple times. how can i prevent that.any kind of help is highly appropriated. – rykamol Aug 23 '20 at 05:11
  • @DanielBrughera please have a look. here is my problem https://stackoverflow.com/questions/63535237/receivedetections-method-is-calling-multiple-times?noredirect=1#comment112351021_63535237 – rykamol Aug 23 '20 at 05:15
0

You need add this after scan process triggered. "-" operator must be added to prevent non-stop working.You plugged-in event in this line

//adds the handler
surfaceView.Click += StartScanning;

after that you need this.

 // removes the handler
surfaceView.Click -= StartScanning;

Also look here

Batuhan
  • 1,521
  • 12
  • 29