1.注冊百度Api
2.引用 c# SDK
安裝文字識別 C# SDK
C# SDK 現已開源! https://github.com/Baidu-AIP/dotnet-sdk
** 支持平台:.Net Framework 3.5 4.0 4.5, .Net Core 2.0 **
方法一:使用Nuget管理依賴 (推薦)
在NuGet中搜索 Baidu.AI,安裝最新版即可。
packet地址 https://www.nuget.org/packages/Baidu.AI/
方法二:下載安裝
文字識別 C# SDK目錄結構
Baidu.Aip
├── net35
│ ├── AipSdk.dll // 百度AI服務 windows 動態庫 │ ├── AipSdk.xml // 注釋文件 │ └── Newtonsoft.Json.dll // 第三方依賴 ├── net40 ├── net45 └── netstandard2.0 ├── AipSdk.deps.json └── AipSdk.dll
在官方網站下載C# SDK壓縮工具包;解壓后,將 AipSdk.dll 和 Newtonsoft.Json.dll 中添加為引用。
3.demo源碼:
using Newtonsoft.Json.Linq;
using System.IO;
namespace HelperBll
{
public class ImgApiBll
{
//基礎參數:從配置文件取過來
private readonly string _api_key;//百度api鍵值
private readonly string _secret_key;//百度api秘鑰
private readonly int _timeOut;//超時設置
public ImgApiBll(string api_key, string secret_key, int timeOut )
{
_api_key = api_key;
_secret_key = secret_key;
_timeOut = timeOut;
}
/// <summary>
/// 文字-json格式
/// </summary>
/// <param name="imgPath">圖片地址</param>
/// <returns></returns>
public string ReadImgTxt(string imgPath)
{
var client = new Baidu.Aip.Ocr.Ocr(_api_key, _secret_key)
{
Timeout = _timeOut //超時時間
};
//本地圖片
byte[] imgStream = File.ReadAllBytes(imgPath); //圖片二進制
JObject jsonResult = client.GeneralBasic(imgStream);
//網絡圖片
//JObject jsonResult = client.GeneralBasicUrl(imgUrl);
//JObject jsonResult = client.Accurate(imgStream); //本地圖片:相對於通用文字識別該產品精度更高,但是識別耗時會稍長。
//JObject jsonResult = client.General(imgStream); //本地圖片:通用文字識別(含位置信息版)
//JObject jsonResult = client.GeneralUrl(imgUrl); //網絡圖片:通用文字識別(含位置信息版)
//JObject jsonResult = client.GeneralEnhanced(imgStream); //本地圖片:調用通用文字識別(含生僻字版)
//JObject jsonResult = client.GeneralEnhancedUrl(imgUrl); //網絡圖片:調用通用文字識別(含生僻字版)
//JObject jsonResult = client.WebImage(imgStream); //本地圖片:用戶向服務請求識別一些背景復雜,特殊字體的文字。
//JObject jsonResult = client.WebImageUrl(imgUrl); //網絡圖片:用戶向服務請求識別一些背景復雜,特殊字體的文字。
return jsonResult.ToString();
}
}
}
4. 程序效果:
圖片示例:


讀出文字結果:
{{
"log_id": 2872598628702849350,
"words_result_num": 18,
"words_result": [
{
"words": "接口名稱"
},
{
"words": "接口能力簡要描述"
},
{
"words": "通用文字識別"
},
{
"words": "識別圖片中的文字信息"
},
{
"words": "通用文字識別(高精度版)"
},
{
"words": "更高精度地識別圖片中的文字信息"
},
{
"words": "通用文字識別(含位置信息版)識別圖片中的文字信息(包含文字區域的坐標信息)"
},
{
"words": "通用文字識別(高精度含位置更高精度地識別圖片中的文字信息(包含文字區域的坐標信息)"
},
{
"words": "通用文字識別(含生僻字版)識別圖片中的文字信息(包含對常見字和生僻字的識別)"
},
{
"words": "網絡片文字識別"
},
{
"words": "識別一些網絡上背景復雜,特殊字體的文字"
},
{
"words": "身份證識別"
},
{
"words": "識別身份證正反面的文字信息"
},
{
"words": "銀行卡識另"
},
{
"words": "識別銀行卡的卡號並返回發卡行和卡片性質信息"
},
{
"words": "識別機動車駕駛證所有關鍵字段"
},
{
"words": "行駛證識別"
},
{
"words": "識別機動車行駛證所有關鍵字段"
}
]
}}
5.總結:
文字截圖,基本是可以完整讀出來的。
參考:https://www.cnblogs.com/xiongze520/p/11283484.html
