0%

调用百度云接口实现文字识别功能

HTML部分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="图片文字识别.Test" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8;application/x-www-form-urlencoded" />
<title></title>

</head>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">

function submitXML() {
//获取提交的文件
var fileInput = $('#uploading').get(0).files[0];
/*判断是否选中文件*/
if (fileInput) {
/*选中后进行提交*/
$("#form1").submit();
} else {
/*如果没有选中文件进行提示*/
alert("请选择上传文件!");
}
}
</script>
<body>
<form id="form1" enctype="multipart/form-data" runat="server" >
<div>
<input type="file" name="uploading" id="uploading"/>
<input type="button" name="btn" onclick="submitXML()" value="上传" />
<h2>识别到的数据:</h2>
<hr />
<%foreach (var item in ImageTxT)
{%>
<p><%=item %></p>
<%} %>
</div>
</form>
</body>
</html>

第零步:判断是否提交表单和上传图片保存到服务器文件夹

1)判断是否提交表单

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public partial class Test : System.Web.UI.Page
{
/// <summary>
/// 识别出来的文字
/// </summary>
public List<string> ImageTxT = new List<string>();

protected void Page_Load(object sender, EventArgs e)
{
//判断是否提交表单
if (IsPostBack)
{

//获取到上传的文件
HttpPostedFile file = Request.Files["uploading"];
//显示识别出来的文字
ImageTxT = Json.Json_ImageTXT_1(ImageFile.File(file));

}
}
}

2)上传图片保存到服务器文件夹

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
public static class ImageFile
{
/// <summary>
/// 上传文件后保存在本地
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
public static string File(HttpPostedFile file)
{


if (file.ContentLength > 0)
{
//3、判断一下文件类型

//3.1获取文件的类型
string fileName = Path.GetFileName(file.FileName);
//获取文件扩展名
string fileExt = Path.GetExtension(fileName);
//判断文件扩展名是否为jpg后缀 jjpg/jpeg/png/bmp
if (fileExt == ".jpg" || fileExt == ".jjpg" || fileExt == ".jpeg" || fileExt == ".png" || fileExt == ".bmp")
{
//上传上来的文件 名字 文件流MD5值 GUID(全局唯一的标识)
string newFileName = Guid.NewGuid().ToString();
//上传上来的文件 目录 设计分层的结构 日期
//string dir = "/FileUP/LoadImage/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/";
string dir = "LoadImage/";
////如果不存在对应日期的文件夹 创建
if (!Directory.Exists(HttpContext.Current.Request.MapPath(dir)))
{
//创建文件夹
Directory.CreateDirectory(HttpContext.Current.Request.MapPath(dir));
}
//string FullDir = dir + newFileName + fileExt;
string FullDir = dir + newFileName + fileExt;
file.SaveAs(HttpContext.Current.Request.MapPath(FullDir));
//保存文件
return FullDir;

}
else
{
HttpContext.Current.Response.Write("<script>alert('暂不支持该文件类型!');</script>");
return "";
}
}
else
{
HttpContext.Current.Response.Write("<script>alert('请选择图片文件!');</script>");
return "";
}
}
}

第一步:获取TOKEN的json串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/// <summary>
/// 获取TOKEN的json串
/// </summary>
public static class AccessToken

{
// 调用getAccessToken()获取的 access_token建议根据expires_in 时间 设置缓存
// 返回token示例

// 百度云中开通对应服务应用的 API Key 建议开通应用的时候多选服务
private static String clientId = "lmpV2HQCskbvhdsP026bCYx3";
// 百度云中开通对应服务应用的 Secret Key
private static String clientSecret = "hXiv86Q7hM8sIV4u1oT2bGsEB4OrSYto";
/// <summary>
///获取TOKEN的json串的方法
/// </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;
Console.WriteLine(result);
return result;
}
}

第二步:解析TOKEN的json串

1
2
3
4
5
6
7
8
9
10
11
12
/// <summary>
/// token的json串解析
/// </summary>
/// <returns></returns>
public static string Json_token()
{
//获取到解析好的json串通过实体类使用数据
Jsontoken json = JsonConvert.DeserializeObject<Jsontoken>(AccessToken.getAccessToken());

//返回获取到的 access_token
return json.access_token;
}

第三步:把解析好的TOKEN的json串传入文字识别中充当必要参数,获取识别后的json串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/// <summary>
/// 图片文字识别
/// </summary>
public class GeneralBasic
{
/// <summary>
/// 进行文字识别
/// </summary>
/// <returns></returns>
public static string generalBasic(string imageName)
{
string token = Json.Json_token();
//通用文字识别
//string host = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=" + token;
//通用文字识别 含位置信息版
//string host = "https://aip.baidubce.com/rest/2.0/ocr/v1/general?access_token=" + token;
//通用文字识别 高精度版
//string host = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?access_token=" + token;
//通用文字识别 高精度含位置信息版
string host = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate?access_token=" + token;
Encoding encoding = Encoding.Default;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host);
request.Method = "post";
request.KeepAlive = true;
// 图片的base64编码
string base64 = getFileBase64(System.AppDomain.CurrentDomain.BaseDirectory + imageName);
String str = "image=" + HttpUtility.UrlEncode(base64);
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.UTF8);
string result = reader.ReadToEnd();

return result;
}
/// <summary>
/// 获取图片文件
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
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;
}




}

第四步:解析识别后的json串

1.高低精度 不包括文字位置版

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//进行拼接解析好的数据,返回输出
public static List<string> Json_ImageTXT(string imageName)
{
JsonImageTXT json = JsonConvert.DeserializeObject<JsonImageTXT>(GeneralBasic.generalBasic(imageName));

//输出需要的数据
//StringBuilder ImageTXT_Str = null;
List<string> ImageTXT_Str = new List<string>();
for (int i = 0; i < json.words_result.Count; i++)
{
ImageTXT_Str.Add( json.words_result[i].words);
}
return ImageTXT_Str;
}
/// <summary>
/// 不包含位置信息
/// </summary>
public class JsonImageTXT
{
/// <summary>
///唯一的log id,用于问题定位
/// </summary>
public string log_id { get; set; }
/// <summary>
/// 识别结果数,表示words_result的元素个数
/// </summary>
public string words_result_num { get; set; }
/// <summary>
/// 识别结果数组
/// </summary>
public List<WordsResult> words_result { get; set; }
}
public class WordsResult
{
/// <summary>
/// 识别结果字符串
/// </summary>
public string words { get; set; }
}

2.高低精度 含文字位置版

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//进行拼接解析好的数据,返回输出(高低精度 含文字位置版)
public static List<string> Json_ImageTXT_1(string imageName)
{
JsonImageTXT_1 json = JsonConvert.DeserializeObject<JsonImageTXT_1>(GeneralBasic.generalBasic(imageName));

List<string> ImageTXT_Str_1 = new List<string>();
for (int i = 0; i < json.words_result.Count; i++)
{
ImageTXT_Str_1.Add(json.words_result[i].words+
"\r 高度:" +
json.words_result[i].location.height +
"\r 宽度:" +
json.words_result[i].location.width +
"\r 左间距:" +
json.words_result[i].location.left +
"\r 上间距:" +
json.words_result[i].location.top);
}
return ImageTXT_Str_1;
}
/// <summary>
/// 含位置信息版
/// </summary>
public class JsonImageTXT_1
{
/// <summary>
/// 唯一的log id,用于问题定位
/// </summary>
public string log_id { get; set; }
/// <summary>
/// 识别结果数,表示words_result的元素个数
/// </summary>
public string words_result_num { get; set; }
/// <summary>
/// 识别结果数组
/// </summary>
public List<_Words_result> words_result { get; set; }
}
public class _Words_result
{
/// <summary>
/// 文字位置
/// </summary>
public _location location { get; set; }
/// <summary>
/// 识别结果字符串
/// </summary>
public string words { get; set; }
}
public class _location
{
/// <summary>
/// 宽度
/// </summary>
public string width { get; set; }
/// <summary>
/// 上间距
/// </summary>
public string top { get; set; }
/// <summary>
/// 左间距
/// </summary>
public string left { get; set; }
/// <summary>
/// 高度
/// </summary>
public string height { get; set; }
}