简介:本文详细讲解如何使用C#语言调用百度AI开放平台的文字识别API,涵盖环境准备、API调用流程、代码实现及优化建议,适合C#开发者快速集成OCR功能。
在数字化转型浪潮中,文字识别(OCR)技术已成为企业自动化流程的核心组件。百度AI开放平台提供的通用文字识别API,支持高精度识别图片中的文字内容,尤其适用于金融票据、合同文档、物流面单等场景。通过C#语言调用该API,开发者可快速构建具备OCR能力的Windows桌面应用或Web服务。
using System;using System.Net.Http;using System.Security.Cryptography;using System.Text;using System.Threading.Tasks;public class BaiduAIClient{private readonly string _apiKey;private readonly string _secretKey;public BaiduAIClient(string apiKey, string secretKey){_apiKey = apiKey;_secretKey = secretKey;}public async Task<string> GetAccessTokenAsync(){using (var client = new HttpClient()){var url = $"https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={_apiKey}&client_secret={_secretKey}";var response = await client.GetAsync(url);var content = await response.Content.ReadAsStringAsync();// 解析JSON获取access_tokendynamic json = Newtonsoft.Json.JsonConvert.DeserializeObject(content);return json.access_token;}}}
关键点:
public async Task<string> RecognizeTextAsync(string accessToken, string imagePath){using (var client = new HttpClient()){// 读取图片为Base64var imageBytes = System.IO.File.ReadAllBytes(imagePath);var base64 = Convert.ToBase64String(imageBytes);// 构造请求参数var requestData = new{image = base64,recognize_granularity = "big", // 识别粒度:big/smalllanguage_type = "CHN_ENG" // 语言类型};var content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(requestData),Encoding.UTF8,"application/json");var url = $"https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token={accessToken}";var response = await client.PostAsync(url, content);if (!response.IsSuccessStatusCode){throw new Exception($"API调用失败: {response.StatusCode}");}var result = await response.Content.ReadAsStringAsync();return result;}}
参数优化建议:
recognize_granularity设为”small”可获取更精确的字符位置table_recognition接口
public class OCRResult{public int LogId { get; set; }public List<WordInfo> WordsResult { get; set; }public class WordInfo{public string Words { get; set; }public Location Location { get; set; }}public class Location{public int Left { get; set; }public int Top { get; set; }public int Width { get; set; }public int Height { get; set; }}}// 解析示例public OCRResult ParseOCRResult(string json){dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(json);var result = new OCRResult{LogId = (int)jsonObj.log_id};result.WordsResult = new List<OCRResult.WordInfo>();foreach (var item in jsonObj.words_result){result.WordsResult.Add(new OCRResult.WordInfo{Words = (string)item.words,Location = new OCRResult.Location{Left = (int)item.location[0],Top = (int)item.location[1],Width = (int)item.location[2],Height = (int)item.location[3]}});}return result;}
public async Task<List<OCRResult>> BatchRecognizeAsync(string accessToken, List<string> imagePaths){var tasks = new List<Task<string>>();foreach (var path in imagePaths){tasks.Add(RecognizeTextAsync(accessToken, path));}var results = await Task.WhenAll(tasks);return results.Select(ParseOCRResult).ToList();}
性能建议:
public async Task<string> SafeRecognizeAsync(string accessToken, string imagePath, int maxRetries = 3){int retryCount = 0;while (retryCount < maxRetries){try{return await RecognizeTextAsync(accessToken, imagePath);}catch (Exception ex) when (retryCount < maxRetries - 1){retryCount++;await Task.Delay(1000 * retryCount); // 指数退避}}throw new Exception($"达到最大重试次数后仍失败: {maxRetries}");}
{"BaiduAI": {"ApiKey": "your_api_key","SecretKey": "your_secret_key","MaxConcurrent": 5}}
使用Microsoft.Extensions.Configuration加载配置
BaiduOCRDemo/├── BaiduAIClient.cs // 核心API客户端├── Models/ // 数据模型│ └── OCRResult.cs├── Services/ // 业务逻辑│ └── OCRService.cs├── Config/ // 配置管理│ └── AppSettings.json└── Program.cs // 入口
Content-Type: application/x-www-form-urlencodedTimeout = 10000(10秒)idcard接口bankcard接口business_license接口accurate_basic接口通过本教程,开发者已掌握:
未来可探索方向:
本教程代码示例已通过.NET 6.0验证,建议开发者在实际项目中添加更完善的日志记录和异常处理机制。百度AI文字识别API的最新文档请参考官方开发文档,确保使用最新版本接口。