Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IOS] Scanner starts slow after 4/5 times. #71

Closed
rogierrietdijk opened this issue Oct 9, 2013 · 13 comments
Closed

[IOS] Scanner starts slow after 4/5 times. #71

rogierrietdijk opened this issue Oct 9, 2013 · 13 comments

Comments

@rogierrietdijk
Copy link

The scanner starts slow after 4/5 times.

While i was debugging the app with de current source i see dat the addinput and addoutput by de avcapturesession gives the problems.

            var scanner = new ZXing.Mobile.MobileBarcodeScanner ();
        scanner.UseCustomOverlay = false;


        ZXing.Mobile.MobileBarcodeScanningOptions options = new ZXing.Mobile.MobileBarcodeScanningOptions();
        options.TryHarder = true;
        options.PossibleFormats.Add(ZXing.BarcodeFormat.QR_CODE);

        scanner.Scan(options).ContinueWith(t => {  

            if (t.Result != null) {
                Console.WriteLine ("Scanned Barcode: " + t.Result.Text);
                }
            } else {

            }
        });
@Redth
Copy link
Owner

Redth commented Oct 9, 2013

Can you expand on what you mean by 'AddInput and AddOutput gives the problems'? Not sure what you mean. Also would appreciate you giving me a chance to help make this better before leaving such a negative review on the component store.

@rogierrietdijk
Copy link
Author

Thnx for your quick response.

I dont now :-) Here below is the code.

-- session.AddOutput (metadataOutput); and session.AddInput (input);

bool SetupCaptureSession ()
{
// configure the capture session for low resolution, change this if your code
// can cope with more data or volume
session = new AVCaptureSession () {
SessionPreset = AVCaptureSession.Preset640x480,
};

        // create a device input and attach it to the session

// var captureDevice = AVCaptureDevice.DefaultDeviceWithMediaType (AVMediaType.Video);
AVCaptureDevice captureDevice = null;
var devices = AVCaptureDevice.DevicesWithMediaType(AVMediaType.Video);
foreach (var device in devices)
{
captureDevice = device;
if (options.UseFrontCameraIfAvailable.HasValue &&
options.UseFrontCameraIfAvailable.Value &&
device.Position == AVCaptureDevicePosition.Front)

                break; //Front camera successfully set
            else if (device.Position == AVCaptureDevicePosition.Back && (!options.UseFrontCameraIfAvailable.HasValue || !options.UseFrontCameraIfAvailable.Value))
                break; //Back camera succesfully set
        }
        if (captureDevice == null){
            Console.WriteLine ("No captureDevice - this won't work on the simulator, try a physical device");
            if (overlayView != null)
            {
                this.AddSubview (overlayView);
                this.BringSubviewToFront (overlayView);
            }
            return false;
        }

        var input = AVCaptureDeviceInput.FromDevice (captureDevice);
        if (input == null){
            Console.WriteLine ("No input - this won't work on the simulator, try a physical device");
            if (overlayView != null)
            {
                this.AddSubview (overlayView);
                this.BringSubviewToFront (overlayView);
            }               
            return false;
        }
        else
            session.AddInput (input);


        foundResult = false;
        //Detect barcodes with built in avcapture stuff
        AVCaptureMetadataOutput metadataOutput = new AVCaptureMetadataOutput();

        var dg = new CaptureDelegate (metaDataObjects =>
            {
                if (foundResult)
                    return;

                //Console.WriteLine("Found MetaData Objects");

                var mdo = metaDataObjects.FirstOrDefault();

                if (mdo == null)
                    return;

                var readableObj = mdo as AVMetadataMachineReadableCodeObject;

                if (readableObj == null)
                    return;

                foundResult = true;

                //Console.WriteLine("Barcode: " + readableObj.StringValue);

                var zxingFormat = ZXingBarcodeFormatFromAVCaptureBarcodeFormat(readableObj.Type.ToString());

                var rs = new ZXing.Result(readableObj.StringValue, null, null, zxingFormat);

                resultCallback(rs);
            });

        metadataOutput.SetDelegate (dg, MonoTouch.CoreFoundation.DispatchQueue.MainQueue);
        session.AddOutput (metadataOutput);

        //Setup barcode formats
        if (ScanningOptions.PossibleFormats != null && ScanningOptions.PossibleFormats.Count > 0)
        {
            var formats = new List<string> ();

            foreach (var f in ScanningOptions.PossibleFormats)
                formats.AddRange (AVCaptureBarcodeFormatFromZXingBarcodeFormat (f));

            metadataOutput.MetadataObjectTypes = (from f in formats.Distinct () select new NSString(f)).ToArray();
        }
        else
            metadataOutput.MetadataObjectTypes = metadataOutput.AvailableMetadataObjectTypes;





        previewLayer = new AVCaptureVideoPreviewLayer(session);

        //Framerate set here (15 fps)
        if (previewLayer.RespondsToSelector(new Selector("connection")))
            previewLayer.Connection.VideoMinFrameDuration = new CMTime(1, 10);

        previewLayer.LayerVideoGravity = AVLayerVideoGravity.ResizeAspectFill;
        previewLayer.Frame = this.Frame;
        previewLayer.Position = new PointF(this.Layer.Bounds.Width / 2, (this.Layer.Bounds.Height / 2));

        layerView = new UIView(this.Frame);
        layerView.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
        layerView.Layer.AddSublayer(previewLayer);

        this.AddSubview(layerView);

        ResizePreview(UIApplication.SharedApplication.StatusBarOrientation);

        if (overlayView != null)
        {
            this.AddSubview (overlayView);
            this.BringSubviewToFront (overlayView);

            //overlayView.LayoutSubviews ();
        }

        session.StartRunning ();

        Console.WriteLine ("RUNNING!!!");



        //output.AlwaysDiscardsLateVideoFrames = true;


        Console.WriteLine("SetupCamera Finished");

        //session.AddOutput (output);
        //session.StartRunning ();


        if (captureDevice.IsFocusModeSupported(AVCaptureFocusMode.ModeContinuousAutoFocus))
        {
            NSError err = null;
            if (captureDevice.LockForConfiguration(out err))
            {
                captureDevice.FocusMode = AVCaptureFocusMode.ModeContinuousAutoFocus;

                if (captureDevice.FocusPointOfInterestSupported)
                    captureDevice.FocusPointOfInterest = new PointF(0.5f, 0.5f);

                captureDevice.UnlockForConfiguration();
            }
            else
                Console.WriteLine("Failed to Lock for Config: " + err.Description);
        }

        return true;
    }

@rogierrietdijk
Copy link
Author

This is in the ACCaptureScannerView.cs file from the MonoTouch.Sample app

@Redth
Copy link
Owner

Redth commented Oct 9, 2013

It would be more helpful if you could share some results of profiling: http://docs.xamarin.com/guides/ios/deployment%2C_testing%2C_and_metrics/using_instruments_to_detect_native_leaks_using_markheap

@Redth
Copy link
Owner

Redth commented Oct 9, 2013

For example, are there memory leaks you're encountering at the point it slows down, or generally high usage of memory? etc.

@rogierrietdijk
Copy link
Author

I found it. the fix is to remove de input and output from the session in the StopScanning method.

session.RemoveOutput (session.Outputs [0]);
session.RemoveInput (session.Inputs [0]);

@Redth
Copy link
Owner

Redth commented Oct 9, 2013

Great, I'll give this a try!

@djlsw
Copy link

djlsw commented Nov 10, 2013

Hi All,
I hope this helps someone else out there. With version 1.4.0, I was experiencing this same issue with the scanner locking up after 4-5 scans when using the OS7 built in AVCaptureSession MetadataObject barcode scanning.

I fixed my problem by adding the same code for suggested fix #71 to the StopScanning method in the AVCaptureView.cs file (found at 3f4267e). Once I did that, my problem went away.

Once again, thanks for the efforts on this project. They are greatly appreciated.

@brunnurs
Copy link

brunnurs commented Feb 3, 2014

@Redth First, thanks for the effort you guys put in this project! It makes life for startups like we are a lot easier.

Unfortunately, I still experience the effect, that after a few scans, the scanner opens very slow.

Here some figures:

On 2nd opening:

2014-02-03 17:09:13.967 DralloTouch[618:60b] Starting to scan... 2014-02-03 17:09:14.017 DralloTouch[618:60b] ZXingScannerView.Setup() took 24.003 ms. 2014-02-03 17:09:14.023 DralloTouch[618:60b] StartScanning 2014-02-03 17:09:14.155 DralloTouch[618:e007] Decode Time: 1884 ms 2014-02-03 17:09:16.030 DralloTouch[618:60b] PERF: Alloc AVCaptureVideoPreviewLayer took 33.345 ms. 2014-02-03 17:09:16.049 DralloTouch[618:60b] PERF: ActiveVideoMinFrameDuration Took 2.5936 ms 2014-02-03 17:09:16.060 DralloTouch[618:60b] PERF: Setting up layers took 7.6259 ms 2014-02-03 17:09:18.485 DralloTouch[618:60b] PERF: session.StartRunning() took 2421.1751 ms 2014-02-03 17:09:18.498 DralloTouch[618:60b] PERF: SetupCamera Finished. Took 3.5815 ms. Thread started: #27 2014-02-03 17:09:19.280 DralloTouch[618:60b] PERF: Setup Focus in 3.6367 ms. 2014-02-03 17:09:19.288 DralloTouch[618:60b] PERF: StartScanning() Took 5294.4142 ms.

On 3rd opening:

2014-02-03 17:10:16.337 DralloTouch[618:60b] Starting to scan... 2014-02-03 17:10:16.402 DralloTouch[618:60b] ZXingScannerView.Setup() took 23.532 ms. 2014-02-03 17:10:16.415 DralloTouch[618:60b] StartScanning 2014-02-03 17:10:16.715 DralloTouch[618:ea27] Decode Time: 1481 ms 2014-02-03 17:10:27.353 DralloTouch[618:60b] PERF: Alloc AVCaptureVideoPreviewLayer took 91.823 ms. 2014-02-03 17:10:27.363 DralloTouch[618:60b] PERF: ActiveVideoMinFrameDuration Took 2.3172 ms 2014-02-03 17:10:27.386 DralloTouch[618:60b] PERF: Setting up layers took 18.7959 ms 2014-02-03 17:10:38.721 DralloTouch[618:60b] PERF: session.StartRunning() took 11325.0526 ms 2014-02-03 17:10:38.735 DralloTouch[618:60b] PERF: SetupCamera Finished. Took 2.7098 ms. Thread started: #36 2014-02-03 17:10:39.517 DralloTouch[618:60b] PERF: Setup Focus in 2.2247 ms. 2014-02-03 17:10:39.553 DralloTouch[618:60b] PERF: StartScanning() Took 23174.767 ms.

I'm using zxing.monotouch.dll V 0.12.0.0 and ZXing.Net.Mobile V 1.4.1.0 and I see that the commit 3f4267e is already included in this version.

So I'm a little baffled. Thanks for any help!

@brunnurs
Copy link

brunnurs commented Feb 3, 2014

Update: I included the official V 1.4.0 version in the app and everything is working fine...

@mapo80
Copy link

mapo80 commented Mar 10, 2014

Hi,

I've the same problem. After 4/5 times scanner starts very slowly. I'm using v.1.4.2 and I'm using an iPhone 5 on iOS 7.

Any solution? I've seen that fix suggested is included in verso 1.4.2

2014-03-10 19:14:12.525 PhotoShare[1053:60b] PERF: Setup Focus in 1,4683 ms.
2014-03-10 19:14:12.527 PhotoShare[1053:60b] PERF: StartScanning() Took 2772,0947 ms.
2014-03-10 19:14:13.670 PhotoShare[1053:60b] Stopping scan...
2014-03-10 19:14:15.424 PhotoShare[1053:7f03] Connected to the server
2014-03-10 19:14:15.936 PhotoShare[1053:60b] Starting to scan...
2014-03-10 19:14:15.944 PhotoShare[1053:60b] ZXingScannerView.Setup() took 5,282 ms.
2014-03-10 19:14:15.945 PhotoShare[1053:60b] StartScanning
2014-03-10 19:14:25.537 PhotoShare[1053:60b] PERF: Alloc AVCaptureVideoPreviewLayer took 11,132 ms.
2014-03-10 19:14:25.541 PhotoShare[1053:60b] PERF: ActiveVideoMinFrameDuration Took 2,0896 ms
2014-03-10 19:14:25.547 PhotoShare[1053:60b] PERF: Setting up layers took 3,8447 ms
2014-03-10 19:14:36.171 PhotoShare[1053:60b] PERF: session.StartRunning() took 10622,8122 ms
2014-03-10 19:14:36.173 PhotoShare[1053:60b] PERF: SetupCamera Finished. Took 0,7167 ms.
2014-03-10 19:14:36.920 PhotoShare[1053:60b] PERF: Setup Focus in 1,4963 ms.
2014-03-10 19:14:36.922 PhotoShare[1053:60b] PERF: StartScanning() Took 20982,9365 ms.

@rogierrietdijk
Copy link
Author

In the file AVCaptureScannerView.cs change te method StopScanning() to the code below: (Remove output and inputs)

public void StopScanning()
{
if (stopped)
return;

        Console.WriteLine("Stopping...");

        if (session.Running) {

            session.StopRunning ();

            session.RemoveOutput (session.Outputs [0]);
            session.RemoveInput (session.Inputs [0]);
            session.Dispose ();
        }

        stopped = true;
    }

Rogier Rietdijk

Op 10 mrt. 2014, om 19:16 heeft mapo80 notifications@github.com het volgende geschreven:

Hi,

I've the same problem. After 4/5 times scanner starts very slowly. I'm using v.1.4.2 and I'm using an iPhone 5 on iOS 7.

Any solution? I've seen that fix suggested is included in verso 1.4.2

2014-03-10 19:14:12.525 PhotoShare[1053:60b] PERF: Setup Focus in 1,4683 ms.
2014-03-10 19:14:12.527 PhotoShare[1053:60b] PERF: StartScanning() Took 2772,0947 ms.
2014-03-10 19:14:13.670 PhotoShare[1053:60b] Stopping scan...
2014-03-10 19:14:15.424 PhotoShare[1053:7f03] Connected to the server
2014-03-10 19:14:15.936 PhotoShare[1053:60b] Starting to scan...
2014-03-10 19:14:15.944 PhotoShare[1053:60b] ZXingScannerView.Setup() took 5,282 ms.
2014-03-10 19:14:15.945 PhotoShare[1053:60b] StartScanning
2014-03-10 19:14:25.537 PhotoShare[1053:60b] PERF: Alloc AVCaptureVideoPreviewLayer took 11,132 ms.
2014-03-10 19:14:25.541 PhotoShare[1053:60b] PERF: ActiveVideoMinFrameDuration Took 2,0896 ms
2014-03-10 19:14:25.547 PhotoShare[1053:60b] PERF: Setting up layers took 3,8447 ms
2014-03-10 19:14:36.171 PhotoShare[1053:60b] PERF: session.StartRunning() took 10622,8122 ms
2014-03-10 19:14:36.173 PhotoShare[1053:60b] PERF: SetupCamera Finished. Took 0,7167 ms.
2014-03-10 19:14:36.920 PhotoShare[1053:60b] PERF: Setup Focus in 1,4963 ms.
2014-03-10 19:14:36.922 PhotoShare[1053:60b] PERF: StartScanning() Took 20982,9365 ms.


Reply to this email directly or view it on GitHub.

@mapo80
Copy link

mapo80 commented Mar 10, 2014

Hi,

I've solved! In ZXingScannerView.cs and in StartScanning method there isn't

        stopped = false;

So StopScanning method is badly executed!

I hope to see this fix asap!!

Thanks.

mapo80 added a commit to mapo80/ZXing.Net.Mobile that referenced this issue Mar 11, 2014
Barcode scanning become slowly after 4/5 times you open view controller.
Redth added a commit that referenced this issue Mar 21, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants