private static JObject JudgeMethod(ApiVersion apiVersion, byte[] image) { JObject result; switch (apiVersion) { case ApiVersion.GeneralBasic: result = client.GeneralBasic(image); break; case ApiVersion.General: result = client.General(image); break; case ApiVersion.AccurateBasic: result = client.AccurateBasic(image); break; case ApiVersion.Accurate: result = client.Accurate(image); break; case ApiVersion.Numbers: result = client.Numbers(image); break; default: result = null; break; } return(result); }
public void GeneralDemo() { var client = new Baidu.Aip.Ocr.Ocr(API_Key, SECRE_KEY); client.Timeout = 60000; var image = File.ReadAllBytes(ImagePath); OutResult = client.General(image); }
void ImageToWords(Bitmap bitmap) { // Baidu.Com.Model.AccessTokenResponse accessTokenResponse = Baidu.Com.AI.AccessToken.GetAccessToken(); byte[] imageBase64; using (MemoryStream ms = new MemoryStream()) { bitmap.Save(ms, ImageFormat.Bmp); imageBase64 = ms.GetBuffer(); } bitmap.Dispose(); Baidu.Aip.Ocr.Ocr ocr = new Baidu.Aip.Ocr.Ocr(Baidu.Com.AI.AccessToken.ClientId, Baidu.Com.AI.AccessToken.ClientSecret); var jObject = ocr.General(imageBase64); Baidu.Com.Model.GeneralResponse generalResponse = jObject.ToObject <Baidu.Com.Model.GeneralResponse>(); textBox1.Text = ""; foreach (var item in generalResponse.words_result) { textBox1.Text += item.words; } }
private void addimg(string tpath) { //读取编号 int tcnt = getcnt() + 1; string filename = ""; //OCR 添加index using (System.IO.StreamWriter file = new System.IO.StreamWriter(indexpath, true, Encoding.GetEncoding("GBK"))) { var res = client.General(File.ReadAllBytes(tpath)); string st = tcnt.ToString() + "." + tpath.Split('.')[1]; filename = st; st += "@*@"; foreach (var str in res["words_result"]) { st += str["words"].ToString() + "@*@"; } Console.WriteLine(st); file.WriteLine(st); } //添加图片 FileStream fs = new FileStream(path + "/" + filename, FileMode.Create); BinaryWriter bw = new BinaryWriter(fs); bw.Write(File.ReadAllBytes(tpath), 0, File.ReadAllBytes(tpath).Length); bw.Close(); fs.Close(); //写入编号 using (System.IO.StreamWriter file = new System.IO.StreamWriter(cntpath, false, Encoding.GetEncoding("GBK"))) { file.WriteLine(tcnt); } //更新List updatelist(); updatelist(); }
static void Main(string[] args) { System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); //DateTime start = DateTime.Now; //IntPtr ptr = (IntPtr)0x20BA6; //Rect AppRect = new Rect(); //GetWindowRect(ptr, ref AppRect); //Rectangle rect = new Rectangle(AppRect.Left, AppRect.Top, (AppRect.Right - AppRect.Left), (AppRect.Bottom - AppRect.Top)); //Bitmap bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb); //Graphics g = Graphics.FromImage(bmp); //g.CopyFromScreen(rect.Left, rect.Top, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy); //// make sure temp directory is there or it will throw. //bmp.Save(@"c:\test.jpg", ImageFormat.Jpeg); //List<IntPtr> listWnd = new List<IntPtr>(); //EnumChildWindows(ptr, new EnumWindowProc(delegate (IntPtr hwnd, IntPtr lParam) //{ // int length = GetTextBoxTextLength(hwnd); // StringBuilder sb = new StringBuilder(); // GetWindowText(hwnd, sb, length + 1); // Console.WriteLine(sb.ToString()); // return true; //}), (IntPtr)0); //var list = GetAllChildrenWindowHandles(ptr, 1000); //foreach(var item in list) //{ // Console.WriteLine(item); // EnumChildWindows(item, new EnumWindowProc(delegate (IntPtr hwnd, IntPtr lParam) // { // int length = GetTextBoxTextLength(hwnd); // StringBuilder sb = new StringBuilder(); // GetWindowText(hwnd, sb, length + 1); // Console.WriteLine(sb.ToString()); // return true; // }), (IntPtr)0); //} var fileList = new string[] { "A", "B", "C", "D" }; var APP_ID = "14565074"; var API_KEY = "nN5shGjIsPuVK0dN3cZP56rN"; var SECRET_KEY = "pyik6Od7p1kgnL4BdX3MefAqjHM2SNHd"; var client = new Baidu.Aip.Ocr.Ocr(API_KEY, SECRET_KEY); foreach (var fItem in fileList) { //var ocr = new TesseractEngine("./tessdata", "chi_sim", EngineMode.Default); //ocr.SetVariable("tessedit_char_whitelist", "委买卖一"); var img = new Bitmap(@"C:\Saved Pictures\" + fItem + ".png"); var image = File.ReadAllBytes(@"C:\Saved Pictures\" + fItem + ".png"); // 调用通用文字识别, 图片参数为本地图片,可能会抛出网络等异常,请使用try/catch捕获 var options = new Dictionary <string, object> { { "recognize_granularity", "big" }, { "language_type", "CHN_ENG" }, { "detect_direction", "true" }, { "detect_language", "true" }, { "vertexes_location", "true" }, { "probability", "true" } }; // 带参数调用通用文字识别(含位置信息版), 图片参数为本地图片 var result = client.General(image, options); var show = result["words_result"].ToList(); var one = show.Where(a => a["words"].ToString().StartsWith("委买队列")).FirstOrDefault(); Pen myPen = new Pen(System.Drawing.Color.BurlyWood, 3); var g = Graphics.FromImage(img); int width = Convert.ToUInt16(one["location"]["width"].ToString()); int height = Convert.ToUInt16(one["location"]["height"].ToString()); int x = Convert.ToUInt16(one["location"]["left"].ToString()); int y = Convert.ToUInt16(one["location"]["top"].ToString()); g.DrawRectangle(myPen, new Rectangle(x, y, width, height)); Console.WriteLine(one); ////var img = CaptureScreen(1525, 187, 200, 20); //Pen myPen = new Pen(System.Drawing.Color.BurlyWood, 3); //var g = Graphics.FromImage(img); //var page = ocr.Process(img); //using (var iterator = page.GetIterator()) //{ // iterator.Begin(); // do // { // string currentWord = iterator.GetText(PageIteratorLevel.Word); // //do something with bounds // Tesseract.Rect bounds; // iterator.TryGetBoundingBox(PageIteratorLevel.Word, out bounds); // Console.WriteLine("x1" + bounds.X1.ToString()); // Console.WriteLine("Y1" + bounds.Y1.ToString()); // Console.WriteLine("x2" + bounds.X2.ToString()); // Console.WriteLine("y2" + bounds.Y2.ToString()); // Console.WriteLine(currentWord); // //if (currentWord.IndexOf("委") > 0 && (currentWord.IndexOf("买") > 0 || currentWord.IndexOf("卖") > 0)) // //{ // // g.DrawRectangle(myPen, new Rectangle(bounds.X1, bounds.Y1, bounds.X2, bounds.Y2)); // //} // if (currentWord.IndexOf("一") > 0 && (currentWord.IndexOf("买") > 0 || currentWord.IndexOf("卖") > 0)) // { // g.DrawRectangle(myPen, new Rectangle(bounds.X1, bounds.Y1, bounds.X2, bounds.Y2)); // } // } // while (iterator.Next(PageIteratorLevel.Word)); //} Bitmap bmp = new Bitmap(img); img.Dispose(); bmp.Save(@"C:\Saved Pictures\" + fItem + "_b.png"); // } //var result = page.GetText(); //Console.WriteLine(result); //var ProcessList = Process.GetProcesses(); //var DzhProcessId = 0; //foreach (var item in ProcessList) //{ // if (item.MainWindowTitle.IndexOf("Notepad++") > 0 && item.ProcessName == "notepad++") // { // DzhProcessId = item.Id; // break; // } //} //var result = Process.GetProcessById(DzhProcessId).MainWindowHandle; //var hWnd = result; //IntPtr hscrdc = Win32Api.GetWindowDC(hWnd); //Rectangle windowRect = new Rectangle(); //Win32Api.GetWindowRect(hWnd, ref windowRect); //int width = Math.Abs(windowRect.X - windowRect.Width); //int height = Math.Abs(windowRect.Y - windowRect.Height); //IntPtr hbitmap = Win32Api.CreateCompatibleBitmap(hscrdc, width, height); //IntPtr hmemdc = Win32Api.CreateCompatibleDC(hscrdc); //DateTime start = DateTime.Now; //Win32Api.SelectObject(hmemdc, hbitmap); //Win32Api.PrintWindow(hWnd, hmemdc, 0); //Bitmap bmp = Image.FromHbitmap(hbitmap); //DateTime end = DateTime.Now; //Win32Api.DeleteDC(hscrdc);//删除用过的对象 //Win32Api.DeleteDC(hmemdc);//删除用过的对象 //bmp.Save(@"c:\dd"+ DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg"); //Parallel.Invoke(Ocr(bmp, 0), Ocr(bmp, 1), Ocr(bmp, 2)); sw.Stop(); Console.WriteLine("{0}", sw.ElapsedMilliseconds); Console.Read(); }
public void TestMethod1() { var fileList = new string[] { "A", "B", "C", "D" }; var client = new Baidu.Aip.Ocr.Ocr(BaiduHelper.API_KEY, BaiduHelper.SECRET_KEY); foreach (var fItem in fileList) { //var ocr = new TesseractEngine("./tessdata", "chi_sim", EngineMode.Default); //ocr.SetVariable("tessedit_char_whitelist", "委买卖一"); var img = new Bitmap(@"F:\byj\pic\" + fItem + ".png"); var image = File.ReadAllBytes(@"F:\byj\pic\" + fItem + ".png"); // 调用通用文字识别, 图片参数为本地图片,可能会抛出网络等异常,请使用try/catch捕获 var options = new Dictionary <string, object> { { "recognize_granularity", "big" }, { "language_type", "CHN_ENG" }, { "detect_direction", "true" }, { "detect_language", "true" }, { "vertexes_location", "true" }, { "probability", "true" } }; // 带参数调用通用文字识别(含位置信息版), 图片参数为本地图片 var result = client.General(image, options); var show = result["words_result"].ToList(); var one = show.Where(a => a["words"].ToString().StartsWith("委买队列")).FirstOrDefault(); Newtonsoft.Json.Linq.JToken buyone = null; if (one == null) { var AccurateOptions = new Dictionary <string, object> { { "recognize_granularity", "big" }, { "detect_direction", "true" }, { "vertexes_location", "true" }, { "probability", "true" } }; result = client.Accurate(image, AccurateOptions); show = result["words_result"].ToList(); one = show.Where(a => a["words"].ToString().StartsWith("委买队列")).FirstOrDefault(); } if (one == null) { throw new Exception("分析失败 "); } Pen myPen = new Pen(System.Drawing.Color.BurlyWood, 3); var g = Graphics.FromImage(img); int width = Convert.ToUInt16(one["location"]["width"].ToString()); int height = Convert.ToUInt16(one["location"]["height"].ToString()); int x = Convert.ToUInt16(one["location"]["left"].ToString()); int y = Convert.ToUInt16(one["location"]["top"].ToString()); g.DrawRectangle(myPen, new Rectangle(x, y, width, height)); g.DrawRectangle(myPen, new Rectangle(x - 68, y + height, 55, height)); //g.DrawRectangle(myPen, new Rectangle(x - 40, y + 37, 80, 31)); g.DrawRectangle(myPen, new Rectangle(x + 36, y + height, 40, height)); g.DrawRectangle(myPen, new Rectangle(x + 89, y + height, 30, height)); Bitmap bmp = new Bitmap(img); img.Dispose(); bmp.Save(@"F:\byj\pic\" + fItem + "_b.png"); // } }