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# 自动填充文字内容到指定图片》

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

相关文章
|
1天前
|
IDE API 开发工具
沉浸式集成阿里云 OpenAPI|Alibaba Cloud API Toolkit for VS Code
Alibaba Cloud API Toolkit for VSCode 是集成了 OpenAPI 开发者门户多项功能的 VSCode 插件,开发者可以通过这个插件方便地查找API文档、进行API调试、插入SDK代码,并配置基础环境设置。我们的目标是缩短开发者在门户和IDE之间的频繁切换,实现API信息和开发流程的无缝结合,让开发者的工作变得更加高效和紧密。
沉浸式集成阿里云 OpenAPI|Alibaba Cloud API Toolkit for VS Code
|
23小时前
|
运维 Cloud Native 应用服务中间件
阿里云微服务引擎 MSE 及 云原生 API 网关 2024 年 09 月产品动态
阿里云微服务引擎 MSE 面向业界主流开源微服务项目, 提供注册配置中心和分布式协调(原生支持 Nacos/ZooKeeper/Eureka )、云原生网关(原生支持Higress/Nginx/Envoy,遵循Ingress标准)、微服务治理(原生支持 Spring Cloud/Dubbo/Sentinel,遵循 OpenSergo 服务治理规范)能力。API 网关 (API Gateway),提供 APl 托管服务,覆盖设计、开发、测试、发布、售卖、运维监测、安全管控、下线等 API 生命周期阶段。帮助您快速构建以 API 为核心的系统架构.满足新技术引入、系统集成、业务中台等诸多场景需要
|
1月前
|
Cloud Native 关系型数据库 Serverless
基于阿里云函数计算(FC)x 云原生 API 网关构建生产级别 LLM Chat 应用方案最佳实践
本文带大家了解一下如何使用阿里云Serverless计算产品函数计算构建生产级别的LLM Chat应用。该最佳实践会指导大家基于开源WebChat组件LobeChat和阿里云函数计算(FC)构建企业生产级别LLM Chat应用。实现同一个WebChat中既可以支持自定义的Agent,也支持基于Ollama部署的开源模型场景。
231 12
|
9天前
|
API
使用`System.Net.WebClient`类发送HTTP请求来调用阿里云短信API
使用`System.Net.WebClient`类发送HTTP请求来调用阿里云短信API
12 0
|
1月前
|
运维 Cloud Native 应用服务中间件
阿里云微服务引擎 MSE 及 云原生 API 网关 2024 年 08 月产品动态
阿里云微服务引擎 MSE 面向业界主流开源微服务项目, 提供注册配置中心和分布式协调(原生支持 Nacos/ZooKeeper/Eureka )、云原生网关(原生支持Higress/Nginx/Envoy,遵循Ingress标准)、微服务治理(原生支持 Spring Cloud/Dubbo/Sentinel,遵循 OpenSergo 服务治理规范)能力。API 网关 (API Gateway),提供 APl 托管服务,覆盖设计、开发、测试、发布、售卖、运维监测、安全管控、下线等 API 生命周期阶段。帮助您快速构建以 API 为核心的系统架构.满足新技术引入、系统集成、业务中台等诸多场景需要
|
2月前
|
机器人 API Python
智能对话机器人(通义版)会话接口API使用Quick Start
本文主要演示了如何使用python脚本快速调用智能对话机器人API接口,在参数获取的部分给出了具体的获取位置截图,这部分容易出错,第一次使用务必仔细参考接入参数获取的位置。
142 1
|
5天前
|
人工智能 自然语言处理 PyTorch
Text2Video Huggingface Pipeline 文生视频接口和文生视频论文API
文生视频是AI领域热点,很多文生视频的大模型都是基于 Huggingface的 diffusers的text to video的pipeline来开发。国内外也有非常多的优秀产品如Runway AI、Pika AI 、可灵King AI、通义千问、智谱的文生视频模型等等。为了方便调用,这篇博客也尝试了使用 PyPI的text2video的python库的Wrapper类进行调用,下面会给大家介绍一下Huggingface Text to Video Pipeline的调用方式以及使用通用的text2video的python库调用方式。
|
5天前
|
JSON JavaScript API
(API接口系列)商品详情数据封装接口json数据格式分析
在成长的路上,我们都是同行者。这篇关于商品详情API接口的文章,希望能帮助到您。期待与您继续分享更多API接口的知识,请记得关注Anzexi58哦!
|
25天前
|
安全 API 开发者
Web 开发新风尚!Python RESTful API 设计与实现,让你的接口更懂开发者心!
在当前的Web开发中,Python因能构建高效简洁的RESTful API而备受青睐,大大提升了开发效率和用户体验。本文将介绍RESTful API的基本原则及其在Python中的实现方法。以Flask为例,演示了如何通过不同的HTTP方法(如GET、POST、PUT、DELETE)来创建、读取、更新和删除用户信息。此示例还包括了基本的路由设置及操作,为开发者提供了清晰的API交互指南。
92 6
|
2天前
|
监控 API 开发工具
深入理解API设计:构建高效的接口
【10月更文挑战第6天】深入理解API设计:构建高效的接口
9 0