开通腾讯云
进入腾讯云 文字识别控制台,勾选同意并点击立即开通。
准备开发环境
- 安装运行环境,下载 .NET 8 SDK 并安装;
- 安装开发工具,下载 VS2022 并安装。
创建Web项目
选择ASP.NET Core Web应用(MVC),填写项目名称。
编辑根目录下的 appsettings.json 配置文件。填写腾讯云获取到的SecretId和SecretKey。
添加控制器,并添加调用腾讯云API识别接口代码。
/// <summary>
/// 卡证文字识别
/// </summary>
/// <returns></returns>
[HttpPost]
public IActionResult BankCard()
{
var basePath = _webHostEnvironment.WebRootPath;
Credential cred = new Credential
{
SecretId = Configuration["TencentCloud:SecretId"],
SecretKey = Configuration["TencentCloud:SecretKey"]
};
ClientProfile clientProfile = new ClientProfile();
HttpProfile httpProfile = new HttpProfile();
httpProfile.Endpoint = ("ocr.tencentcloudapi.com");
clientProfile.HttpProfile = httpProfile;
OcrClient client = new OcrClient(cred, "ap-beijing", clientProfile);
GeneralHandwritingOCRRequest req = new GeneralHandwritingOCRRequest();
byte[] imageBytes = System.IO.File.ReadAllBytes($"{basePath}/image/02fa657cf3b8db45-2046057.jpg");
string base64String = Convert.ToBase64String(imageBytes);
req.ImageBase64 = base64String;
GeneralHandwritingOCRResponse resp = client.GeneralHandwritingOCRSync(req);
return View("Index", resp);
}
卡证文字识别:支持身份证识别、银行卡识别、驾驶证、行驶证、营业执照识别、护照识别、港澳台通行证识别、港澳台来往内地通行证识别、港澳台居住证识别、中国香港身份证、名片识别、机动车登记证书识别等常用卡证类文字识别。
/// <summary>
/// 通用文字识别
/// </summary>
/// <returns></returns>
[HttpPost]
public IActionResult GeneralRecogn()
{
var basePath = _webHostEnvironment.WebRootPath;
Credential cred = new Credential
{
SecretId = Configuration["TencentCloud:SecretId"],
SecretKey = Configuration["TencentCloud:SecretKey"]
};
ClientProfile clientProfile = new ClientProfile();
HttpProfile httpProfile = new HttpProfile();
httpProfile.Endpoint = ("ocr.tencentcloudapi.com");
clientProfile.HttpProfile = httpProfile;
OcrClient client = new OcrClient(cred, "ap-beijing", clientProfile);
GeneralHandwritingOCRRequest req = new GeneralHandwritingOCRRequest();
byte[] imageBytes = System.IO.File.ReadAllBytes($"{basePath}/image/20240414151458.png");
string base64String = Convert.ToBase64String(imageBytes);
req.ImageBase64 = base64String;
GeneralHandwritingOCRResponse resp = client.GeneralHandwritingOCRSync(req);
return View("General", resp);
}
通用文字识别:支持通用印刷体识别、通用手写体识别、英文识别、表格识别等多种服务,支持将图片上的文字内容,智能识别为可编辑的文本,可应用于随手拍扫描、纸质文档电子化、电商广告审核等多种场景。
/// <summary>
/// 特定场景识别
/// </summary>
/// <returns></returns>
[HttpPost]
public IActionResult LicensePlateOCRRecogn()
{
var basePath = _webHostEnvironment.WebRootPath;
Credential cred = new Credential
{
SecretId = Configuration["TencentCloud:SecretId"],
SecretKey = Configuration["TencentCloud:SecretKey"]
};
ClientProfile clientProfile = new ClientProfile();
HttpProfile httpProfile = new HttpProfile();
httpProfile.Endpoint = ("ocr.tencentcloudapi.com");
clientProfile.HttpProfile = httpProfile;
OcrClient client = new OcrClient(cred, "ap-beijing", clientProfile);
GeneralHandwritingOCRRequest req = new GeneralHandwritingOCRRequest();
byte[] imageBytes = System.IO.File.ReadAllBytes($"{basePath}/image/2796494.jpg");
string base64String = Convert.ToBase64String(imageBytes);
req.ImageBase64 = base64String;
GeneralHandwritingOCRResponse resp = client.GeneralHandwritingOCRSync(req);
return View("LicensePlateOCR", resp);
}
特定场景识别:支持车牌识别、车辆 VIN 码识别、印章识别等特定场景识别,支持将图片上的文字内容,智能识别为结构化的文本。
更多文字识别使用实例,及API接口详细使用说明,请参考腾讯云文字识别官方接口文档。
下载示例源代码
本示例源码下载地址:立即下载
https://download.csdn.net/download/u010420943/89134641