Moli
2022-04-15 22477f0eca904ec628ab59cd749f959af35c1439
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using TencentCloud.Common;
using TencentCloud.Common.Profile;
using TencentCloud.Ocr.V20181119;
using TencentCloud.Ocr.V20181119.Models;
 
namespace Helpers
{
    public static class PublicApiHelpers
    {
 
 
        #region 腾讯API接口
        
        /// <summary>
        /// 发送“通用印刷体识别(高精度版)”请求(腾讯)
        /// </summary>
        /// <param name="SecretId">密钥参数SecretId</param>
        /// <param name="SecretKey">密钥参数SecretKey</param>
        /// <param name="Endpoint">请求URL</param>
        /// <param name="Region">地域参数,用来标识希望操作哪个地域的数据。</param>
        /// <param name="ImageUrl">
        /// 图片的 Url 地址。
        /// 要求图片经Base64编码后不超过 7M,分辨率建议600*800以上
        /// 支持PNG、JPG、JPEG、BMP格式。
        /// </param>
        /// <returns></returns>
        public static string SendGeneralAccurateOCRTC(string SecretId, string SecretKey, string Endpoint, string Region, string ImageUrl,string ImageBase64="")
        {
            try
            {
                Credential cred = new Credential
                {
                    SecretId = SecretId,
                    SecretKey = SecretKey
                };
 
                ClientProfile clientProfile = new ClientProfile();
                HttpProfile httpProfile = new HttpProfile();
                httpProfile.Endpoint = (Endpoint);
                clientProfile.HttpProfile = httpProfile;
 
                OcrClient client = new OcrClient(cred, Region, clientProfile);
                GeneralAccurateOCRRequest req = new GeneralAccurateOCRRequest();
                if (!string.IsNullOrWhiteSpace(ImageUrl)) {
                    req.ImageUrl = ImageUrl;
                }
                else {
                    req.ImageBase64 = ImageBase64;
                }
                GeneralAccurateOCRResponse resp = client.GeneralAccurateOCRSync(req);
                return AbstractModel.ToJsonString(resp);
            }
            catch (Exception e)
            {
                Helpers.Tool.PrintLog_Error("程序(SendGeneralAccurateOCRTC)运行异常:" + e.Message, e.StackTrace+"\r\n"+ ImageUrl);
                return string.Empty;
            }
        }
 
        #endregion
 
        #region 百度API接口
 
        //private static string GetAccessTokenBD()
        //{
        //    var config = Tool.GetSystemConfig().BDApi;
 
        //    Dictionary<string, string> dic = new Dictionary<string, string>();
        //    dic.Add("grant_type", config.accessToken.grant_type);
        //    dic.Add("client_id", config.accessToken.client_id);
        //    dic.Add("client_secret", config.accessToken.client_secret);
        //    string paramStr = Tool.DicToUrlParam(dic);
 
        //    string res = Tool.HttpPost(config.accessToken.RequestUrl, paramStr);
 
        //    if (res.IndexOf("error") > -1)
        //    {
        //        //请求token错误,记录下错误情况
 
        //        Tool.PrintLog_Error("百度API Token验证获取失败", $"百度API Token验证获取失败,请求参数:{paramStr},请求地址:{config.accessToken.RequestUrl},返回结果:{res}");
        //        return string.Empty;
        //    }
 
        //    Hashtable hst = res.ToModel<Hashtable>();
 
        //    //记录到redis中
 
        //    return "";
        //}
 
 
        //public static string SendGeneralAccurateOCRBD() { }
 
#endregion
    }
}