示例#1
0
        public async void Sample3AllModes()
        {
            foreach (var engineMode in Enum.GetValues(typeof(OcrEngineMode)))
            {
                Console.WriteLine("Engine mode: {0}", engineMode);
                await _api.Init("eng", (OcrEngineMode)engineMode);

                foreach (var segmentationMode in Enum.GetValues(typeof(PageSegmentationMode)))
                {
                    _api.SetPageSegmentationMode((PageSegmentationMode)segmentationMode);
                    using (var stream = LoadSample("sample3.png")) {
                        var result = await _api.SetImage(stream);

                        Assert.IsTrue(result);
                        Console.WriteLine("Segmentation mode: {0}, result: \"{1}\"", segmentationMode,
                                          _api.Text.Replace("\n", " "));
                    }
                }
            }
        }
        private async void initialize()
        {
            if (!_tesseractApi.Initialized)
            {
                await _tesseractApi.Init("spa");

                _tesseractApi.SetPageSegmentationMode(PageSegmentationMode.SparseText);
                string textPermited = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.;€:";
                //abcdefghijklmnopqrstuvwxyz
                _tesseractApi.SetWhitelist(textPermited);
            }
        }
示例#3
0
        public CalKnightsViewModel()
        {
            _tesseractApi = Resolver.Resolve <ITesseractApi>();
            _device       = Resolver.Resolve <IDevice>();
            _dicts        = new Dictionary <string, string>
            {
                { "vanguard", "test" }
            };

            PickImageCommand = new Command(
                execute: async() =>
            {
                ImageWaiting = false;

                if (!_tesseractApi.Initialized)
                {
                    await _tesseractApi.Init("kor", OcrEngineMode.TesseractOnly);
                    _tesseractApi.SetPageSegmentationMode(PageSegmentationMode.SparseText);
                }

                var imageFile = await TakePic();
                if (imageFile != null)
                {
                    //var imgStream = imageFile.GetStream();
                    //int imgLen = (int)imgStream.Length;
                    //var bytes = new byte[imgLen];

                    //imgStream.Position = 0;
                    //imgStream.Read(bytes, 0, imgLen);
                    //for (int i = 0; i < imgLen; i++)
                    //    bytes[i] = (byte)(0xFF ^ bytes[i]);

                    //var memoryStream = new MemoryStream();
                    //await mediaFile.GetStream().CopyToAsync(memoryStream);
                    //byte[] imageAsByte = memoryStream.ToArray();
                    byte[] bytes;

                    using (Stream stream = imageFile.GetStream())
                    {
                        //await stream.CopyToAsync(memStream);
                        //memStream.Seek(0, SeekOrigin.Begin);
                        var bitmap = SKBitmap.Decode(stream);

                        var img    = SKImage.FromBitmap(bitmap);
                        int height = img.Height;
                        int width  = img.Width;

                        var subset = SKRectI.Create(
                            (int)(width * 0.2f), (int)(height * 0.4f),
                            (int)(width * 0.5f), (int)(height * 0.3f));
                        var placeholder  = SKRectI.Empty;
                        var placeholder2 = SKPointI.Empty;

                        var img2 = img.ApplyImageFilter(
                            SKImageFilter.CreateColorFilter(_greyFilter),
                            subset, subset, out placeholder, out placeholder2);
                        if (img2 == null)
                        {
                            Debug.WriteLine("ISNULL");
                        }
                        img = img.Subset(subset);

                        bytes          = img.Encode().ToArray();
                        OCRImageSource = ImageSource.FromStream(() =>
                        {
                            return(new MemoryStream(bytes));
                        });
                    }

                    // ImageSource.FromStream(() => { return imageFile.GetStream(); });
                    var tessResult = await _tesseractApi.SetImage(bytes);

                    if (tessResult)
                    {
                        Debug.WriteLine("read");
                        Debug.WriteLine(_tesseractApi.Text);
                    }
                    else
                    {
                        Debug.WriteLine("unavaliable");
                    }
                }
                ImageWaiting = true;
            });

            FoldSelectorCommand = new Command <string>(
                execute: (string selector) =>
            {
                int i = 0;
                switch (selector)
                {
                case "filter": i = 0; break;

                case "tag": i = 1; break;

                case "sort": i = 2; break;
                }
                _isFrameVisible[i] = !_isFrameVisible[i];
                OnPropertyChanged("IsFrameVisible");
            });
        }
        /// <summary>
        /// Method for recognizing the characters. Input comes from the ScanPage.xaml.cs, and is then processed into words
        /// </summary>
        /// <param name="result">Stream of bytes of the picture to process</param>
        /// <returns></returns>
        private async Task Recognise(Stream result)
        {
            if (IsBusy)
            {
                return;
            }
            IsBusy = true;

            if (result == null)
            {
                return;
            }

            if (!_tesseract.Initialized)
            {
                var initialised = await _tesseract.Init("deu");

                _tesseract.SetWhitelist("0123456789");
                _tesseract.SetPageSegmentationMode(PageSegmentationMode.SingleChar);
                if (!initialised)
                {
                    return;
                }
            }


            bool success = await _tesseract.SetImage(result);

            if (success)
            {
                var words      = _tesseract.Results(PageIteratorLevel.Word);
                var symbols    = _tesseract.Results(PageIteratorLevel.Symbol);
                var blocks     = _tesseract.Results(PageIteratorLevel.Block);
                var paragraphs = _tesseract.Results(PageIteratorLevel.Paragraph);
                var lines      = _tesseract.Results(PageIteratorLevel.Textline);

                var textResult = "";

                var enumerator = words.GetEnumerator();

                while (enumerator.MoveNext())
                {
                    var item = enumerator.Current.Text;
                    textResult += item;
                }

                bool isDigit = textResult.All(char.IsDigit);

                if (!isDigit)
                {
                    textResult = "?";
                }

                Reading += textResult;
            }
            else
            {
                Reading = "didnt work";
            }
            IsBusy = false;
        }
 public async void Sample3AllModes ()
 {
     using (_api = new TesseractApi (Android.App.Application.Context, AssetsDeployment.OncePerInitialization)) {
         foreach (var engineMode in Enum.GetValues(typeof(OcrEngineMode))) {
             Console.WriteLine ("Engine mode: {0}", engineMode);
             await _api.Init ("eng", (OcrEngineMode)engineMode);
             foreach (var segmentationMode in Enum.GetValues(typeof(PageSegmentationMode))) {
                 _api.SetPageSegmentationMode ((PageSegmentationMode)segmentationMode);
                 using (var stream = LoadSample ("sample3.png")) {
                     var result = await _api.SetImage (stream);
                     Assert.IsTrue (result);
                     Console.WriteLine ("Segmentation mode: {0}, result: \"{1}\"", segmentationMode, _api.Text.Replace ("\n", " "));
                 }
             }
         }
     }
 }