/// <summary>
        /// Takes an image, and runs it through Tesseract, returning
        /// the cleaned string it detected.
        /// </summary>
        /// <returns>The ocr.</returns>
        /// <param name="image">Image.</param>
        private async Task <string[]> ProcessOCR(UIImage image)
        {
            if (!_tesseractInitialised)
            {
                //tesseract.SetPageSegmentationMode(PageSegmentationMode.SingleWord);
                _tesseractInitialised = await tesseract.Init("eng");

                tesseract.SetVariable("tosp_min_sane_kn_sp", "10");

                if (!string.IsNullOrEmpty(OCRWhiteList))
                {
                    tesseract.SetWhitelist(OCRWhiteList);
                }
            }

            bool success = await tesseract.SetImage(image.AsPNG().AsStream());

            if (!isDisposing)
            {
                if (success)
                {
                    var results = tesseract.Results(PageIteratorLevel.Word);
                    return(results?.Select(u => u.Text).ToArray());
                }
            }

            return(null);
        }
Beispiel #2
0
        public async void Sample4JpgWithWhitelist()
        {
            await _api.Init("eng");

            _api.SetWhitelist("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
            using (var stream = LoadSample("sample4.jpg")) {
                var result = await _api.SetImage(stream);

                Assert.IsTrue(result);
                Assert.IsTrue(_api.Text.Contains("Good font for the OCR"));
            }
        }
        private async void initialize()
        {
            if (!_tesseractApi.Initialized)
            {
                await _tesseractApi.Init("spa");

                _tesseractApi.SetPageSegmentationMode(PageSegmentationMode.SparseText);
                string textPermited = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.;€:";
                //abcdefghijklmnopqrstuvwxyz
                _tesseractApi.SetWhitelist(textPermited);
            }
        }
        public async void Sample4JpgWithWhitelist()
        {
            await _api.Init("eng");

            _api.SetWhitelist("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
            using (var stream = LoadSample("sample4.jpg")) {
                var result = await _api.SetImage(stream);

                Assert.IsTrue(result);
                Assert.AreEqual(
                    "Good font for the OCR\nDingufrfom n Me am\nhe mm mm m cm\n\nGood 60m size for ocn\n\n", _api.Text);
            }
        }
        /// <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;
        }
Beispiel #6
0
        private async void CameraButton_Clicked(object sender, EventArgs e)
        {
            await CrossMedia.Current.Initialize();

            var file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
            {
                Directory = "MedSticker",
                Name      = "sticker.jpg"
            });

            if (file == null)
            {
                return;
            }
            System.IO.Stream photoStream = file.GetStream();


            //photo.Source = ImageSource.FromStream(() =>
            // {
            //     var stream = file.GetStream();
            //file.Dispose();
            //     return stream;
            // });

            //NameEntry.Text = "1";
            //Photo.Source = ImageSource.FromStream(file.GetStream); //debug code
            //Bitmap bitmap = new Bitmap(photoStream);
            //Stream photoStream2 = RaiseContrast(bitmap);
            //Photo.Source = ImageSource.FromStream(() => stream2);
            bool initialised = await api.Init("eng");

            //NameEntry.Text += "2";
            api.SetWhitelist("0123456789:-,#'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
            bool success = await api.SetImage(photoStream);

            //NameEntry.Text += "3";
            if (success)
            {
                //List<Result> lines = api.Results(PageIteratorLevel.Textline);
                //List<Result> words = api.Results(PageIteratorLevel.Word);
                List <Result> results = api.Results(PageIteratorLevel.Symbol).ToList();
                //List<Result> blocks = api.Results(PageIteratorLevel.Block);

                //List<Result> results = api.Res    ults(PageIteratorLevel.Paragraph).ToList();
                var res  = " ";
                var conf = " ";
                foreach (Result r in results)
                {
                    if (r.Confidence > 85f)
                    {
                        res  += r.Text.ToUpper();
                        conf += r.Confidence.ToString() + " ";
                    }
                }
                TestLabel.Text = res;
                res            = res.Replace(":", "");

                //SexEntry.Text = res;
                //MREntry.Text = conf;
                if (res.Contains("SEX"))
                {
                    var sindex = res.LastIndexOf("SEX") + 2;
                    SexEntry.Text = res.Substring(sindex + 1, 1);
                }
                if (res.Contains("PCP"))
                {
                    var nindex = res.LastIndexOf("PCP");
                    NameEntry.Text = res.Substring(0, nindex);
                }
                else if (res.Contains("'MR"))
                {
                    var nindex2 = res.LastIndexOf("'MR");
                    NameEntry.Text = res.Substring(0, nindex2 - 1);
                }
                if (res.Contains("ACCT") & res.Contains("#"))
                {
                    var mindex2 = res.LastIndexOf("ACCT");
                    var mindex  = res.IndexOf("#");
                    MREntry.Text = res.Substring(mindex + 1, mindex2 - mindex - 1);
                }
                if (!(res.Contains("PR") | res.Contains("PCP") | res.Contains("ACCT") | res.Contains("#")))
                {
                    await DisplayAlert("Could not detect text.", "Try again or enter info manually.", "OK");
                }
            }
            else
            {
                NameEntry.Text = "Image Recognition Failed";
            }
        }
        public async Task Init()
        {
            await _tesseractEngine.Init("num");

            _tesseractEngine.SetWhitelist("0123456789");
        }