
核心代码已足够你使用,若需要完整代码,请至原文与作者联系
选择图片及上传按钮代码如下:
private void button1_Click (object sender, EventArgs e) {
try {
richTextBox1.Text = "";
Openimgfile (); //调用文件打开对话框
this.button1.Text = "上传并提取中...";
pictureBox1.LoadAsync (textBox1.Text);
GeneralBasicDemo ();
调用百度ai接口
} catch (Exception ex) {
MessageBox.Show ("报错,原因:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
打开文件对话框过程代码:
//打开文件模块
public void Openimgfile () {
try {
OpenFileDialog dialog = new OpenFileDialog ();
dialog.Filter = "图片(*.png;*.jpg;*.bmp;*.jpeg)|*.png;*.jpg;*.bmp;*.jpeg";
if (dialog.ShowDialog () == DialogResult.OK) {
this.textBox1.Text = dialog.FileName;
}
} catch (Exception ex) {
MessageBox.Show ("报错,原因:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
调用百度api接口 过程代码:
public void GeneralBasicDemo () {
try {
string Json;
var APP_ID = "123456";
var API_KEY = "123456";
var SECRET_KEY = "123456";
var client = new Baidu.Aip.Ocr.Ocr (API_KEY, SECRET_KEY);
client.Timeout = 60000;
var image = File.ReadAllBytes (textBox1.Text);
var result = client.GeneralBasic (image);
Console.WriteLine (result);
// 如果有可选参数
var options = new Dictionary
{ "detect_direction", "true" },
{ "detect_language", "true" },
{ "probability", "true" }
};
result = client.GeneralBasic (image, options);
//Console.WriteLine(result);
//richTextBox1.Text = result.ToString();
Json = result.ToString ();
Json_get (Json);
this.button1.Text = "选择图片并提取文字";
} catch (Exception ex) {
MessageBox.Show ("报错,原因:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
this.button1.Text = "选择图片并提取文字";
}
}
百度返回的内容是json格式,需要从json中提取words关键字对应的值。
原文:https://blog.tag.gg/showinfo-23-362-0.html
以上就是关于“C#通过百度Ai接口实现图片文字识别核心代码”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm