public override void ViewDidLoad()
 {
     if (Request != null)
     {
         Request = new VNRecognizeTextRequest(RecognizeTextHandler);
     }
     base.ViewDidLoad();
 }
예제 #2
0
        public void DidFinish(VNDocumentCameraViewController controller, VNDocumentCameraScan scan)
        {
            var pageCount = (int)scan.PageCount;
            var allItems  = new List <List <string> >();

            for (int i = 0; i < pageCount; i++)
            {
                var image = scan.GetImage(nuint.Parse(i.ToString()));
                var imageRequestHandler = new VNImageRequestHandler(image.CGImage, options: new NSDictionary());

                var textRequest = new VNRecognizeTextRequest(new VNRequestCompletionHandler((request, error) =>
                {
                    var results = request.GetResults <VNRecognizedTextObservation>();

                    foreach (var result in results)
                    {
                        var items = new List <string>();

                        foreach (var candidate in result.TopCandidates(100))
                        {
                            items.Add(candidate.String);
                        }

                        allItems.Add(items);
                    }

                    Device.BeginInvokeOnMainThread(() =>
                    {
                        _page.LoadRecognizedTextItems(allItems);
                        DismissViewController(true, null);
                    });
                }));

                switch (_page.TextRecognitionLevel)
                {
                case TextRecognitionLevelEnum.Accurate:
                    textRequest.RecognitionLevel = VNRequestTextRecognitionLevel.Accurate;
                    break;

                case TextRecognitionLevelEnum.Fast:
                    textRequest.RecognitionLevel = VNRequestTextRecognitionLevel.Fast;
                    break;

                default:
                    break;
                }

                textRequest.UsesLanguageCorrection = true;

                DispatchQueue.DefaultGlobalQueue.DispatchAsync(() =>
                {
                    imageRequestHandler.Perform(new VNRequest[] { textRequest }, out NSError error);
                });
            }
        }
예제 #3
0
        public Task <ScanResult> ProcessImage(Stream bitmapStream)
        {
            var imageData = NSData.FromStream(bitmapStream);
            var image     = UIImage.LoadFromData(imageData);
            var v         = new VNImageOptions();

            if (image.CGImage == null)
            {
                throw new Exception("No image");
            }

            // TODO: Find a way to make orientation foolproof
            // (Probably convert stream to UIImage which has orientation encoded...)
            var requestHandler =
                new VNImageRequestHandler(image.CGImage, v);

            var completionSource = new TaskCompletionSource <ScanResult>();

            var request = new VNRecognizeTextRequest((vnRequest, error) =>
            {
                var results = vnRequest.GetResults <VNRecognizedTextObservation>();

                var scanResult = new ScanResult();
                foreach (var textObservation in results)
                {
                    var candidate = textObservation.TopCandidates(1).FirstOrDefault();
                    if (candidate != null)
                    {
                        scanResult.Add(GetBlock(candidate, textObservation));
                    }
                }
                completionSource.TrySetResult(scanResult);
            });

            requestHandler.Perform(new VNRequest[] { request }, out var nsError);
            // ReSharper disable once ConstantConditionalAccessQualifier
            if (!string.IsNullOrEmpty(nsError?.Description))
            {
                throw new Exception(nsError.Description);
            }

            return(completionSource.Task);
        }