Esempio n. 1
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();
        }
Esempio n. 2
0
        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();
            }
        }
Esempio n. 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();
        }
Esempio n. 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);
        }
Esempio n. 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);
        }
Esempio n. 6
0
        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();
        }
Esempio n. 7
0
        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();
                }
            }
        }
Esempio n. 8
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("");
        }
Esempio n. 9
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();
        }