移动系统liao
2024-08-15 d6e560e1d30bd6af259b0c07ffb51b9caf8ab925
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using TencentCloud.Ocr.V20181119;
using TencentCloud.Ocr.V20181119.Models;
using Newtonsoft.Json;
using TencentCloud.Common;
using TencentCloud.Common.Profile;
using Furion.DependencyInjection;
using Furion;
 
namespace EzTencentCloud
{
    public class TencentCloudService: ITencentCloudService, IScoped
    {
        /// <summary>
        /// 剪裁后的身份证图片
        /// </summary>
        public string CropIdCard { get; set; }
 
        public string GetIdCordImg()
        {
            if (string.IsNullOrEmpty(CropIdCard))
                return "";
            else
                return CropIdCard;
 
        }
 
        public   IDCardOCRResponse IdCord(string ImageBase64, bool isFRONT)
        {
            try
            {
                Credential cred = new Credential
                {
                    SecretId = App.Configuration["TencentCloud:SecretId"] ??"AKIDIPFp9CyThfMmvoQlpeCl34pKYVBahY9T",
                    SecretKey = App.Configuration["TencentCloud:SecretKey"] ?? "4rNcaHhrkMhmb9QQ9bmgKipfFZcOt86n"
                };
 
                ClientProfile clientProfile = new ClientProfile();
                HttpProfile httpProfile = new HttpProfile();
 
 
                httpProfile.Endpoint = ("ocr.tencentcloudapi.com");
                clientProfile.HttpProfile = httpProfile;
 
                OcrClient client = new OcrClient(cred, "ap-beijing", clientProfile);
                IDCardOCRRequest req = new IDCardOCRRequest();
                if (isFRONT)
                    req.CardSide = "FRONT";
                else
                    req.CardSide = "BACK";
                req.ImageBase64 = ImageBase64;
 
                req.Config = JsonConvert.SerializeObject(new
                {
                    //身份证照片裁剪(去掉证件外多余的边缘、自动矫正拍摄角度)
                    CropIdCard = true,
                    // CropPortrait = true,
                    //边框和框内遮挡告警
                    BorderCheckWarn = true,
                    //PS检测告警
                    DetectPsWarn = true,
                    //临时身份证告警
                    TempIdWarn = true,
                    //身份证有效日期不合法告警
                    InvalidDateWarn = true,
                    //图片质量分数(评价图片的模糊程度)
                    Quality = true,
 
                });
                IDCardOCRResponse resp =  client.IDCardOCRSync(req);
 
                if (resp == null)
                    throw new Exception("图片无法识别,请重新选择身份证图片");
 
 
                var adv = JsonConvert.DeserializeObject<AdvancedInfo>(resp.AdvancedInfo);
                if (adv == null)
                    throw new Exception("图片无法识别,请重新选择身份证图片");
                if (adv?.BorderCodeValue != null && adv.BorderCodeValue > 50)
                    throw new Exception("身份证图片不完整,请重新选择身份证图片");
                if (adv?.Quality != null && adv.Quality < 50)
                    throw new Exception("图片模糊,请重新选择身份证图片");
                if (adv?.WarnInfos?.Where(x => x == -9100) == null)
                    throw new Exception("身份证日期不合法,请重新选择身份证图片");
                if (adv?.WarnInfos?.Where(x => x == -9106) == null)
                    throw new Exception("该图片可能是被PS过,请重新选择身份证图片");
 
                CropIdCard = adv.IdCard;
                return resp;
            }
            catch (Exception)
            {
                throw;
            }
 
 
        }
 
 
    }
 
    //返回扩展参数
    internal class AdvancedInfo
    {
        /// <summary>
        /// idCard 裁剪后身份证照片的base64编码,请求 Config.CropIdCard 时返回;
        /// </summary>
        public string? IdCard { get; set; }
        /// <summary>
        /// 身份证头像照片的base64编码,请求 Config.CropPortrait 时返回;
        /// </summary>
        public string? Portrait { get; set; }
        /// <summary>
        /// 图片质量分数,请求 Config.Quality 时返回(取值范围:0 ~ 100,分数越低越模糊,建议阈值≥50);
        /// </summary>
        public int? Quality { get; set; }
        /// <summary>
        /// 身份证边框不完整告警阈值分数,请求 Config.BorderCheckWarn时返回(取值范围:0 ~ 100,分数越低边框遮挡可能性越低,建议阈值≤50);
        /// </summary>
        public int BorderCodeValue { get; set; }
        /// <summary>
        /// 告警信息,Code 告警码列表和释义:
        ///-9100 身份证有效日期不合法告警,
        ///-9101 身份证边框不完整告警,
        ///-9102 身份证复印件告警,
        ///-9103 身份证翻拍告警,
        ///-9105 身份证框内遮挡告警,
        ///-9104 临时身份证告警,
        ///-9106 身份证 PS 告警,
        ///-9107 身份证反光告警。
        /// </summary>
        public List<int> WarnInfos { get; set; }
    }
 
}