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

发布于:2024-04-26 ⋅ 阅读:(31) ⋅ 点赞:(0)

 

目录

应用场景

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

开发运行环境

类设计

类属性

类方法

实现代码

创建 Company 类

调用举例 

小结


应用场景

企业营业执照犹如个人的身份证一样,是​​​​​​​工商管理机关发给企业、个体经营者的准许从事某项生产活动的凭证。在企业会员后台注册系统中,验证电子营业执照是一项常用功能,用户上传电子营业执照图片,再通过云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# 自动填充文字内容到指定图片》

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