腾讯云 文字识别功能 OCR 使用记录
1、注册腾讯云账号
注册地址:https://cloud.tencent.com/document/product/378/17985
2、创建密钥
创建地址:https://console.cloud.tencent.com/cam/capi
云API秘钥->API秘钥管理,点击 新建秘钥 即可,记录下对应的APPID、SecretId、SecretKey,在项目中需要的地方替换掉。
3、使用数据万象创建Bucket
创建地址:https://console.cloud.tencent.com/ci
记住要开通这个,否则会报错
(以下以不动产权证识别举例,其他类别同理)
4、接口文档
文档地址:https://cloud.tencent.com/document/product/866/38300
5、文字识别模拟api
api地址:https://cloud.tencent.com/act/event/ocrdemo
找到适合自己的接口
5、调式
调式地址:https://console.cloud.tencent.com/api/explorer?Product=ocr&Version=2018-11-19&Action=EstateCertOCR&SignVersion=
5.1、代码生成
腾讯云提供了代码,选择自己的语言,然后腾讯云会自动生成代码,我们只需要把代码粘贴就可以了,将
SecretId 和 SecretKey 改写成自己的(第二步时候申请的密钥),这样调用接口就成功了
5.2、线上调式—签名串生成
腾讯云支持线上调式,输入自己的SecretId 和 SecretKey,然后生成签名串
5.3、线上调式—发送请求
完成5.1和5.2之后,点击发送请求,可以在线看接口返回值成功与否
6、参考代码
6.1、安装nug包
6.2、代码
[HttpGet]
public virtual async Task<string> Get()
{
var img = Common.Common.ImgToBase64(@"C:\Users\Administrator\Desktop\EstateCertOCR.jpg");
var result=EstateCertOCR.Ocr(img);
return result;
}
using Dto;
using Newtonsoft.Json;
using System;
using System.Drawing;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using TencentCloud.Common;
using TencentCloud.Common.Profile;
using TencentCloud.Ocr.V20181119;
using TencentCloud.Ocr.V20181119.Models;
namespace Common
{
public class EstateCertOCR
{
public static string Ocr(string args)
{
try
{
Credential cred = new Credential
{
SecretId = "AKID72GI4ZooZnpb8RUIq9C0XnnXXXXXXXXXX",
SecretKey = "Xoqc9PP9esqY0B5j6C7nDPLXXXXXXX"
};
ClientProfile clientProfile = new ClientProfile();
HttpProfile httpProfile = new HttpProfile();
httpProfile.Endpoint = ("ocr.tencentcloudapi.com");
clientProfile.HttpProfile = httpProfile;
OcrClient client = new OcrClient(cred, "ap-beijing", clientProfile);
EstateCertOCRRequest req = new EstateCertOCRRequest();
//使用base64
req.ImageBase64 = args;
// 使用URL
//req.ImageUrl = "https://ocr-demo-1254418846.cos.ap-guangzhou.myqcloud.com/card/EstateCertOCR/EstateCertOCR1.jpg";
EstateCertOCRResponse resp = client.EstateCertOCRSync(req);
//json
var result = AbstractModel.ToJsonString(resp);
//json转为实体
var resultDto = JsonConvert.DeserializeObject<CertOCRDto>(result);
return AbstractModel.ToJsonString(resp);
}
catch (Exception e)
{
return e.ToString();
}
}
}
}
//图片转化成base64字符串
public static string ImgToBase64(string ImageFileName)
{
try
{
Bitmap bmp = new Bitmap(ImageFileName);
MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] arr = new byte[ms.Length];
ms.Position = 0;
ms.Read(arr, 0, (int)ms.Length);
ms.Close();
return Convert.ToBase64String(arr);
}
catch (Exception)
{
return null;
}
}
public class CertOCRDto
{
//权利人
public string Obligee { get; set; }
//共有情况
public string Ownership { get; set; }
//坐落
public string Location { get; set; }
//不动产单元号
public string Unit { get; set; }
//权利类型
public string Type { get; set; }
//权利性质
public string Property { get; set; }
//用途
public string Usage { get; set; }
//面积
public string Area { get; set; }
//使用期限
public string Term { get; set; }
//权利其他状况
public string Other { get; set; }
//图片角度
public double Angle { get; set; }
//不动产证号
public string Number { get; set; }
//guid
public Guid RequestId { get; set; }
}
7、所遇到的问题
图片太大转base64 会报签名错误,图片一开始是900K 后来压缩到300K以下 就好了