赞
踩
https://download.csdn.net/download/yuyang076000/19285866
如下:
// 一键识别按钮 private void bt_shibie_Click(object sender, EventArgs e) { // 如果picPropList没有数据,返回 if (this.picPropList.Count <= 0) { MessageBox.Show("木有数据哇"); return; } // 设置 文字识别方式 if (radioButton_tongyong.Checked) { this.shiBIeJieKou = ShiBieJieKou.通用文字识别; } else if (radioButton_gaojingdu.Checked) { this.shiBIeJieKou = ShiBieJieKou.通用文字识别高精度; } // 后台开始识别 backgroundWorker1.RunWorkerAsync(this.shiBIeJieKou); }
代码如下:
// 后台工作 private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { StringBuilder jieguo = new StringBuilder(); for (int i = 0; i < this.picPropList.Count; i++) { DateTime startTime = DateTime.Now;//方法开始时间 // 文字识别,并储存到结果jieguo jieguo.Append(this.OCRClass.OCR(client, this.picPropList[i].address, (ShiBieJieKou)(e.Argument))).Append("\r\n----------------------------------------\r\n"); DateTime endTime = DateTime.Now;//方法结束时间 // 计算时间差,如果一次识别小于500毫秒,就等一会.因为百度文字识别qps每秒最多请求2次,所以弄了一个这个 TimeSpan tS = endTime - startTime; if (tS.TotalMilliseconds < 500) { Thread.Sleep(500 - (int)tS.TotalMilliseconds); //Console.WriteLine("等待了:" + (500 - (int)tS.TotalMilliseconds).ToString()); } startTime = DateTime.Now; //向ProgressChanged报告进度 backgroundWorker1.ReportProgress(i); } e.Result = jieguo.ToString(); }
代码如下(示例):
// 报告进度 private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { // 显示进度 labeljindu.Text = e.ProgressPercentage + 1 + @"/" + this.picPropList.Count; // 是否识别为true this.picPropList[e.ProgressPercentage].isOCR = true; // 选中表格相应行 dataGridViewMain.Rows[e.ProgressPercentage].Selected = true; } // 完成任务 private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { // 显示最后文本,也可以做成边识别,边显示.我这个是最后才显示 this.OCRClass.theOCRstr = e.Result.ToString(); this.tb_jieguo.Text = this.OCRClass.theOCRstr; }
// 鼠标截图识别 private void bt_screenshotOCR_Click(object sender, EventArgs e) { jieTu(); } private void jieTu() { YY.Screenshot.Screenshot screenshot = new YY.Screenshot.Screenshot(); // 创建文件夹 if (!Directory.Exists("截图保存")) { Directory.CreateDirectory("截图保存"); } // 截图,并保存截图 string address = Directory.GetCurrentDirectory() + "\\截图保存\\" + DateTime.Now.ToString("yyyy-MM-dd-hh-mm-sss") + ".png"; Image m = screenshot.start(this, true, false); // 开始截图 m.Save(address); // 保存截图 // 在上面显示,图片地址 OCRClass.address = address; this.tb_address.Text = OCRClass.address; // 读取图片大小 FileInfo f = new FileInfo(OCRClass.address); string file_size = (f.Length / 1024).ToString(); // 添加到List,用于识别 this.picPropList.Add(new PicProp(address, false, file_size)); }
/// <summary> /// 文字识别! /// </summary> /// <param name="client"></param> /// <param name="address"></param> /// <returns></returns> public string OCR(Baidu.Aip.Ocr.Ocr client, string address,ShiBieJieKou shiBieJieKou) { byte[] image = File.ReadAllBytes(address); StringBuilder resultStr = new StringBuilder(); JObject result = null; // 调用通用文字识别, 图片参数为本地图片,可能会抛出网络等异常,请使用try/catch捕获 if (shiBieJieKou == ShiBieJieKou.通用文字识别) { try { result = client.GeneralBasic(image); } catch (Exception) { throw; } } if (shiBieJieKou == ShiBieJieKou.通用文字识别高精度) { try { result = client.AccurateBasic(image); } catch (Exception) { throw; } } // 如果返回错误 if (result.Property("error_code") != null) { resultStr.Append(result["error_msg"]); } // 返回正确,提取文字 if (result.Property("words_result") != null) { foreach (var item in result["words_result"]) { resultStr.Append(item["words"] + "\r\n"); } } return resultStr.ToString(); }
自己精心制作的哦
下载地址:
https://download.csdn.net/download/yuyang076000/19285866
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。