一、概述
本人是用C#编程,调用百度API接口实现手写体识别的,参考了百度的产品文档。文档地址:https://cloud.baidu.com/doc/OCR/index.html
二、代码及解说
本人的源代码大部分是来自百度的产品文档,但其中也遇到了一些麻烦。比如文字识别的编码问题,百度的代码给出的编码是Default,但在我的机器上这样做会显示乱码。经过查找资料,我把编码改成UTF8,乱码的问题才得到解决。
作者的所有源代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;
using System.Drawing;
using System.Web;
using System.Net;
namespace myHandwrite
{
public static class FileUtils
{
///
/// 转base64编码
///
///
///
public static String getFileBase64(String fileName)
{
FileStream filestream = new FileStream(fileName, FileMode.Open);
byte[] arr = new byte[filestream.Length];
filestream.Read(arr, 0, (int)filestream.Length);
string baser64 = Convert.ToBase64String(arr);
filestream.Close();
return baser64;
}
}
class Program
{
// 调用getAccessToken()获取的 access_token建议根据expires_in 时间 设置缓存
// 返回token示例
public static String TOKEN = "24.adda70c11b9786206253ddb70affdc46.2592000.1493524354.282335-1234567";
// 百度云中开通对应服务应用的 API Key 建议开通应用的时候多选服务
private static String clientId = "这里改成你的API Key";
// 百度云中开通对应服务应用的 Secret Key
private static String clientSecret = "这里改成你的Secret Key";
///
/// 获取token的函数
///
///
public static String getAccessToken()
{
String authHost = "https://aip.baidubce.com/oauth/2.0/token";
HttpClient client = new HttpClient();
List> paraList = new List>();
paraList.Add(new KeyValuePair("grant_type", "client_credentials"));
paraList.Add(new KeyValuePair("client_id", clientId));
paraList.Add(new KeyValuePair("client_secret", clientSecret));
HttpResponseMessage response = client.PostAsync(authHost, new FormUrlEncodedContent(paraList)).Result;
String result = response.Content.ReadAsStringAsync().Result;
//Console.WriteLine(result);
//自己加的代码
JObject jo = (JObject)JsonConvert.DeserializeObject(result.ToString());
string myToken = jo["access_token"].ToString();
Console.WriteLine("获得的Token是:" + myToken);
return myToken;
}
///
/// 手写体文字识别
///
///
///
///
public static string myHandwriting(string token,string filename)
{
//string token = "#####调用鉴权接口获取的token#####";
// 图片的base64编码
string strbaser64 = FileUtils.getFileBase64(filename);
string host = "https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting?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 = "recognize_granularity=big&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();
//显示结果是乱码,尝试改变编码,经过测试需要改成UTF8编码
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string result = reader.ReadToEnd();
Console.WriteLine("手写文字识别:");
//Console.WriteLine(result);
//解析Json的代码
JObject jo = (JObject)JsonConvert.DeserializeObject(result.ToString());
int num = (int)jo["words_result_num"];
string[] words = new string[num];
for (int i = 0; i < num; i++)
words[i] = jo["words_result"][i]["words"].ToString();
//返回值
string txtOCR = null;
for (int i = 0; i < num; i++)
txtOCR += words[i] + "\n";
//显示结果
Console.WriteLine(txtOCR);
return txtOCR;
}
static void Main(string[] args)
{
//这里要改成你的图片路径
string filename = @"F:\手写体5.jpg";
string token = getAccessToken();
myHandwriting(token,filename);
Console.Read();
}
}
}
注意,上面的代码中需要各位改成自己的Akey和Skey,另外要改一下图片路径。如果返回的是乱码,还需要改一下编码。
识别的结果如下:
程序用的图片文件如下:
请登录后评论
TOP
切换版块
这里加上了参数,下面五行没看懂
超赞,get到了