C# 通过阿里云 API 实现企业营业执照OCR识别

ops/2024/10/19 11:56:51/

 

目录

应用场景

关于阿里云企业营业执照OCR识别

开发运行环境

类设计

类属性

类方法

实现代码

创建 Company 类

调用举例 

小结


应用场景

企业营业执照犹如个人的身份证一样,是​​​​​​​工商管理机关发给企业、个体经营者的准许从事某项生产活动的凭证。在企业会员后台注册系统中,验证电子营业执照是一项常用功能,用户上传电子营业执照图片,再通过云API服务的方式进行验证及提取相关的所有信息:主要包括工商信息(如公司名称、法人姓名、经营范围等),位置信息(如二维码位置、印章位置、国徽位置等)。

自动化提取的企业工商可以提高录入效率和准确率,另外位置信息可以帮助我们截取图象做更多的业务处理。

本文将以阿里云提供的 API 服务,实现通过对上传的企业营业执照电子图片进行OCR的识别功能。

关于阿里云企业营业执照OCR识别

官方介绍其每天更新全国企业、个体工商户的数据,为营业执照的OCR识别提供基础服务。

更多信息内容请参照:企业工商数据查询、公司营业执照验证、企业信息查询验证API接口【按天更新】支持新注册企业、个体工商户【最新版】_电商_数据_CRM-云市场-阿里云

开发前请准备如下操作:

1. 注册阿里云账号。

2. 获取开发者 AppCode,后继开发会用到。

开发运行环境

操作系统: Windows Server 2019 DataCenter

.net版本: .netFramework4.0 或以上

开发工具:VS2019  C#

类设计

类 Company (企业类) 设计见下表:

类属性

序号属性名类型说明
1ErrorMessagestring发生任何异常返回的错误信息
2ResultJsonstring请求返回结果Json完整数据
3anglestring图片的角度(顺时针旋转),[0, 90, 180,270]
4reg_numstring注册号,没有识别出来时返回"FailInRecognition"
5namestring公司名称,没有识别出来时返回"FailInRecognition"
6typestring公司类型,没有识别出来时返回"FailInRecognition"
7personstring公司法人,没有识别出来时返回"FailInRecognition"
8establish_datestring公司注册日期(例:证件上为"2014年04月16日",算法返回"20140416")
9valid_periodstring公司营业期限终止日期(例:证件上为"2014年04月16日至2034年04月15日",算法返回"20340415"),当前算法将日期格式统一为输出为"年月日"(如"20391130"),并将"长期"表示为"29991231",若证件上没有营业期限,则默认其为"长期",返回"29991231"
10addressstring公司地址,没有识别出来时返回"FailInRecognition"
11capitalstring注册资本,没有识别出来时返回"FailInRecognition"
12businessstring#经营范围,没有识别出来时返回"FailInRecognition"
13emblemstring#国徽位置[top,left,height,width],没有识别出来时返回"FailInDetection"
14titlestring标题位置[top,left,height,width],没有识别出来时返回"FailInDetection"
15stampstring印章位置[top,left,height,width],没有识别出来时返回"FailInDetection"
16qrcodestring二维码位置[top,left,height,width],没有识别出来时返回"FailInDetection"
17is_graystring是否是复印件
18successstring识别成功与否 true/false

类方法

ocr_business_license 方法无返回类型,调用均返回对应的类属性数据,参数见如下表格:

序号参数名类型说明
1UrlorBase64string传递完整的图片 Url 或者图片的Base64编码

本方法返回 string 类型的对应属性值(如果成功的话)。

实现代码

创建 Company 类

public class Company
{public string ResultJson="";public string ErrorMessage = "";public string angle = "";// : float, #输入图片的角度(顺时针旋转),[0, 90, 180,270]public string reg_num = "";// : string, #注册号,没有识别出来时返回"FailInRecognition"public string name = "";// : string, #公司名称,没有识别出来时返回"FailInRecognition"public string type = "";// : string, #公司类型,没有识别出来时返回"FailInRecognition"public string person="";// : string, #公司法人,没有识别出来时返回"FailInRecognition"public string establish_date = "";// string, #公司注册日期(例:证件上为"2014年04月16日",算法返回"20140416")public string valid_period = "";//: string, #公司营业期限终止日期(例:证件上为"2014年04月16日至2034年04月15日",算法返回"20340415")//    #当前算法将日期格式统一为输出为"年月日"(如"20391130"),并将"长期"表示为"29991231",若证件上没有营业期限,则默认其为"长期",返回"29991231"。public string address = "";// : string, #公司地址,没有识别出来时返回"FailInRecognition"public string capital = "";// : string, #注册资本,没有识别出来时返回"FailInRecognition"public string business = "";// string, #经营范围,没有识别出来时返回"FailInRecognition"public string emblem = "";// : string, #国徽位置[top,left,height,width],没有识别出来时返回"FailInDetection"public string title = "";// : string, #标题位置[top,left,height,width],没有识别出来时返回"FailInDetection"public string stamp = "";// : string, #印章位置[top,left,height,width],没有识别出来时返回"FailInDetection"public string qrcode = "";// : string, #二维码位置[top,left,height,width],没有识别出来时返回"FailInDetection"public string is_gray = "";//: false,   #是否是复印件public string success="";// : bool, #识别成功与否 true/falsepublic void ocr_business_license(string UrlorBase64){string host = "https://dm-58.data.aliyun.com";string path = "/rest/160601/ocr/ocr_business_license.json";string method = "POST";String appcode = "您的AppCode";String querys = "";String bodys = "{\"image\":\""+UrlorBase64+"\"}"; String url = host + path;HttpWebRequest httpRequest = null;HttpWebResponse httpResponse = null;if (0 < querys.Length){url = url + "?" + querys;}if (host.Contains("https://")){ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));}else{httpRequest = (HttpWebRequest)WebRequest.Create(url);}httpRequest.Method = method;httpRequest.Headers.Add("Authorization", "APPCODE " + appcode);if (0 < bodys.Length){byte[] data = Encoding.UTF8.GetBytes(bodys);using (Stream stream = httpRequest.GetRequestStream()){stream.Write(data, 0, data.Length);}}try{httpResponse = (HttpWebResponse)httpRequest.GetResponse();}catch (WebException ex){ErrorMessage = ex.Message;httpResponse = (HttpWebResponse)ex.Response;return;}Stream st = httpResponse.GetResponseStream();StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));ResultJson = (reader.ReadToEnd());if (ResultJson.IndexOf("\"success\":true") == -1&& ResultJson.IndexOf("\"success\":false")==-1){return;}Newtonsoft.Json.Linq.JObject jsonObj = Newtonsoft.Json.Linq.JObject.Parse(ResultJson);angle = jsonObj["angle"].ToString();reg_num = jsonObj["reg_num"].ToString();name = jsonObj["name"].ToString();type = jsonObj["type"].ToString();person = jsonObj["person"].ToString();establish_date = jsonObj["establish_date"].ToString();valid_period = jsonObj["valid_period"].ToString();capital = jsonObj["capital"].ToString();business = jsonObj["business"].ToString();emblem = jsonObj["emblem"].ToString();title = jsonObj["title"].ToString();stamp = jsonObj["stamp"].ToString();qrcode = jsonObj["qrcode"].ToString();is_gray = jsonObj["is_gray"].ToString();success = jsonObj["success"].ToString();}
}

调用举例 

调用判断 success 字段是否为true,为true则表示成功,继续输出具体值。

示例代码如下:

string result_base64 = ImgToBase64String("d:\\1.jpg", true);
Company cp = new Company();
cp.ocr_business_license(result_base64);
if (cp.success == "true")
{Response.Write("图片的角度:" + cp.angle + "<br>");Response.Write("注册号:" + cp.reg_num + "<br>");Response.Write("公司名称:" + cp.name + "<br>");Response.Write("公司类型:" + cp.type + "<br>");Response.Write("公司法人:" + cp.person + "<br>");Response.Write("公司注册日期:" + cp.establish_date + "<br>");Response.Write("公司营业期限终止日期:" + cp.valid_period + "<br>");Response.Write("公司地址:" + cp.bussiness + "<br>");Response.Write("注册资本:" + cp.capital + "<br>");Response.Write("经营范围:" + cp.bussiness + "<br>");Response.Write("国徽位置:" + cp.emblem + "<br>");Response.Write("标题位置:" + cp.title + "<br>");Response.Write("印章位置:" + cp.stamp + "<br>");Response.Write("二维码位置:" + cp.qrcode + "<br>");Response.Write("是否是复印件:" + cp.bussiness + "<br>");
}
else
{Response.Write("错误信息:" + cp.ErrorMessage + "<br>");Response.Write("JSON返回信息:" + cp.ResultJson + "<br>");
}

小结

调用云接口服务需要费用,我们需要根据实际应用进行成本考虑,官方说明如果查询失败则不扣除费用,具体内容可参考本文第二小节关于阿里云关于阿里云企业营业执照OCR识别API中的链接。

 如何获取图像 base64 数据的方法请参照我的文章:《C# 自动填充文字内容到指定图片》

感谢您的阅读,希望本文能够对您有所帮助。


http://www.ppmy.cn/ops/15733.html

相关文章

三维点云处理-KDTree和Octree

点云数据主要是表征目标表面的海量点集合&#xff0c;并不具备传统实体网格数据的集合拓扑信息。因此&#xff0c;如何建立离散点间的拓扑关系&#xff0c;实现基于邻域关系的快速查找也是点云数据处理中比较核心的问题。对于一维数据来说&#xff0c;典型的树形存储结构如Bina…

探索常见经典目标检测算法:从YOLO到Faster R-CNN

目标检测是计算机视觉领域中的重要任务之一&#xff0c;它旨在从图像或视频中检测出目标的位置和类别。随着深度学习技术的发展&#xff0c;许多经典的目标检测算法相继出现&#xff0c;其中包括YOLO&#xff08;You Only Look Once&#xff09;、Faster R-CNN&#xff08;Regi…

教授 Avi Wigderson荣获2023年图灵奖

2023年图灵奖&#xff0c;最近刚刚颁给普林斯顿数学教授 Avi Wigderson&#xff01;作为理论计算机科学领域的领军人物&#xff0c;他对于理解计算中的随机性和伪随机性的作用&#xff0c;作出了开创性贡献。 Avi Wigderson因其在计算复杂性理论、随机性与算法等领域的开创性贡…

python学习笔记(集合)

知识点思维导图 # 直接使用{}进行创建 s{10,20,30,40} print(s)# 使用内置函数set()创建 sset() print(s)# 创建一个空的{}默认是字典类型 s{} print(s,type(s))sset(helloworld) print(s) sset([10,20,30]) print(s) s1set(range(1,10)) print(s1)print(max:,max(s1)) print(m…

C++11单例模式

&#x1f436;博主主页&#xff1a;ᰔᩚ. 一怀明月ꦿ ❤️‍&#x1f525;专栏系列&#xff1a;线性代数&#xff0c;C初学者入门训练&#xff0c;题解C&#xff0c;C的使用文章&#xff0c;「初学」C&#xff0c;linux &#x1f525;座右铭&#xff1a;“不要等到什么都没有了…

前端获取文件后缀名

function getFileExtension(filename) {var parts filename.split(.);if (parts.length > 1) {return parts.pop();} else {return ;} }// 使用例子 var filename "example.png"; var extension getFileExtension(filename); console.log(extension); // 输出:…

Swift中的单例

在Swift中实现单例模式可以通过使用静态常量或静态变量来实现。下面是一个示例&#xff1a; class Singleton {static let sharedInstance Singleton()private init() {// 这里是初始化代码}// 这里是其他实例方法和属性 }在上面的示例中&#xff0c;通过使用static let来创建…

关于SM2软件加密与硬件加密的问题

SM2国密算法在中国的密码学领域中使用得非常广泛&#xff0c;它是一种基于椭圆曲线公钥密码的算法。日常开发工作中&#xff0c;涉及到SM2算法的加密和解密时&#xff0c;如果一方使用软件实现&#xff0c;而另一方使用硬件实现&#xff0c;可能会遇到一些联调过程中的问题。在…