Xamarin ZXing初始化错误和缺少权限

时间:2015-08-11 07:29:30

标签: xamarin zxing

我有一个需要QR码扫描程序的Xamarin.Forms项目。我决定使用ZXing库。 安装工作正常,我在窗体项目中创建了一个DependencyService接口,因此我可以在其中使用Android / iOS实现。我从这里得到了这个描述: http://blog.thomaslebrun.net/2014/09/xamarin-scanning-qrcode-in-a-xamarin-forms-application/#.VcMrTPlNjkc

我遇到的问题是没有构造函数接受一个参数。只有一个零参数。

我有以下代码:

[assembly: Dependency(typeof(QrCodeScanningService))]
namespace my.project.Droid.DependencyServiceImplementations
{
    public class QrCodeScanningService : IQrCodeScanningService
    {
        public async Task<string> ScanAsync()
        {
            var scanner = new ZXing.Mobile.MobileBarcodeScanner(Application.Context); 
    // error: 'ZXing.Mobile.MobileBarcodeScanner' does not contain a constructor that takes 1 arguments

            var scanResults = await scanner.Scan();

            return scanResults.Text;
        }
    }
}

ZXing的官方例子使用 MobileBarcodeScanner.Initialize(应用程序); 但是在我的应用程序中,编译器说:

'Android.App.Application' is a 'type' but is used like a 'variable'

到目前为止,图书馆似乎没有初始化和传递上下文,但我不认为以这种方式使用它是个好主意。

此外,当将应用程序放入后台并将其恢复到前台时会崩溃,但会出现以下异常:

[MonoDroid] UNHANDLED EXCEPTION:
[MonoDroid] System.Exception: Couldn't get exclusive access to the camera
[MonoDroid] at ZXing.Mobile.ZXingSurfaceView.GetExclusiveAccess () <IL 0x00033, 0x00150>
[MonoDroid] at ZXing.Mobile.ZXingSurfaceView.StartScanning (ZXing.Mobile.MobileBarcodeScanningOptions,System.Action`1<ZXing.Result>) <IL 0x00046, 0x0028b>
[MonoDroid] at ZXing.Mobile.ZXingScannerFragment.scan () <IL 0x00012, 0x0007f>
[MonoDroid] at ZXing.Mobile.ZXingScannerFragment.OnResume () <IL 0x000fd, 0x00617>
[MonoDroid] at Android.Support.V4.App.Fragment.n_OnResume (intptr,intptr) <IL 0x0000a, 0x0009f>
[MonoDroid] at (wrapper dynamic-method) object.2ec0fbbb-3a10-48d4-b1ab-3fe72444ec91 (intptr,intptr) <IL 0x00011, 0x0003b>

我不知道丢失的初始化与此错误之间是否存在关联。相机权限已设置。

有人可以帮我解决这些错误吗? 提前谢谢。

0 个答案:

没有答案