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