如何在PageRenderer中使用ZXingScannerFragment

时间:2017-05-15 04:46:54

标签: android xamarin.android xamarin.forms zxing.net

由于ZXing.Net.Mobile在2.2.0更新后的表单上有问题,我需要将ZXing分成不同的平台。但是android版本无法在PageRenderer中运行。

以下是我的项目信息:

Xamarin: 10.10

Android版: API 23

项目: Xamarin PCL表格

ZXing版本: 2.1.47

App.Android - AndroidManifest:

 <manifest ...../>
        <uses-permission android:name="android.permission.CAMERA" />
          <uses-permission android:name="android.permission.FLASHLIGHT" />
          <uses-feature android:name="android.hardware.camera" />
          <uses-feature android:name="android.hardware.camera.autofocus" />
          <uses-feature android:name="android.hardware.camera.flash" />
    <application ....

App.Android - MainActivity.cs:

protected override void OnCreate(Bundle bundle)
{
           base.OnCreate(bundle);
           MobileBarcodeScanner.Initialize(Application);

}

App.Android - QRCodePageRenderer:

class QRCodePageRenderer : PageRenderer
{
        private ZXingScannerFragment ScanFragment;
        ZXingSurfaceView ZxingScanningView;
        private Android.Views.View ZxingOverlay;
        FrameLayout frame;
        public bool ContinuousScanning { get; set; }
        protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
        {
            base.OnElementChanged(e);

            var activity = this.Context as Activity;
            LayoutInflater layoutInflater = (LayoutInflater)this.Context.GetSystemService(Context.LayoutInflaterService);
            frame = (FrameLayout)layoutInflater.Inflate(Resource.Layout.zxingscannerfragmentlayout, null);
            ZxingScanningView = new ZXingSurfaceView(activity);
            ScanningOptions = new MobileBarcodeScanningOptions();
            ScanningOptions.PossibleFormats = Constants.QRCODE_SCAN_FORMAT;


            try
            {
                var scanner = new ZXingSurfaceView(activity, ScanningOptions);
                var layoutParams = getChildLayoutParams();
                frame.AddView(scanner, layoutParams);
                ZxingOverlay = new ZxingOverlayView(activity);
                frame.AddView(ZxingOverlay, layoutParams);
                this.AddView(frame);

            }
            catch (Exception ex)
            {
                Console.WriteLine("Create Surface View Failed: " + ex);
            }

            ScanFragment = new ZXingScannerFragment();
            ScanFragment.TopText = "ABC";
                Task.Factory.StartNew(() =>
                {
                    Console.WriteLine("StartNew scan .....");

                    ScanFragment.StartScanning(result => {

                        if (!ContinuousScanning)
                        {
                            Console.WriteLine(result.Text);
                            Console.WriteLine("Stopping scan...");

                            ScanFragment.PauseAnalysis();

                        }
                    }, this.ScanningOptions);

                });

            }
 }

更新2017-05-15

QRCodeReader的屏幕截图 Screenshot of the QRCodeReader. It has empty view

0 个答案:

没有答案