识别图片中文字(百度AI)

 这个是百度官方的文档         https://ai.baidu.com/docs#/OCR-API/top

   通用的文字识别,如果是其他的含生僻字/含位置信息的版本,请参考官方的文档,只需要在请求时发送不同的参数即可

   根据文档简单的使用一般处理程序完成 百度 中文字识别的功能,下面是主方法:

    private static String clientId = "**************"; // 百度云中开通对应服务应用的 API Key 建议开通应用的时候多选服务
// 百度云中开通对应服务应用的 Secret Key
private static String clientSecret = "***************";//注意不要加空格 public void ProcessRequest(HttpContext context) {
context.Response.ContentType = "text/plain";string token_ = getAccessToken();
string return_font = general(token_, "123.jpg");
//string json = JsonConvert.SerializeObject(return_font);
context.Response.Write(return_font);
}
    /// <summary>
/// 获取token
/// </summary>
/// <returns></returns>
public static String getAccessToken()
{
String authHost = "https://aip.baidubce.com/oauth/2.0/token";
HttpClient client = new HttpClient();
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)); HttpResponseMessage response = client.PostAsync(authHost, new FormUrlEncodedContent(paraList)).Result;
String result = response.Content.ReadAsStringAsync().Result;
string token_ = result.Split(',')[].Split(':')[];
token_ = token_.Substring(, token_.Length-); return token_;
}
/// <summary>
/// 获取图片中的文字
/// </summary>
/// <param name="token"></param>
/// <param name="image_path">图片路径</param>
/// <returns></returns>
public static string general(string token,string image_path)
{
string strbaser64 = base64(image_path);//将图片转为base64 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, , buffer.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string result = reader.ReadToEnd();
return result;
} /// <summary>
/// 将图片转为base64
/// </summary>
/// <param name="image_name"></param>
/// <returns></returns>
public static string base64(string image_name) {
FileInfo file = new FileInfo(image_name);
var stream = file.OpenRead();
byte[] buffer = new byte[file.Length];
//读取图片字节流
stream.Read(buffer, , Convert.ToInt32(file.Length));
return Convert.ToBase64String(buffer); }

 其中两个公共变量分别是如下图:

识别图片中文字(百度AI)

以上就是全部代码了,返回值就是识别的图片中的文字。

上一篇:2017.4.4 TCP/IP三次握手,四次挥手


下一篇:VirtualBox开发环境的搭建详解(转)