예제 #1
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (files.Count <= 0)
            {
                return;
            }

            OcrButton.IsEnabled    = false;
            ocrButtonNormalContent = OcrButton.Content.ToString();
            OcrButton.Content      = "Please wait!";
            lvFiles.AllowDrop      = false;

            for (int i = 0; i < files.Count; i++)
            {
                OcrFile file = files[i];
                file.ClearStatus();
                files[i] = file;
            }

            //Start a new thread for the image processing and ocr stuff
            ConvertImageDelegate convertImageDelegate = new ConvertImageDelegate(ConvertImages);

            convertImageDelegate.BeginInvoke(null, null);
        }
예제 #2
0
 private void UpdateFile(OcrFile file, int index)
 {
     files[index] = file;
 }
예제 #3
0
        private void ConvertImages()
        {
            for (int i = 0; i < files.Count; i++)
            {
                OcrFile file = files[i];

                Bitmap bitmap = new Bitmap(file.FileInfo.FullName);
                if (bitmap == null)
                {
                    file.Status = "Not an image...";

                    //Refresh the UI
                    lvFiles.Dispatcher.BeginInvoke(DispatcherPriority.Normal, null, null);

                    continue;
                }

                string invertedImagePath = Path.Combine(file.FileInfo.DirectoryName, GetInvertedImageName(file.FileInfo));

                //First remove transparency by drawing a black background and drawing the text on it
                //Then invert the colors of the whole thing. It's better for the OCR
                //Finally scale it, also better for the OCR
                using (var b = new Bitmap(bitmap.Width, bitmap.Height))
                {
                    b.SetResolution(bitmap.HorizontalResolution, bitmap.VerticalResolution);

                    using (var g = Graphics.FromImage(b))
                    {
                        g.Clear(Color.Black);
                        g.DrawImageUnscaled(bitmap, 0, 0);
                    }

                    MagickImage magickImage = new MagickImage(b);
                    magickImage.Negate(Channels.RGB);

                    //magickImage.FilterType = FilterType.Lanczos2;
                    magickImage.Resize(1024, 1024);

                    Bitmap inverted = magickImage.ToBitmap();
                    inverted.Save(invertedImagePath);
                    inverted.Dispose();

                    magickImage.Dispose();
                }

                try
                {
                    using (var engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default))
                    {
                        using (var img = Pix.LoadFromFile(invertedImagePath))
                        {
                            using (var page = engine.Process(img))
                            {
                                File.WriteAllText(string.Concat(file.FileInfo.FullName, ".txt"), page.GetText());
                                file.Confidence = page.GetMeanConfidence();
                                file.Status     = string.Concat("Done! (", file.Confidence.ToString("0.00"), ")");
                            }
                        }
                    }
                }
                catch (Exception error)
                {
                    file.Status = "Tesseract Error: " + error.Message;
                }

                File.Delete(invertedImagePath);

                //Refresh the UI
                lvFiles.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new UpdateFileDelegate(UpdateFile), file, i);
            }

            //I have no idea what I'm doing...
            //lvFiles.ItemsSource = null;
            //lvFiles.ItemsSource = files;

            Dispatcher.BeginInvoke(DispatcherPriority.Normal, new ConversionDoneDelegate(ConversionDone));
        }