sunpengfei
2025-08-20 65c143397772e0c2458a243f4110863854478ddc
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
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;
        }
 
    }
}