MonoTouch ZXING相机无法打开

时间:2012-07-04 16:41:56

标签: c# xamarin.ios zxing

我正在使用此应用程序扫描ZXING条形码:

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

如果我进入应用程序,扫描条形码,按主页按钮,重新进入应用程序,然后单击扫描,我看到一个看起来像相机快门的黑色屏幕永远不会打开。我已将图像附在此票上。

如果我按取消,然后返回扫描,我会再次打开相机。

为什么相机在某些情况下永远不会打开?

Camera Image Stays closed

3 个答案:

答案 0 :(得分:1)

我必须将此代码添加到Info.plist:

UIApplicationExitsOnSuspend YES

To App Delegate,我不得不添加以下代码:

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

此软件中使用的录像机无法在后台线程上运行。

答案 1 :(得分:1)

我通过创建自己的扫描方法解决了这个问题,该方法保留了对先前显示的ViewController的引用,并在显示新的之前对其进行处理。

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;
        });
    }
}

然后你就这样使用

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();
        }
    }))

答案 2 :(得分:0)

你不必极端地改变你的应用程序而不是在后台运行。

我通过在AppDelegate上创建一个viewcontroller属性修复了这个问题,每当我启动cameraviewcontroller打开相机时我会指向这个属性。我叫它cvc。

我通过以下方式访问了该属性:

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

然后在AppDelegate中我有了这段代码:

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

感谢kickstart