コード例 #1
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            contatinerView = new UIView(CGRect.Empty);
            View.AddSubview(contatinerView);
            freezeButton = new UIButton(CGRect.Empty);
            freezeButton.SetTitle("Freeze", UIControlState.Normal);
            freezeButton.SetBackgroundImage(UIImageExtensions.Brand.GetImage(), UIControlState.Normal);
            freezeButton.TouchUpInside += (sender, e) =>
            {
                var scanning = picker.IsScanning();
                if (scanning)
                {
                    matrixScanHandler.Enabled = false;
                    picker.PauseScanning();
                    freezeButton.SetTitle("Done", UIControlState.Normal);
                }
                else
                {
                    matrixScanHandler.RemoveAllAugmentations();
                    matrixScanHandler.Enabled = true;
                    picker.StartScanning();
                    freezeButton.SetTitle("Freeze", UIControlState.Normal);
                }
            };
            View.AddSubview(freezeButton);

            freezeButton.TranslatesAutoresizingMaskIntoConstraints   = false;
            contatinerView.TranslatesAutoresizingMaskIntoConstraints = false;
            View.AddConstraints(new[]
            {
                contatinerView.LeadingAnchor.ConstraintEqualTo(View.LeadingAnchor),
                contatinerView.TopAnchor.ConstraintEqualTo(View.TopAnchor),
                contatinerView.TrailingAnchor.ConstraintEqualTo(View.TrailingAnchor),
                contatinerView.BottomAnchor.ConstraintEqualTo(View.BottomAnchor),
                freezeButton.LeadingAnchor.ConstraintEqualTo(View.LeadingAnchor, 20),
                freezeButton.BottomAnchor.ConstraintEqualTo(View.BottomAnchor, -20),
                freezeButton.TrailingAnchor.ConstraintEqualTo(View.TrailingAnchor, -20),
                freezeButton.HeightAnchor.ConstraintEqualTo(60)
            });

            var settings = ScanSettings.DefaultSettings();

            settings.SetSymbologyEnabled(Symbology.EAN13, true);
            settings.MatrixScanEnabled        = true;
            settings.MaxNumberOfCodesPerFrame = 15;
            settings.HighDensityModeEnabled   = true;
            picker = new BarcodePicker(settings);
            picker.OverlayView.GuiStyle = GuiStyle.None;

            matrixScanHandler = new MatrixScanHandler(picker);
            matrixScanHandler.ShouldReject += (matrixScanHandler, trackedBarcode) => false;

            // This delegate method is called every time a new frame has been processed.
            // In this case we use it to update the offset of the augmentation.
            matrixScanHandler.DidProcess += (sender, e) =>
            {
                DispatchQueue.MainQueue.DispatchAsync(() =>
                {
                    foreach (var item in e.Frame.TrackedCodes)
                    {
                        var offset = GetYOffSet(item.Value as TrackedBarcode);
                        viewBasedMatrixScanOverlay.SetOffset(offset, item.Key as NSNumber);
                    }
                });
            };

            viewBasedMatrixScanOverlay = new ViewBasedMatrixScanOverlay();

            // This method is called every time a new barcode has been tracked.
            // You can implement this method to return the offset that will be used to position the augmentation
            // with respect to the center of the tracked barcode.
            viewBasedMatrixScanOverlay.OffsetForOverlay += (overlay, barcode, identifier) => GetYOffSet(barcode);

            // This delegate method is called every time a new barcode has been tracked.
            // You can implement this method to return the view that will be used as the augmentation.
            viewBasedMatrixScanOverlay.ViewForOverlay += (overlay, barcode, identifier) =>
            {
                if (barcode.Data == null)
                {
                    return(new UIView(CGRect.Empty));
                }
                var view  = new StockView(new CGRect(0, 0, StockView.StandardWidth, StockView.StandardHeight));
                var model = Model.MockedModel(barcode.Data);
                view.AddGestureRecognizer(new UITapGestureRecognizer(() =>
                {
                    var overlayViewController = new OverlayViewController
                    {
                        Model = model,
                        ModalTransitionStyle   = UIModalTransitionStyle.CoverVertical,
                        ModalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
                    };
                    PresentViewController(overlayViewController, false, null);
                }));
                view.Model = model;

                return(view);
            };

            // Add a ViewBasedMatrixScanOverlay in order to have custom UIView instances as augmentations.
            matrixScanHandler.AddOverlay(viewBasedMatrixScanOverlay);

            simpleMatrixScanOverlay = new SimpleMatrixScanOverlay();

            // This method is called every time a new barcode has been tracked.
            // You can implement this method to customize the color of the highlight.
            simpleMatrixScanOverlay.ColorForOverlay += (overlay, barcode, identifier) => Model.MockedColor(barcode.Data);

            // Add a SimpleMatrixScanOverlay in order to highlight the barcodes.
            matrixScanHandler.AddOverlay(simpleMatrixScanOverlay);

            AddChildViewController(picker);
            picker.View.Frame            = contatinerView.Frame;
            picker.View.AutoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth;
            contatinerView.AddSubview(picker.View);
            picker.DidMoveToParentViewController(this);
            picker.StartScanning();
        }