ASP.NET Core 8 对接腾讯云文字识别,将图片上的文字内容智能识别成为可编辑的文本

ASP.NET Core 8 对接腾讯云文字识别,将图片上的文字内容智能识别成为可编辑的文本

开通腾讯云

进入腾讯云 文字识别控制台,勾选同意并点击立即开通

在这里插入图片描述

准备开发环境

  1. 安装运行环境,下载 .NET 8 SDK 并安装;
  2. 安装开发工具,下载 VS2022 并安装。

创建Web项目

选择ASP.NET Core Web应用(MVC),填写项目名称。

在这里插入图片描述
编辑根目录下的 appsettings.json 配置文件。填写腾讯云获取到的SecretIdSecretKey

在这里插入图片描述
添加控制器,并添加调用腾讯云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

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

隔壁程序员有话说

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值