利用百度AI开放平台的API对于本地图片识别图片上的文字,首先在百度云-管理中心登录百度账号,创建应用获取API Key和Secret Key,因为调用API时必须在URL中带上accesss_token参数,这两个Key是要获取accesss_token的必要参数。
百度AI平台上有示例代码,在代码中将API Key和Secret Key更换成新建应用的。
在这里需要做一个提示,示例代码中将图片转64位的方法已经无法使用,我将该类的包导入查看该类的定义,但是这个方法已经无法使用,下面是另外一种转码的方法。
完整代码如下:通用文字识别(位置信息版)
using org.apache.commons.io; using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Net.Http; using System.Text; using System.Web; namespace com.baidu.ai { class OCRTest { //获取AccessToken public class AccessToken { // 百度云中开通对应服务应用的 API Key 建议开通应用的时候多选服务 private static String clientId = "14Q83HgXFdwWsyZzWYI4XTG8"; // 百度云中开通对应服务应用的 Secret Key private static String clientSecret = "0kGanrWVyUBEA7R5wruIbiCrhLG6lLAR"; public String getAccessToken() { String authHost = "https://aip.baidubce.com/oauth/2.0/token"; HttpClient client = new HttpClient(); //创建http客户端 List<KeyValuePair<String, String>> paraList = new List<KeyValuePair<string, string>>(); paraList.Add(new KeyValuePair<string, string>("grant_type", "client_credentials")); paraList.Add(new KeyValuePair<string, string>("client_id", clientId)); paraList.Add(new KeyValuePair<string, string>("client_secret", clientSecret)); //以异步操作将 POST 请求发送给指定 URI。(URI,发送到服务器的 HTTP 请求内容) HttpResponseMessage response = client.PostAsync(authHost, new FormUrlEncodedContent(paraList)).Result; String token_result = response.Content.ReadAsStringAsync().Result; Console.WriteLine(token_result); return token_result; } } //通用文字识别(含位置信息版) public class General { public string general() { //获取AccessToken AccessToken at = new AccessToken(); string token = at.getAccessToken(); //获取图片64位编码 //string strbaser64 = FileUtils.getFileBase64("/work/ai/images/ocr/general.jpeg"); // 图片的base64编码 string strbaser64 = Convert.ToBase64String(System.IO.File.ReadAllBytes("E:\\Visual Reality workspace\\MRBook\\OCR\\Images\\7.jpg")); string host = "https://aip.baidubce.com/rest/2.0/ocr/v1/general?access_token=" + token; Encoding encoding = Encoding.Default; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host); request.Method = "post"; request.ContentType = "application/x-www-form-urlencoded"; request.KeepAlive = true; String str = "image=" + HttpUtility.UrlEncode(strbaser64); 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.Default); string result = reader.ReadToEnd(); Console.WriteLine("通用文字识别:"); Console.WriteLine(result); return result; } } public static void Main() { General g = new General(); g.general(); Console.ReadKey(); } } }
运行效果