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

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

应用场景

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

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

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

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

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

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

开发前请准备如下操作:

1. 注册阿里云账号。

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

开发运行环境

操作系统: Windows Server 2019 DataCenter

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

开发工具:VS2019  C#

类设计

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

类属性

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

类方法

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

序号 参数名 类型 说明
1 UrlorBase64 string 传递完整的图片 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/false
 
            public 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# 自动填充文字内容到指定图片》

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

相关文章
|
2天前
|
监控 安全 数据挖掘
Email 接口API有哪些?具体分析一下阿里云和AOK的优点
本文介绍了常见的Email接口API,如阿里云邮件推送、AOKSend、SendGrid、Mailgun和Amazon SES。阿里云API以其高稳定性和数据分析功能脱颖而出,支持批量发送和多语言;而AOKSend API以易于集成、高安全性和优秀客户支持为亮点。企业在选择时应考虑自身需求和预算,以优化邮件营销效果。
|
2天前
|
监控 安全 搜索推荐
Email发送API的方法?AOKSend和阿里云哪个效果更好?
Email发送API在企业与客户沟通中扮演关键角色,允许自动化和个性化邮件发送。本文比较了AOKSend和阿里云的API:AOKSend以其高送达率、快速发送和详细分析报告脱颖而出,适合中小企业;阿里云则凭借稳定性、大规模发送能力和综合云服务吸引大企业。选择合适API能优化邮件营销效果。
|
3天前
|
文字识别 JavaScript Java
印刷文字识别产品使用合集之阿里云文字识别OCR demo主要有哪些
印刷文字识别(Optical Character Recognition, OCR)技术能够将图片、扫描文档或 PDF 中的印刷文字转化为可编辑和可搜索的数据。这项技术广泛应用于多个领域,以提高工作效率、促进信息数字化。以下是一些印刷文字识别产品使用的典型场景合集。
138 0
|
4天前
|
人工智能 API
阿里云微服务引擎及 API 网关 2024 年 4 月产品动态
阿里云微服务引擎及 API 网关 2024 年 4 月产品动态。
|
4天前
|
运维 Cloud Native 应用服务中间件
阿里云微服务引擎 MSE 及 API 网关 2024 年 04 月产品动态
阿里云微服务引擎 MSE 面向业界主流开源微服务项目, 提供注册配置中心和分布式协调(原生支持 Nacos/ZooKeeper/Eureka )、云原生网关(原生支持Higress/Nginx/Envoy,遵循Ingress标准)、微服务治理(原生支持 Spring Cloud/Dubbo/Sentinel,遵循 OpenSergo 服务治理规范)能力。API 网关 (API Gateway),提供 APl 托管服务,覆盖设计、开发、测试、发布、售卖、运维监测、安全管控、下线等 API 生命周期阶段。帮助您快速构建以 API 为核心的系统架构.满足新技术引入、系统集成、业务中台等诸多场景需要。
|
4天前
|
存储 数据可视化 数据建模
阿里云大佬叮嘱我务必要科普这个 Elasticsearch API
阿里云大佬叮嘱我务必要科普这个 Elasticsearch API
15 0
|
4天前
|
文字识别 容器
文字识别OCR常见问题之本地部署如何解决
文字识别OCR(Optical Character Recognition)技术能够将图片或者扫描件中的文字转换为电子文本。以下是阿里云OCR技术使用中的一些常见问题以及相应的解答。
66 3
|
4天前
|
文字识别 前端开发 API
文字识别OCR常见问题之处理产品图片识别如何解决
文字识别OCR(Optical Character Recognition)技术能够将图片或者扫描件中的文字转换为电子文本。以下是阿里云OCR技术使用中的一些常见问题以及相应的解答。
28 3
|
4天前
|
文字识别
文字识别OCR常见问题之拦截扫描件的识别如何解决
文字识别OCR(Optical Character Recognition)技术能够将图片或者扫描件中的文字转换为电子文本。以下是阿里云OCR技术使用中的一些常见问题以及相应的解答。
23 2
|
4天前
|
机器学习/深度学习 文字识别 监控
印刷文字识别产品使用合集之在OCR中,识别增值税专用发票的时候为什么会把不含税金额做取整处理
印刷文字识别(Optical Character Recognition, OCR)技术能够将图片、扫描文档或 PDF 中的印刷文字转化为可编辑和可搜索的数据。这项技术广泛应用于多个领域,以提高工作效率、促进信息数字化。以下是一些印刷文字识别产品使用的典型场景合集。
16 2

热门文章

最新文章

http://www.vxiaotou.com