Exemplo n.º 1
0
        private void backgroundWorker3_DoWork(object sender, DoWorkEventArgs e)
        {
            string   selectedImageFile = (string)e.Argument;
            FileInfo imageFile         = new FileInfo(selectedImageFile);

            if (selectedImageFile.ToLower().EndsWith(".pdf"))
            {
                string workingTiffFileName = null;

                try
                {
                    workingTiffFileName = Utilities.ConvertPdf2Tiff(selectedImageFile);
                    imageList           = ImageIOHelper.GetImageList(new FileInfo(workingTiffFileName));
                }
                finally
                {
                    if (workingTiffFileName != null && File.Exists(workingTiffFileName))
                    {
                        File.Delete(workingTiffFileName);
                    }
                }
            }
            else
            {
                imageList = ImageIOHelper.GetImageList(imageFile);
            }

            e.Result = imageFile;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Opens image file.
        /// </summary>
        /// <param name="selectedImageFile"></param>
        public void openFile(string selectedImageFile)
        {
            FileInfo imageFile = new FileInfo(selectedImageFile);

            if (selectedImageFile.ToLower().EndsWith(".pdf"))
            {
                try
                {
                    string   workingTiffFileName = Utilities.ConvertPdf2Tiff(selectedImageFile);
                    FileInfo workingTiffFile     = new FileInfo(workingTiffFileName);
                    imageList = ImageIOHelper.GetImageList(workingTiffFile);
                    workingTiffFile.Delete();
                }
                catch (Exception e)
                {
                    MessageBox.Show(this, e.Message + "\nPlease install Ghostscript and/or set system path to the library object.", strProgName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            else
            {
                imageList = ImageIOHelper.GetImageList(imageFile);
            }

            if (imageList == null)
            {
                MessageBox.Show(this, Properties.Resources.Cannotloadimage, strProgName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            imageTotal = imageList.Count;
            imageIndex = 0;

            displayImage();

            this.Text = imageFile.Name + " - " + strProgName;
            this.toolStripStatusLabel1.Text = null;
            this.pictureBox1.Deselect();

            this.toolStripBtnFitImage.Enabled  = true;
            this.toolStripBtnZoomIn.Enabled    = true;
            this.toolStripBtnZoomOut.Enabled   = true;
            this.toolStripBtnRotateCCW.Enabled = true;
            this.toolStripBtnRotateCW.Enabled  = true;

            if (imageList.Count == 1)
            {
                this.toolStripBtnNext.Enabled = false;
                this.toolStripBtnPrev.Enabled = false;
            }
            else
            {
                this.toolStripBtnNext.Enabled = true;
                this.toolStripBtnPrev.Enabled = true;
            }

            setButton();
        }
Exemplo n.º 3
0
        /// <summary>
        /// Convert PDF to TIFF format.
        /// </summary>
        /// <param name="inputPdfFile"></param>
        /// <returns>a multi-page TIFF image</returns>
        public static string ConvertPdf2Tiff(string inputPdfFile)
        {
            try {
                string[] pngFiles = ConvertPdf2Png(inputPdfFile);
                string   tiffFile = Path.GetTempFileName();
                tiffFile = Path.ChangeExtension(tiffFile, ".tif");

                // put PNG images into a single multi-page TIFF image for return
                ImageIOHelper.MergeTiff(pngFiles, tiffFile);

                // delete temporary PNG images
                foreach (string tempFile in pngFiles)
                {
                    new FileInfo(tempFile).Delete();
                }

                return(tiffFile);
            }
            catch (Exception e)
            {
                Console.WriteLine("ERROR: " + e.Message);
                return(null);
            }
        }
Exemplo n.º 4
0
        void PerformOCR(string[] args)
        {
            try
            {
                if (args[0] == "-?" || args[0] == "-help" || args.Length == 1 || args.Length == 3 || args.Length == 5)
                {
                    Console.WriteLine("Usage: vietocr imagefile outputfile [-l lang] [-psm pagesegmode]");
                    return;
                }
                FileInfo imageFile  = new FileInfo(args[0]);
                FileInfo outputFile = new FileInfo(args[1]);

                if (!imageFile.Exists)
                {
                    Console.WriteLine("Input file does not exist.");
                    return;
                }

                string curLangCode = "eng"; //default language
                string psm         = "3";   // or alternatively, "PSM_AUTO"; // 3 - Fully automatic page segmentation, but no OSD (default)

                if (args.Length == 4)
                {
                    if (args[2].Equals("-l"))
                    {
                        curLangCode = args[3];
                    }
                    else if (args[2].Equals("-psm"))
                    {
                        psm = args[3];
                    }
                }
                else if (args.Length == 6)
                {
                    curLangCode = args[3];
                    psm         = args[5];
                    try
                    {
                        Int16.Parse(psm);
                    }
                    catch
                    {
                        Console.WriteLine("Invalid input value.");
                        return;
                    }
                }

                IList <Image> imageList = ImageIOHelper.GetImageList(imageFile);

                OCR <Image> ocrEngine = new OCRImages();
                ocrEngine.PSM = psm;
                string result = ocrEngine.RecognizeText(imageList, curLangCode);

                // postprocess to correct common OCR errors
                result = Processor.PostProcess(result, curLangCode);
                // correct common errors caused by OCR
                result = TextUtilities.CorrectOCRErrors(result);
                // correct letter cases
                result = TextUtilities.CorrectLetterCases(result);

                using (StreamWriter sw = new StreamWriter(outputFile.FullName + ".txt", false, new System.Text.UTF8Encoding()))
                {
                    sw.Write(result);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e.Message);
            }
        }