sunpengfei
2025-08-22 06d5b35bae85bf0f4b5bd1f837205395305274f1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using Baidu.Aip.Ocr;
using Furion;
using Furion.DistributedIDGenerator;
using Furion.FriendlyException;
using Furion.HttpRemote;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.Extensions.Options;
using NetTopologySuite.Operation.Buffer;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
 
namespace FlexJobApi.Core
{
    /// <summary>
    /// 百度文字识别工具
    /// </summary>
    public static class BaiduOcrUtils
    {
        /// <summary>
        /// 营业执照OCR
        /// </summary>
        /// <param name="url"></param>
        /// <param name="scene"></param>
        /// <param name="isOssUrl"></param>
        public static async Task<BaiduOcrBusinessLicenseResult> OcrBusinessLicense(this string url, string scene = null, bool isOssUrl = true)
        {
            var options = App.GetOptions<BaiduOptions>();
            var ocr = new Ocr(options.Ocr.Key, options.Ocr.Secret);
            ocr.Timeout = 60000;
            JObject value;
            if (isOssUrl)
            {
                var downloadUrl = AliyunOSSUtils.GetUrl(url);
                var buffer = await App.GetRequiredService<IHttpRemoteService>().GetAsByteArrayAsync(downloadUrl);
                value = ocr.BusinessLicense(buffer);
            }
            else
            {
                var upload = AliyunOSSUtils.Upload(scene, url);
                url = upload.Url;
                value = ocr.BusinessLicense(upload.Buffer);
            }
            var result = value.ToObject<BaiduOcrBusinessLicenseResult>();
            result.Url = url;
            if (result.ErrorCode.IsNotNull())
            {
                return result;
            }
            result.Model = new BaiduOcrBusinessLicenseResultModel
            {
                EnterpriseName = result.WordsResult["单位名称"].Words,
                SocietyCreditCode = result.WordsResult["社会信用代码"].Words,
                LegalPerson = result.WordsResult["法人"].Words,
                EnterpriseType = result.WordsResult["类型"].Words,
                RegisteredCapital = result.WordsResult["注册资本"].Words,
                EstablishmentDate = result.WordsResult["成立日期"].Words,
                Address = result.WordsResult["地址"].Words,
                MainBusiness = result.WordsResult["经营范围"].Words,
            };
            return result;
        }
 
        /// <summary>
        /// 身份证正面OCR
        /// </summary>
        /// <param name="url"></param>
        /// <param name="scene"></param>
        /// <param name="isOssUrl"></param>
        public static async Task<BaiduOcrIdentityFrontResult> OcrIdentityFront(this string url, string scene = null, bool isOssUrl = true)
        {
            var options = App.GetOptions<BaiduOptions>();
            var ocr = new Ocr(options.Ocr.Key, options.Ocr.Secret);
            ocr.Timeout = 60000;
            JObject value;
            if (isOssUrl)
            {
                var downloadUrl = AliyunOSSUtils.GetUrl(url);
                var buffer = await App.GetRequiredService<IHttpRemoteService>().GetAsByteArrayAsync(downloadUrl);
                value = ocr.Idcard(buffer, "front");
            }
            else
            {
                var upload = AliyunOSSUtils.Upload(scene, url);
                url = upload.Url;
                value = ocr.Idcard(upload.Buffer, "front");
            }
            var result = value.ToObject<BaiduOcrIdentityFrontResult>();
            result.Url = url;
            if (result.ErrorCode.IsNotNull())
            {
                return result;
            }
 
            result.Model = new BaiduOcrIdentityFrontResultModel
            {
                Address = result.WordsResult["住址"].Words,
                Identity = result.WordsResult["公民身份号码"].Words,
                BirthdayText = result.WordsResult["出生"].Words,
                Name = result.WordsResult["姓名"].Words,
                GenderText = result.WordsResult["性别"].Words,
                Nation = result.WordsResult["民族"].Words,
            };
            return result;
        }
 
        /// <summary>
        /// 身份证背面OCR
        /// </summary>
        /// <param name="url"></param>
        /// <param name="scene"></param>
        /// <param name="isOssUrl"></param>
        public static async Task<BaiduOcrIdentityBackResult> OcrIdentityBack(this string url, string scene = null, bool isOssUrl = true)
        {
            var options = App.GetOptions<BaiduOptions>();
            var ocr = new Ocr(options.Ocr.Key, options.Ocr.Secret);
            ocr.Timeout = 60000;
            JObject value;
            if (isOssUrl)
            {
                var downloadUrl = AliyunOSSUtils.GetUrl(url);
                var buffer = await App.GetRequiredService<IHttpRemoteService>().GetAsByteArrayAsync(downloadUrl);
                value = ocr.Idcard(buffer, "back");
            }
            else
            {
                var upload = AliyunOSSUtils.Upload(scene, url);
                url = upload.Url;
                value = ocr.Idcard(upload.Buffer, "back");
            }
            var result = value.ToObject<BaiduOcrIdentityBackResult>();
            result.Url = url;
            if (result.ErrorCode.IsNotNull())
            {
                return result;
            }
 
            result.Model = new BaiduOcrIdentityBackResultModel
            {
                ExpiryDateText = result.WordsResult["失效日期"].Words,
                IssueAuthority = result.WordsResult["签发机关"].Words,
                IssueDateText = result.WordsResult["签发日期"].Words,
            };
            return result;
        }
    }
}