コード例 #1
0
ファイル: Form1.cs プロジェクト: guochunyang2004/ocr-demo
        private void button2_Click(object sender, EventArgs e)
        {
            openFileDialog1.InitialDirectory = Path.Combine(Application.StartupPath, "test");
            if (openFileDialog1.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            //PictureBox控件显示图片
            pictureBox1.Load(openFileDialog1.FileName);
            //获取用户选择文件的后缀名
            string extension = Path.GetExtension(openFileDialog1.FileName);

            //声明允许的后缀名
            string[] str = new string[] { ".jpg", ".png" };
            if (!str.Contains(extension))
            {
                MessageBox.Show("仅能上传jpg,png格式的图片!");
            }
            else
            {
                AspriseOCR.SetUp();
                AspriseOCR ocr = new AspriseOCR();
                ocr.StartEngine("eng", AspriseOCR.SPEED_FASTEST);
                string s = ocr.Recognize(openFileDialog1.FileName, -1, -1, -1, -1, -1, AspriseOCR.RECOGNIZE_TYPE_ALL, AspriseOCR.OUTPUT_FORMAT_PLAINTEXT);
                label1.Text = s;
                ocr.StopEngine();
            }
        }
コード例 #2
0
        private void ChangetoSearchablePdf(string[] folders)
        {
            string incomefolder  = folders[0];
            string outcomefolder = folders[1];

            AspriseOCR.SetUp();
            AspriseOCR ocr = new AspriseOCR();

            ocr.StartEngine("eng", AspriseOCR.SPEED_FASTEST);
            string[] files = System.IO.Directory.GetFiles(incomefolder, "*.pdf").Select(Path.GetFileName).ToArray();
            foreach (string tmpfile in files)
            {
                try {
                    string fullinputfilename  = incomefolder + @"/" + tmpfile;
                    string fulloutputfilename = outcomefolder + @"/" + tmpfile;
                    ocr.Recognize(fullinputfilename, -1, -1, -1, -1, -1,
                                  AspriseOCR.RECOGNIZE_TYPE_ALL, AspriseOCR.OUTPUT_FORMAT_PDF,
                                  AspriseOCR.PROP_PDF_OUTPUT_FILE, fulloutputfilename,
                                  AspriseOCR.PROP_PDF_OUTPUT_TEXT_VISIBLE, false);
                    Console.WriteLine("File " + fulloutputfilename + "has done transformation yeah");
                    File.Delete(fullinputfilename);
                } catch (Exception ex) {
                    Console.WriteLine(ex + "3333");
                }
            }
            ocr.StopEngine();
        }
コード例 #3
0
        public static void GetOCRpart(string img_path)
        {
            //if (threadOcr != null && threadOcr.IsAlive)
            //{
            //    return;
            //}

            requestLang = "eng";
            if (requestLang == null || requestLang.Length == 0)
            {
                return;
            }
            requestPropsStart     = "";
            requestImgFile        = img_path;
            requestLayout         = "auto";
            requestDataCapture    = true;
            requestAutoRotate     = false;
            requestWordLevel      = false;
            requestOutputFormat   = AspriseOCR.OUTPUT_FORMAT_PLAINTEXT;
            requestPdfHighlight   = true;
            requestRecognizeType  = AspriseOCR.RECOGNIZE_TYPE_ALL;
            requestPropsRecognize = "";


            AspriseOCR.SetUp();
            ocr = new AspriseOCR();
            ocr.StartEngine(currentLang, AspriseOCR.SPEED_FASTEST, "");

            doOcr();
        }
コード例 #4
0
        public AspOCR()
        {
            AspriseOCR.SetUp();
            ocr = new AspriseOCR();
            ocr.StartEngine("eng", AspriseOCR.SPEED_FASTEST);
            IDictionary <String, Boolean> dict = new Dictionary <String, Boolean>();

            dict.Add(AspriseOCR.PROP_OUTPUT_SEPARATE_WORDS, true);
        }
コード例 #5
0
        public static string  ReadImage()
        {
            AspriseOCR.SetUp();
            AspriseOCR ocr = new AspriseOCR();

            ocr.StartEngine("eng", AspriseOCR.SPEED_FASTEST);
            string s = ocr.Recognize("D:\\DouYin\\1539354248783.jpg", -1, -1, -1, -1, -1, AspriseOCR.RECOGNIZE_TYPE_ALL, AspriseOCR.OUTPUT_FORMAT_PLAINTEXT);

            ocr.StopEngine();
            return(s);
        }
コード例 #6
0
ファイル: Form1.cs プロジェクト: xiefengdaxia/caichengyu
        private void btnOcrRead_Click(object sender, EventArgs e)
        {
            //textResults.Text = Marshal.PtrToStringAnsi(OCR(textImage.Text, -1));

            AspriseOCR.SetUp();
            AspriseOCR ocr = new AspriseOCR();

            ocr.StartEngine("eng", AspriseOCR.SPEED_FASTEST);

            string s = ocr.Recognize(textImage.Text, -1, -1, -1, -1, -1,
                                     AspriseOCR.RECOGNIZE_TYPE_ALL, AspriseOCR.OUTPUT_FORMAT_PLAINTEXT);

            textResults.Text = s;
            // process more images here ...

            ocr.StopEngine();
        }
コード例 #7
0
ファイル: frmOcrTest.cs プロジェクト: growdu/CSharpTest
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                //PictureBox控件显示图片
                pictureBox1.Load(openFileDialog.FileName);
                //获取用户选择文件的后缀名
                string extension = Path.GetExtension(openFileDialog.FileName);
                //声明允许的后缀名
                string[] str = new string[] { ".jpg", ".png" };
                if (!str.Contains(extension))
                {
                    MessageBox.Show("仅能上传jpg,png格式的图片!");
                }
                else
                {
                    AspriseOCR.SetUp();
                    AspriseOCR ocr = new AspriseOCR();
                    ocr.StartEngine("chi_sim", AspriseOCR.SPEED_FASTEST);

                    string s = ocr.Recognize(openFileDialog.FileName, -1, -1, -1, -1, -1,
                                             AspriseOCR.RECOGNIZE_TYPE_ALL, AspriseOCR.OUTPUT_FORMAT_PLAINTEXT);
                    //Console.WriteLine("OCR Result: " + s);
                    richTextBox1.Text = s;
                    // process more images here ...

                    ocr.StopEngine();
                    //识别图片文字
                    //var img = new Bitmap(openFileDialog.FileName);

                    //var ocr = new TesseractEngine(@"H:\Tesseract-OCR\tessdata\tesseract-ocr-3.02.chi_sim\tesseract-ocr\tessdata", "chi_sim", EngineMode.TesseractAndCube);
                    //var page = ocr.Process(img);
                    //richTextBox1.Text = page.GetText();
                }
            }
        }
コード例 #8
0
        private void buttonBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();

            // settings
            if (!AspriseOCR.isEmpty(textImage.Text))
            {
                try
                {
                    dialog.InitialDirectory = Path.GetDirectoryName(textImage.Text.Trim());
                }
                catch (Exception exception)
                {
                }
            }
            dialog.Title            = "Please select OCR input image";
            dialog.Filter           = "Image Files(*.BMP;*.GIF;*.JPG;*PNG;*TIF;*TIFF)|*.BMP;*.GIF;*.JPG;*PNG;*TIF;*TIFF|All files (*.*)|*.*";
            dialog.RestoreDirectory = true;
            if (dialog.ShowDialog(this) == DialogResult.OK)
            {
                textImage.Text = dialog.FileName;
            }
        }
コード例 #9
0
        public string imgdo(Bitmap img)
        {
            Bitmap btp = img;

            #region 前期处理
            int Dggay = GetDgGrayValue(btp);
            btp = ClearNoise(btp, Dggay, 1);
            btp = ClearNoise(btp, Dggay, 3);
            btp = ClearNoise(btp, Dggay);
            btp.Save("d:\\去除相关颜色.png");
            #endregion

            //去色

            Color c = new Color();
            int   rr, gg, bb;
            for (int i = 0; i < btp.Width; i++)
            {
                for (int j = 0; j < btp.Height; j++)
                {
                    //取图片当前的像素点
                    c  = btp.GetPixel(i, j);
                    rr = c.R; gg = c.G; bb = c.B;
                    //改变颜色
                    if (rr == 102 && gg == 0 && bb == 0)
                    {
                        //重新设置当前的像素点
                        btp.SetPixel(i, j, Color.FromArgb(255, 255, 255, 255));
                    }
                    if (rr == 153 && gg == 0 && bb == 0)
                    {
                        //重新设置当前的像素点
                        btp.SetPixel(i, j, Color.FromArgb(255, 255, 255, 255));
                    }
                    if (rr == 153 && gg == 0 && bb == 51)
                    {
                        //重新设置当前的像素点
                        btp.SetPixel(i, j, Color.FromArgb(255, 255, 255, 255));
                    }
                    if (rr == 153 && gg == 43 && bb == 51)
                    {
                        //重新设置当前的像素点
                        btp.SetPixel(i, j, Color.FromArgb(255, 255, 255, 255));
                    }
                    if (rr == 255 && gg == 255 && bb == 0)
                    {
                        //重新设置当前的像素点
                        btp.SetPixel(i, j, Color.FromArgb(255, 255, 255, 255));
                    }
                    if (rr == 255 && gg == 255 && bb == 51)
                    {
                        //重新设置当前的像素点
                        btp.SetPixel(i, j, Color.FromArgb(255, 255, 255, 255));
                    }
                }
            }
            //   btp.Save("d:\\去除相关颜色.png");

            //   pictureBox2.Image = Image.FromFile("d:\\去除相关颜色.png");


            //灰度
            Bitmap bmphd = btp;
            for (int i = 0; i < bmphd.Width; i++)
            {
                for (int j = 0; j < bmphd.Height; j++)
                {
                    //取图片当前的像素点
                    var color = bmphd.GetPixel(i, j);

                    var gray = (int)(color.R * 0.001 + color.G * 0.700 + color.B * 0.250);

                    //重新设置当前的像素点
                    bmphd.SetPixel(i, j, Color.FromArgb(gray, gray, gray));
                }
            }
            // bmphd.Save("d:\\灰度.png");
            //pictureBox27.Image = Image.FromFile("d:\\灰度.png");


            //二值化
            Bitmap     erzhi = bmphd;
            Bitmap     orcbmp;
            int        nn   = 3;
            int        w    = erzhi.Width;
            int        h    = erzhi.Height;
            BitmapData data = erzhi.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
            unsafe
            {
                byte *p = (byte *)data.Scan0;
                byte[,] vSource = new byte[w, h];
                int offset = data.Stride - w * nn;

                for (int y = 0; y < h; y++)
                {
                    for (int x = 0; x < w; x++)
                    {
                        vSource[x, y] = (byte)(((int)p[0] + (int)p[1] + (int)p[2]) / 3);
                        p            += nn;
                    }
                    p += offset;
                }
                erzhi.UnlockBits(data);

                Bitmap     bmpDest  = new Bitmap(w, h, PixelFormat.Format24bppRgb);
                BitmapData dataDest = bmpDest.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
                p      = (byte *)dataDest.Scan0;
                offset = dataDest.Stride - w * nn;
                for (int y = 0; y < h; y++)
                {
                    for (int x = 0; x < w; x++)
                    {
                        p[0] = p[1] = p[2] = (int)vSource[x, y] > 161 ? (byte)255 : (byte)0;
                        //p[0] = p[1] = p[2] = (int)GetAverageColor(vSource, x, y, w, h) > 50 ? (byte)255 : (byte)0;
                        p += nn;
                    }
                    p += offset;
                }
                bmpDest.UnlockBits(dataDest);

                orcbmp = bmpDest;
                orcbmp.Save(Environment.CurrentDirectory + "\\二值化.png");
                //  pictureBox29.Image = Image.FromFile("d:\\二值化.png");
            }

            //OCR的值
            if (orcbmp != null)
            {
                AspriseOCR.SetUp();
                AspriseOCR ocr = new AspriseOCR();
                ocr.StartEngine("eng", AspriseOCR.SPEED_FASTEST);
                string s = ocr.Recognize(Environment.CurrentDirectory + "\\二值化.png", -1, -1, -1, -1, -1, AspriseOCR.RECOGNIZE_TYPE_ALL, AspriseOCR.OUTPUT_FORMAT_PLAINTEXT);
                ocr.StopEngine();
                return(s);
                //string result = Ocr(orcbmp);
                //label32.Text = result.Replace("\n", "\r\n").Replace(" ", "");
            }
            return("");
        }
コード例 #10
0
        private void asprise_init()
        {
            bool browsed = false;
            int  count   = 0;

            string dllFilePath = AspriseOCR.getOcrDllPath();

            if (dllFilePath == null)
            {
                string parentFolder = AspriseOCR.detectOcrDllInParentFolders();
                if (parentFolder != null)
                {
                    AspriseOCR.addToSystemPath(parentFolder);
                    log("Folder containing ocr dll detected: " + parentFolder);
                }
            }

            String lang = GetLastLang();

            if (lang == null || lang.Length == 0)
            {
                comboLang.SelectedIndex = 0;
                currentLang             = "eng";
            }
            else
            {
                for (int i = 0; i < comboLang.Items.Count; i++)
                {
                    if (comboLang.Items[i].ToString().Equals(lang))
                    {
                        comboLang.SelectedIndex = i;
                        break;
                    }
                }
                currentLang = lang;
            }

            // Let user browse the ocr dll if it is not found in PATH.
            while (true)
            {
                dllFilePath = AspriseOCR.getOcrDllPath();
                if (dllFilePath == null)
                {
                    log("OCR dll (" + AspriseOCR.getOcrDllName() + ") is not found in PATH.");
                    OpenFileDialog fileDialog = new OpenFileDialog();
                    fileDialog.RestoreDirectory = true;
                    fileDialog.Title            = "Please select " + AspriseOCR.getOcrDllName();
                    fileDialog.FileName         = AspriseOCR.getOcrDllName();
                    if (fileDialog.ShowDialog(this) == DialogResult.OK)
                    {
                        log("User browsed dll: " + fileDialog.FileName);
                        AspriseOCR.addToSystemPath(Path.GetDirectoryName(fileDialog.FileName));
                        browsed = true;
                    }
                    count++;
                    if (count == 2)
                    {
                        showMessageBox("OCR DLL not found. ", "Error");
                        break;
                    }
                }
                else
                {
                    log("OCR dll found: " + dllFilePath);
                    if (browsed)
                    {
                        log("Please consider copy " + AspriseOCR.getOcrDllName() + " to directory: " +
                            Directory.GetCurrentDirectory());
                    }

                    log(AspriseOCR.GetLibraryVersion());

                    try
                    {
                        log("Starting OCR engine ...");
                        AspriseOCR.SetUp();
                        ocr = new AspriseOCR();
                        ocr.StartEngine(currentLang, AspriseOCR.SPEED_FASTEST);
                        log("OCR engine started successfully.");
                    }
                    catch (Exception e)
                    {
                        log("ERROR: Failed to start OCR engine: " + e);
                        log(e.StackTrace);
                    }
                    break;
                }
            }

            // user preference
            textImage.Text = GetLastImagePath();
        }
コード例 #11
0
 private static string GetLastLang()
 {
     return(AspriseOCR.firstNonNull(Settings.Default["LAST_LANG"], "eng").ToString());
 }
コード例 #12
0
 private static string GetLastImagePath()
 {
     return(AspriseOCR.firstNonNull(Settings.Default["IMAGE_FILE"], "").ToString());
 }
コード例 #13
0
        void doOcr()
        {
            if (textImage.Text.Trim().Length == 0)
            {
                showMessageBox("Please select an input image first.", "Error");
                return;
            }

            if (!File.Exists(textImage.Text.Trim()))
            {
                showMessageBox("Image file does not exist: " + textImage.Text, "Error");
                return;
            }

            if (!langOnUI.Equals(currentLang))
            {
                ocr.StopEngine();
                currentLang = null;

                ocr = new AspriseOCR();
                ocr.StartEngine(langOnUI, AspriseOCR.SPEED_FASTEST);
                currentLang = langOnUI;
            }

            if (ocr == null || !ocr.IsEngineRunning)
            {
                showMessageBox("OCR engine is not running", "Error");
                return;
            }

            if (!checkRecognizeText.Checked && !checkRecognizeBarcodes.Checked)
            {
                showMessageBox("Please check at least one of 'Text', 'Barcodes'", "Warn");
                return;
            }

            string recognizeType = AspriseOCR.RECOGNIZE_TYPE_ALL;

            if (!checkRecognizeText.Checked)
            {
                recognizeType = AspriseOCR.RECOGNIZE_TYPE_BARCODE;
            }
            if (!checkRecognizeBarcodes.Checked)
            {
                recognizeType = AspriseOCR.RECOGNIZE_TYPE_TEXT;
            }

            string outputFormat = AspriseOCR.OUTPUT_FORMAT_PLAINTEXT;

            if (radioOutputXml.Checked)
            {
                outputFormat = AspriseOCR.OUTPUT_FORMAT_XML;
            }
            if (radioOutputPdf.Checked)
            {
                outputFormat = AspriseOCR.OUTPUT_FORMAT_PDF;
            }

            Dictionary <string, string> dict = new Dictionary <string, string>();
            string pdfOutputFile             = null;

            if (outputFormat.Equals(AspriseOCR.OUTPUT_FORMAT_PDF))
            {
                pdfOutputFile = Path.Combine(Directory.GetCurrentDirectory(), DateTime.Now.ToString("O").Replace(':', '-') + ".pdf");
                dict.Add(AspriseOCR.PROP_PDF_OUTPUT_FILE, pdfOutputFile);
                dict.Add(AspriseOCR.PROP_PDF_OUTPUT_TEXT_VISIBLE, "true");
                dict.Add(AspriseOCR.PROP_PDF_OUTPUT_IMAGE_FORCE_BW, "true");
            }

            log("OCR in progress, please stand by ...", true);
            DateTime timeStart = DateTime.Now;
            // Performs the actual recognition
            string   s       = ocr.Recognize(textImage.Text.Trim(), -1, -1, -1, -1, -1, recognizeType, outputFormat, dict);
            DateTime timeEnd = DateTime.Now;

            // open pdf file
            if (outputFormat.Equals(AspriseOCR.OUTPUT_FORMAT_PDF))
            {
                if (s != null && s.Trim().Length > 0)
                {
                    log(s + "\nPDF file: " + pdfOutputFile, true);
                }
                else
                {
                    log("PDF file created: " + pdfOutputFile, true);
                    try
                    {
                        System.Diagnostics.Process.Start(@pdfOutputFile);
                    }
                    catch (Exception e)
                    {
                        // ignore
                    }
                }
                log("For illustration purpose, text has been rendered in color instead of transparent.");
            }
            else
            {
                log(s, true);
            }

            // user preference
            SetLastImagePath(textImage.Text);
            SetLastLang(currentLang);
        }
コード例 #14
0
        static void doOcr()
        {
            if (requestImgFile.Length == 0)
            {
                return;
            }

            if (!File.Exists(requestImgFile))
            {
                return;
            }

            if (!requestLang.Equals(currentLang) || !requestPropsStart.Equals(currentEngineStartProps))
            {
                ocr.StopEngine();
                currentLang = null;

                ocr = new AspriseOCR();
                ocr.StartEngine(requestLang, AspriseOCR.SPEED_FASTEST, requestPropsStart);
                currentLang             = requestLang;
                currentEngineStartProps = requestPropsStart;
            }

            if (ocr == null || !ocr.IsEngineRunning)
            {
                return;
            }

            Dictionary <string, string> dict = new Dictionary <string, string>();

            dict.Add(AspriseOCR.PROP_OUTPUT_SEPARATE_WORDS, requestWordLevel ? "true" : "false");
            dict.Add(AspriseOCR.PROP_PAGE_TYPE, requestLayout);
            dict.Add(AspriseOCR.PROP_TABLE_SKIP_DETECTION, requestDataCapture ? "false" : "true");
            dict.Add(AspriseOCR.PROP_IMG_PREPROCESS_TYPE, requestAutoRotate ? AspriseOCR.PROP_IMG_PREPROCESS_TYPE_DEFAULT_WITH_ORIENTATION_DETECTION : AspriseOCR.PROP_IMG_PREPROCESS_TYPE_DEFAULT);

            string pdfOutputFile = null;

            if (requestOutputFormat.Equals(AspriseOCR.OUTPUT_FORMAT_PDF))
            {
                pdfOutputFile = Path.Combine(Directory.GetCurrentDirectory(), DateTime.Now.ToString("O").Replace(':', '-') + ".pdf");
                dict.Add(AspriseOCR.PROP_PDF_OUTPUT_FILE, pdfOutputFile);
                dict.Add(AspriseOCR.PROP_PDF_OUTPUT_TEXT_VISIBLE, "true");
                dict.Add(AspriseOCR.PROP_PDF_OUTPUT_IMAGE_FORCE_BW, "true");
            }
            string rtfOutputFile = null;

            if (requestOutputFormat.Equals(AspriseOCR.OUTPUT_FORMAT_RTF))
            {
                rtfOutputFile = Path.Combine(Directory.GetCurrentDirectory(), DateTime.Now.ToString("O").Replace(':', '-') + ".rtf");
                dict.Add(AspriseOCR.PROP_RTF_OUTPUT_FILE, rtfOutputFile);
            }

            string allRecogProps = AspriseOCR.dictToString(dict) +
                                   (requestPropsRecognize == null || requestPropsRecognize.Trim().Length == 0 ?
                                    "" : AspriseOCR.CONFIG_PROP_SEPARATOR + requestPropsRecognize);
            String status = "Recognizing " + requestRecognizeType + " to output as " + requestOutputFormat + " on image: " + requestImgFile + " ...\n" +
                            "OCR engine start props: " + requestPropsStart + "\n" +
                            "OCR recognition props:  " + allRecogProps + "\n" +
                            "Please stand by ...";


            DateTime timeStart = DateTime.Now;

            // Performs the actual recognition
            ORCResult = ocr.Recognize(requestImgFile, -1, -1, -1, -1, -1, requestRecognizeType, requestOutputFormat, AspriseOCR.dictToString(dict) + AspriseOCR.CONFIG_PROP_SEPARATOR + requestPropsRecognize);
            DateTime timeEnd = DateTime.Now;

            // open pdf file
            if (requestOutputFormat.Equals(AspriseOCR.OUTPUT_FORMAT_PDF))
            {
                if (ORCResult != null && ORCResult.Trim().Length > 0)
                {
                }
                else
                {
                    try
                    {
                        System.Diagnostics.Process.Start(@pdfOutputFile);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                }
            }
            else if (requestOutputFormat.Equals(AspriseOCR.OUTPUT_FORMAT_RTF))
            {
                if (ORCResult != null && ORCResult.Trim().Length > 0)
                {
                }
                else
                {
                    try
                    {
                        System.Diagnostics.Process.Start(@rtfOutputFile);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                }
            }
            else if (requestOutputFormat.Equals(AspriseOCR.OUTPUT_FORMAT_XML))
            {
                try
                {
                    string xmlOutputFile = Path.Combine(Directory.GetCurrentDirectory(), DateTime.Now.ToString("O").Replace(':', '-') + ".xml");
                    File.WriteAllText(xmlOutputFile, ORCResult, Encoding.UTF8);
                    AspriseOCR.saveAocrXslTo(Directory.GetCurrentDirectory(), false);
                    if (File.Exists(xmlOutputFile))
                    {
                        System.Diagnostics.Process.Start(@xmlOutputFile);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
            else
            {
            }

            // user preference
            //saveSettings();
        }