예제 #1
0
        public static async Task <List <BarcodeResult> > ScanFromImage(byte[] imageArray)
        {
            UIImage               image           = new UIImage(NSData.FromArray(imageArray));
            var                   visionImage     = new VisionImage(image);
            VisionImageMetadata   metadata        = new VisionImageMetadata();
            VisionApi             vision          = VisionApi.Create();
            VisionBarcodeDetector barcodeDetector = vision.GetBarcodeDetector(Configuration.BarcodeDetectorSupportFormat);

            VisionBarcode[] barcodes = await barcodeDetector.DetectAsync(visionImage);

            if (barcodes == null || barcodes.Length == 0)
            {
                return(new List <BarcodeResult>());
            }

            List <BarcodeResult> resultList = new List <BarcodeResult>();

            foreach (var barcode in barcodes)
            {
                resultList.Add(new BarcodeResult
                {
                    BarcodeType  = Methods.ConvertBarcodeResultTypes(barcode.ValueType),
                    DisplayValue = barcode.DisplayValue,
                    RawValue     = barcode.RawValue
                });
            }
            return(resultList);
        }
예제 #2
0
            public CaptureVideoDelegate()
            {
                metadata        = new VisionImageMetadata();
                vision          = VisionApi.Create();
                barcodeDetector = vision.GetBarcodeDetector(Configuration.BarcodeDetectorSupportFormat);
                // Using back-facing camera
                var devicePosition = AVCaptureDevicePosition.Back;

                var deviceOrientation = UIDevice.CurrentDevice.Orientation;

                switch (deviceOrientation)
                {
                case UIDeviceOrientation.Portrait:
                    metadata.Orientation = devicePosition == AVCaptureDevicePosition.Front ? VisionDetectorImageOrientation.LeftTop : VisionDetectorImageOrientation.RightTop;
                    break;

                case UIDeviceOrientation.LandscapeLeft:
                    metadata.Orientation = devicePosition == AVCaptureDevicePosition.Front ? VisionDetectorImageOrientation.BottomLeft : VisionDetectorImageOrientation.TopLeft;
                    break;

                case UIDeviceOrientation.PortraitUpsideDown:
                    metadata.Orientation = devicePosition == AVCaptureDevicePosition.Front ? VisionDetectorImageOrientation.RightBottom : VisionDetectorImageOrientation.LeftBottom;
                    break;

                case UIDeviceOrientation.LandscapeRight:
                    metadata.Orientation = devicePosition == AVCaptureDevicePosition.Front ? VisionDetectorImageOrientation.TopRight : VisionDetectorImageOrientation.BottomRight;
                    break;

                case UIDeviceOrientation.FaceUp:
                case UIDeviceOrientation.FaceDown:
                case UIDeviceOrientation.Unknown:
                    metadata.Orientation = VisionDetectorImageOrientation.LeftTop;
                    break;
                }
            }
예제 #3
0
        public static async Task <List <BarcodeResult> > ScanFromImage(byte[] imageArray)
        {
            UIImage               image           = new UIImage(NSData.FromArray(imageArray));
            var                   visionImage     = new VisionImage(image);
            VisionImageMetadata   metadata        = new VisionImageMetadata();
            VisionApi             vision          = VisionApi.Create();
            VisionBarcodeDetector barcodeDetector = vision.GetBarcodeDetector(Configuration.BarcodeDetectorSupportFormat);

            VisionBarcode[] barcodes = await barcodeDetector.DetectAsync(visionImage);

            if (barcodes == null || barcodes.Length == 0)
            {
                return(new List <BarcodeResult>());
            }

            List <BarcodeResult> resultList = new List <BarcodeResult>();

            foreach (var barcode in barcodes)
            {
                var points = barcode.CornerPoints.ToList().ConvertAll(nsvalue => nsvalue.PointFValue);
                resultList.Add(new BarcodeResult
                {
                    BarcodeType  = Methods.ConvertBarcodeResultTypes(barcode.ValueType),
                    DisplayValue = barcode.DisplayValue,
                    Points       = points.Select(p => (p.X / (double)image.Size.Width, p.Y / (double)image.Size.Height)).ToList()
                });
예제 #4
0
        /// <summary>
        /// <see cref="LabelReaderBase.GetRawBarcodeTextFromImageAsync(object)"/>
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        public override async Task <string> GetRawBarcodeTextFromImageAsync(object image)
        {
            CoreMedia.CMSampleBuffer iOSImage = (CoreMedia.CMSampleBuffer)image;
            VisionImage visionImage           = new VisionImage(iOSImage);

            visionImage.Metadata = new VisionImageMetadata {
                Orientation = GetOrientation()
            };
            VisionApi             api             = VisionApi.Create();
            VisionBarcodeDetector barcodeDetector = api.GetBarcodeDetector(new VisionBarcodeDetectorOptions(VisionBarcodeFormat.QRCode));

            VisionBarcode[] barcodes = await barcodeDetector.DetectAsync(visionImage);

            if (barcodes.Length <= 0)
            {
                return(String.Empty);
            }
            return(barcodes.First().RawValue);
        }
예제 #5
0
        void UseBarcodeScanningModel()
        {
            var options         = new VisionBarcodeDetectorOptions(VisionBarcodeFormat.All);
            var barcodeDetector = vision.GetBarcodeDetector(options);
            var image           = new VisionImage(ImgSample.Image);

            barcodeDetector.Detect(image, HandleVisionBarcodeDetectionCallback);

            void HandleVisionBarcodeDetectionCallback(VisionBarcode [] barcodes, NSError error)
            {
                if (error != null)
                {
                    TxtData.Text = error.Description;
                    return;
                }

                if (barcodes == null || barcodes.Length == 0)
                {
                    TxtData.Text = "No barcodes were found.";
                    return;
                }

                var stringBuilder = new StringBuilder();

                foreach (var barcode in barcodes)
                {
                    stringBuilder.AppendLine($"Raw Value: {barcode.RawValue}");
                    stringBuilder.AppendLine($"Display Value: {barcode.DisplayValue}");
                    stringBuilder.AppendLine($"Format: {barcode.Format}");
                    stringBuilder.AppendLine($"Value Type: {barcode.ValueType}");
                    stringBuilder.AppendLine();
                }

                TxtData.Text = stringBuilder.ToString();
            }
        }