C#百度OCR-本地和网络图片识别文字
程序员文章站
2022-03-16 18:52:28
...
源码地址:https://download.csdn.net/download/horseroll/10508204
使用效果:
调用的是百度的API接口。API文档:http://ai.baidu.com/docs#/OCR-API/top
1.首先先根据API key和Secret key得到token
token = getAccessToken();
// 调用getAccessToken()获取的 access_token建议根据expires_in 时间 设置缓存
// 返回token示例
public static String TOKEN = "24.adda70c11b9786206253ddb70affdc46.2592000.1493524354.282335-1234567";
// 百度云中开通对应服务应用的 API Key 建议开通应用的时候多选服务
private static String clientId = "cohIahxAt7HveHLYSHYK6G5N";
// 百度云中开通对应服务应用的 Secret Key
private static String clientSecret = "e8SAsDIWSK9NPUKviYiPQNlfaVDXQSY5";
public static String getAccessToken()
{
String authHost = "https://aip.baidubce.com/oauth/2.0/token";
//string result = ToolData.HttpPost(authHost, "grant_type=client_credentials&client_id="+clientId+"&client_secret="+clientSecret);
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;
return result;
}
2.网络图片的识别(post提交)
string httppicture = HttpPost("https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic", "access_token=" + token + "&url=" + "图片的网络地址");//httppost方法就是异步post方法
3.本地图片的识别(post提交)
string base64 = UrlEncode(ImgToBase64String(Image.FromFile("本地图片路径地址")));
string localpicure = HttpPost("https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic", "access_token=" + token + "&image=" + base64);
public String ImgToBase64String(Image bmp)
{
String strbaser64 = String.Empty;
var btarr = convertByte(bmp);
strbaser64 = Convert.ToBase64String(btarr);
return strbaser64;
}
/// <summary>
/// Image转byte[]
/// </summary>
/// <param name="img">Img格式数据</param>
/// <returns>byte[]格式数据</returns>
public static byte[] convertByte(Image img)
{
MemoryStream ms = new MemoryStream();
img.Save(ms, img.RawFormat);
byte[] bytes = ms.ToArray();
ms.Close();
return bytes;
}
public static string UrlEncode(string str)
{
StringBuilder sb = new StringBuilder();
byte[] byStr = System.Text.Encoding.UTF8.GetBytes(str); //默认是System.Text.Encoding.Default.GetBytes(str)
for (int i = 0; i < byStr.Length; i++)
{
sb.Append(@"%" + Convert.ToString(byStr[i], 16));
}
return (sb.ToString());
}
4.HttpPost方法(也可以使用其他post方法)
/// <summary>
/// HTTP POST方式请求数据
/// </summary>
/// <param name="url">URL.</param>
/// <param name="param">POST的数据</param>
/// <returns></returns>
public static string HttpPost(string url, string param)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Accept = "*/*";
request.Timeout = 15000;
request.AllowAutoRedirect = false;
StreamWriter requestStream = null;
WebResponse response = null;
string responseStr = null;
try
{
requestStream = new StreamWriter(request.GetRequestStream());
requestStream.Write(param);
requestStream.Close();
response = request.GetResponse();
if (response != null)
{
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
responseStr = reader.ReadToEnd();
reader.Close();
}
}
catch (Exception)
{
throw;
}
finally
{
request = null;
requestStream = null;
response = null;
}
return responseStr;
}
注意:网络图片我才用的接口地址是:https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic 通用文字识别
本地图片的接口地址:https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic 通用文字识别(高精度版)高精度版本只支持本地图片的识别,不支持网络图片识别
本地图片需要先把图片转成Base64然后再进行urlencode