Esempio n. 1
0
        public static async Task <string> GetFullText2(string pathToImg)
        {
            Bitmap bmp    = new Bitmap(pathToImg);
            int    width  = bmp.Width;
            int    height = bmp.Height;


            using (Graphics g = Graphics.FromImage(bmp))
            {
                // copy rectangle from screen (doesn't include cursor)

                using (var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream())
                {
                    //These steps to get to a SoftwareBitmap are aweful!
                    bmp.Save(stream.AsStream(), System.Drawing.Imaging.ImageFormat.Bmp);//choose the specific image format by your own bitmap source
                    Windows.Graphics.Imaging.BitmapDecoder decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream);

                    SoftwareBitmap bitmap = await decoder.GetSoftwareBitmapAsync();

                    OcrResult ocrResult = await _ocrEngine.RecognizeAsync(bitmap);

                    for (int i = 0; i < ocrResult.Lines.Count; i++)
                    {
                        OcrLine line = ocrResult.Lines[i];
                    }
                }
            }
            return(string.Empty);
        }
Esempio n. 2
0
        public static List <string> GetWords(OcrLine line)
        {
            List <string> words = new List <string>();

            foreach (OcrWord w in line.Words)
            {
                words.Add(w.Text);
            }

            return(words);
        }
Esempio n. 3
0
        private List <OcrLine> GetSingleLine(IEnumerable <OcrLine> lines)
        {
            var line = new OcrLine()
            {
                Id           = 1,
                ParentRegion = lines.First().ParentRegion,
                Text         = string.Join("\n", lines.Select(i => i.Text)),
                Words        = lines.SelectMany(i => i.Words).ToList(),
                Location     = new OcrLocation()
                {
                    X      = lines.Min(i => i.Location.X),
                    Y      = lines.Min(i => i.Location.Y),
                    Width  = lines.Min(i => i.Location.XBound) - lines.Min(i => i.Location.X),
                    Height = lines.Min(i => i.Location.YBound) - lines.Min(i => i.Location.Y)
                }
            };

            return(new List <OcrLine>(new[] { line }));
        }
Esempio n. 4
0
        private async void RenderPage(PdfPage pdfPage)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();

            canvas.Children.Clear();

            await pdfPage.PreparePageAsync();

            StorageFolder tempFolder = ApplicationData.Current.TemporaryFolder;
            StorageFile   jpgFile    = await tempFolder.CreateFileAsync(Guid.NewGuid().ToString() + ".png", CreationCollisionOption.GenerateUniqueName);

            PdfPageRenderOptions renderOptions = new PdfPageRenderOptions();

            renderOptions.DestinationHeight = (uint)(pdfPage.Size.Height * 2.0);
            renderOptions.DestinationWidth  = (uint)(pdfPage.Size.Width * 2.0);

            canvas.Width  = renderOptions.DestinationWidth;
            canvas.Height = renderOptions.DestinationHeight;

            if (jpgFile != null)
            {
                IRandomAccessStream randomStream = await jpgFile.OpenAsync(FileAccessMode.ReadWrite);

                await pdfPage.RenderToStreamAsync(randomStream, renderOptions);

                await randomStream.FlushAsync();

                randomStream.Dispose();
                pdfPage.Dispose();
                //await DisplayImageFileAsync(jpgFile);
            }

            SoftwareBitmap softwareBitmap;
            BitmapImage    image           = new BitmapImage();
            ImageBrush     backgroundBrush = new ImageBrush();

            using (IRandomAccessStream stream = await jpgFile.OpenAsync(FileAccessMode.Read))
            {
                // Create the decoder from the stream
                BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);

                image.SetSource(stream);
                backgroundBrush.ImageSource = image;

                // Get the SoftwareBitmap representation of the file
                softwareBitmap = await decoder.GetSoftwareBitmapAsync();
            }

            OcrEngine engine = OcrEngine.TryCreateFromUserProfileLanguages();
            var       result = await engine.RecognizeAsync(softwareBitmap);

            textBox.Text = "";
            var left = canvas.GetValue(Canvas.LeftProperty);
            var top  = canvas.GetValue(Canvas.TopProperty);

            foreach (OcrLine line in result.Lines)
            {
                Rectangle lineRect = new Rectangle();

                textBox.Text += line.Text + "\r\n";
                foreach (OcrWord word in line.Words)
                {
                    Rectangle r = new Rectangle();
                    r.Margin = new Thickness(word.BoundingRect.Left, word.BoundingRect.Top, 0.0, 0.0);
                    r.Width  = word.BoundingRect.Width;
                    r.Height = word.BoundingRect.Height;
                    r.Stroke = new SolidColorBrush(Colors.Blue);
                    canvas.Children.Add(r);
                    canvas.Background = backgroundBrush;
                }
            }

            // Looking for line "Duration of agreement"
            for (int i = 0; i < result.Lines.Count; i++)
            {
                OcrLine line = result.Lines[i];

                if (line.Text.Contains("Customer Name"))
                {
                    clientName.Text = line.Text.Substring(13);
                    //MessageDialog dlg = new MessageDialog(clientName.Text);
                    //await dlg.ShowAsync();
                }
            }

            sw.Stop();
            MessageDialog md = new MessageDialog(string.Format("Page processed in {0} milliseconds", sw.ElapsedMilliseconds));
            await md.ShowAsync();
        }
Esempio n. 5
0
        public static string GetLineAsString(OcrLine line)
        {
            List <string> words = GetWords(line);

            return(words.Count > 0 ? string.Join(" ", words) : string.Empty);
        }