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); } }
public override async Task <string> OCRProcessAsync(Bitmap img) { if (img == null || langCode == null || langCode == "") { errorInfo = "Param Missing"; return(null); } string host = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=" + accessToken; HttpWebRequest request = WebRequest.CreateHttp(host); request.Method = "post"; request.Timeout = 8000; // 图片的base64编码 string base64 = ImageProcFunc.GetFileBase64(img); String str = "language_type=" + langCode + "&image=" + HttpUtility.UrlEncode(base64); byte[] buffer = Encoding.Default.GetBytes(str); request.ContentLength = buffer.Length; using (var requestStream = request.GetRequestStream()) { await requestStream.WriteAsync(buffer, 0, buffer.Length); } string result; try { HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync(); StreamReader reader = new StreamReader(response.GetResponseStream()); result = await reader.ReadToEndAsync(); response.Close(); } catch (WebException ex) { errorInfo = ex.Message; return(null); } StringBuilder sb = new StringBuilder(); BaiduOCRresOutInfo oinfo = JsonConvert.DeserializeObject <BaiduOCRresOutInfo>(result); if (oinfo.words_result != null) { for (int i = 0; i < oinfo.words_result_num; i++) { sb.AppendLine(oinfo.words_result[i].words); } return(sb.ToString()); } else { errorInfo = "UnknownError"; return(null); } }
/// <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)); }
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); } }
/// <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); } }
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); } }
public override Task <string> OCRProcessAsync(Bitmap img) { try { Process p = Process.Start(new ProcessStartInfo() { FileName = path, Arguments = "- - " + args, UseShellExecute = false, CreateNoWindow = true, RedirectStandardInput = true, RedirectStandardOutput = true, RedirectStandardError = true, StandardOutputEncoding = Encoding.UTF8 }); var imgdata = ImageProcFunc.Image2Bytes(img); p.StandardInput.BaseStream.Write(imgdata, 0, imgdata.Length); p.StandardInput.Close(); p.WaitForExit(); string err = p.StandardError.ReadToEnd(); if (err.ToLower().Contains("error")) { errorInfo = err; return(Task.FromResult <string>(null)); } string result = p.StandardOutput.ReadToEnd(); p.Dispose(); return(Task.FromResult(result)); } catch (Exception ex) { errorInfo = ex.Message; return(Task.FromResult <string>(null)); } }
public override async Task <string> OCRProcessAsync(Bitmap img) { if (img == null || langCode == null || langCode == "") { errorInfo = "Param Missing"; return(null); } byte[] imgdata = ImageProcFunc.Image2Bytes(img); // 计算sign MD5 md5 = MD5.Create(); var ms = new MemoryStream(); var sw = new StreamWriter(ms); sw.Write(appId); sw.Write(BitConverter.ToString(md5.ComputeHash(imgdata)).Replace("-", "").ToLower()); sw.Write(salt); sw.Write("APICUIDmac"); sw.Write(secretKey); sw.Flush(); string sign = BitConverter.ToString(md5.ComputeHash(ms.ToArray())).Replace("-", "").ToLower(); sw.Close(); md5.Dispose(); string endpoint = "https://fanyi-api.baidu.com/api/trans/sdk/picture?cuid=APICUID&mac=mac&salt=" + salt + "&appid=" + appId + "&sign=" + sign + "&to=zh&from=" + langCode; HttpWebRequest request = WebRequest.CreateHttp(endpoint); request.Method = "POST"; request.Timeout = 8000; request.UserAgent = "MisakaTranslator"; const string boundary = "boundary"; request.ContentType = "multipart/form-data;boundary=" + boundary; var rsw = new StreamWriter(request.GetRequestStream()); rsw.WriteLine("--" + boundary); rsw.WriteLine("Content-Disposition: form-data;name=image;filename=data.png"); rsw.WriteLine("Content-Type: application/octet-stream"); rsw.WriteLine("Content-Transfer-Encoding: binary\r\n"); rsw.Flush(); rsw.BaseStream.Write(imgdata, 0, imgdata.Length); rsw.WriteLine("\r\n--" + boundary + "--"); rsw.Close(); try { using (var resp = await request.GetResponseAsync()) { string retStr = new StreamReader(resp.GetResponseStream()).ReadToEnd(); var result = JsonConvert.DeserializeObject <Result>(retStr); if (result.error_code == "0") { return(result.data.sumDst); } else { errorInfo = result.error_code + " " + result.error_msg; return(null); } } } catch (WebException ex) { errorInfo = ex.Message; return(null); } }