Joseph Anderson
Joseph Anderson

Reputation: 4144

MonoTouch ZXING Camera Won't Open

I am using this app to scan ZXING barcodes:

https://github.com/jmawebtech/BarcodeReader-MonoTouch

If I enter the application, scan a barcode, press the home button, re-enter the app, and click scan, I see a black screen that looks like a camera shutter never opens. I have attached an image to this ticket.

If I press cancel, and go back to scan I see the camera open again.

How come the camera never opens on some occasions?

Camera Image Stays closed

Upvotes: 3

Views: 1134

Answers (3)

Alex Wiese
Alex Wiese

Reputation: 8370

I solved this by creating my own scan method which keeps a reference to the previously displayed ViewController and disposes of it before displaying a new one.

public static class BarcodeScanner
{
    private static ZxingCameraViewController currentBarcodeScanner;

    public static Task<Result> Scan(UIViewController hostController, MobileBarcodeScanner scanner, MobileBarcodeScanningOptions options)
    {
        return Task.Factory.StartNew(delegate
        {
            Result result = null;

            var scanResultResetEvent = new ManualResetEvent(false);

            hostController.InvokeOnMainThread(delegate
            {
                // Release previously displayed barcode scanner
                if (currentBarcodeScanner != null)
                {
                    currentBarcodeScanner.Dispose();
                    currentBarcodeScanner = null;
                }

                currentBarcodeScanner = new ZxingCameraViewController(options, scanner);

                // Handle barcode scan event
                currentBarcodeScanner.BarCodeEvent += delegate(BarCodeEventArgs e)
                {
                    currentBarcodeScanner.DismissViewController();
                    result = e.BarcodeResult;
                    scanResultResetEvent.Set();
                };

                // Handle barcode scan cancel event
                currentBarcodeScanner.Canceled += delegate
                {
                    currentBarcodeScanner.DismissViewController();
                    scanResultResetEvent.Set();
                };

                // Display the camera view controller
                hostController.PresentViewController(currentBarcodeScanner, true, delegate{});
            });

            // Wait for scan to complete
            scanResultResetEvent.WaitOne();

            return result;
        });
    }
}

You then use it like so

BarcodeScanner.Scan(this)
    .ContinueWith(t => InvokeOnMainThread(() =>
    {
        if (t.Result == null)
        {
            new UIAlertView("Scan Cancelled", "The barcode scan was cancelled", null, null, "OK").Show();
        }
        else
        {
            new UIAlertView("Scan Complete", "Result from barcode scan was " + t.Result, null, null, "OK").Show();
        }
    }))

Upvotes: 1

user1242297
user1242297

Reputation: 23

You dont have to go to the extreme of changing your app not to run in the background.

I fixed this issue by creating a viewcontroller property on the AppDelegate which every time I launched the cameraviewcontroller to open the camera I would point to this property. I called it cvc.

I accessed the property by referring to it through:

AppDelegate ad = (AppDelegate)UIApplication.SharedApplication.Delegate;

Then in the AppDelegate I had this code:

    public override void OnResignActivation (UIApplication application)
    {
        cvc.PerformSelector(new Selector("terminateWithSuccess"), null, 0f);
    }

Thanks for the kickstart though

Upvotes: 0

Joseph Anderson
Joseph Anderson

Reputation: 4144

I had to add this code to Info.plist:

UIApplicationExitsOnSuspend YES

To App Delegate, I had to add this code:

    public override void OnResignActivation (UIApplication application)
    {
        UIApplication.SharedApplication.PerformSelector(new Selector("terminateWithSuccess"), null, 0f);
    }

The video recorder, which was used in this software, cannot run on a background thread.

Upvotes: 1

Related Questions