Exemplo n.º 1
0
        public string OCRProcess(Bitmap img)
        {
            Bitmap processedImg = ImageProcFunc.Auto_Thresholding(img, imgFunc, imgFuncP1, imgFuncP2);

            try
            {
                //img.Save(Environment.CurrentDirectory + "\\temp\\tmp.PNG");
                processedImg.Save(Environment.CurrentDirectory + "\\temp\\tmp.PNG", System.Drawing.Imaging.ImageFormat.Png);
                Process p = new Process();
                // Redirect the output stream of the child process.
                p.StartInfo.UseShellExecute        = false;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.CreateNoWindow         = true;
                p.StartInfo.FileName  = "C:\\Program Files (x86)\\Tesseract-OCR\\tesseract";
                p.StartInfo.Arguments = "-l jpn temp\\tmp.PNG temp\\outputbase";
                p.Start();
                // Do not wait for the child process to exit before
                // reading to the end of its redirected stream.
                // p.WaitForExit();
                // Read the output stream first and then wait.
                string output = p.StandardOutput.ReadToEnd();
                p.WaitForExit();
                byte[] bs     = System.IO.File.ReadAllBytes(Environment.CurrentDirectory + "\\temp\\outputbase.txt");
                string result = Encoding.UTF8.GetString(bs);
                return(result);
            }
            catch (Exception ex)
            {
                errorInfo = ex.Message;
                return(null);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// OCR处理,根据设定的截图区域自动截图后识别
        /// </summary>
        /// <returns>返回识别结果,如果为空可通过GetLastError得到错误提示</returns>
        public Task <string> OCRProcessAsync()
        {
            Bitmap img = ScreenCapture.GetWindowRectCapture(WinHandle, OCRArea, isAllWin);

            if (img == null)
            {
                errorInfo = "未设置截图区域";
                return(null);
            }
            Bitmap processedImg = ImageProcFunc.Auto_Thresholding(img, imgProc);

            return(OCRProcessAsync(processedImg));
        }
Exemplo n.º 3
0
 public string OCRProcess(Bitmap img)
 {
     try {
         Bitmap processedImg = ImageProcFunc.Auto_Thresholding(img, imgFunc, imgFuncP1, imgFuncP2);
         var    page         = TessOCR.Process(processedImg);
         string res          = page.GetText();
         page.Dispose();
         return(res);
     }
     catch (Exception ex) {
         errorInfo = ex.Message;
         return(null);
     }
 }
Exemplo n.º 4
0
 /// <summary>
 /// OCR处理,根据设定的截图区域自动截图后识别
 /// </summary>
 /// <returns>返回识别结果,如果为空可通过GetLastError得到错误提示</returns>
 public string OCRProcess()
 {
     if (OCRArea != null)
     {
         Bitmap img          = new Bitmap(ScreenCapture.GetWindowRectCapture(WinHandle, OCRArea, isAllWin));
         Bitmap processedImg = ImageProcFunc.Auto_Thresholding(img, imgProc);
         return(OCRProcess(new Bitmap(processedImg)));
     }
     else
     {
         errorInfo = "未设置截图区域";
         return(null);
     }
 }
Exemplo n.º 5
0
        public string OCRProcess(Bitmap img)
        {
            if (img == null || langCode == null || langCode == "")
            {
                errorInfo = "Param Missing";
                return(null);
            }
            Bitmap processedImg = ImageProcFunc.Auto_Thresholding(img, imgFunc, imgFuncP1, imgFuncP2);

            string         host     = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=" + accessToken;
            Encoding       encoding = Encoding.Default;
            HttpWebRequest request  = (HttpWebRequest)WebRequest.Create(host);

            request.Method    = "post";
            request.KeepAlive = true;
            // 图片的base64编码
            string base64 = ImageProcFunc.GetFileBase64(processedImg);
            String str    = "language_type=" + langCode + "&image=" + HttpUtility.UrlEncode(base64);

            byte[] buffer = encoding.GetBytes(str);
            request.ContentLength = buffer.Length;
            request.GetRequestStream().Write(buffer, 0, buffer.Length);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader    reader   = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            string          result   = reader.ReadToEnd();

            string             ret   = "";
            BaiduOCRresOutInfo oinfo = JsonConvert.DeserializeObject <BaiduOCRresOutInfo>(result);

            if (oinfo.words_result != null)
            {
                for (int i = 0; i < oinfo.words_result_num; i++)
                {
                    ret = ret + oinfo.words_result[i].words + "\n";
                }
                return(ret);
            }
            else
            {
                errorInfo = "UnknownError";
                return(null);
            }
        }