.NETCore(人脸识别帮助类)

public class FaceRecognitionHelper
    {
        public static string Tencent_Url = ApolloService.Get("Face_URL");

        public static readonly string Tencent_appId = ApolloService.Get("Tencent_appId");

        public static readonly string Tencent_SecretId = ApolloService.Get("Tencent_SecretId");

        public static readonly string Tencent_SecretKey = ApolloService.Get("Tencent_SecretKey");

        public FaceRecognitionHelper(string tencent_Url)
        {
            FaceRecognitionHelper.Tencent_Url = tencent_Url;
        }

        private static string GetSign()
        {
            string tencent_appId = FaceRecognitionHelper.Tencent_appId;
            string tencent_SecretId = FaceRecognitionHelper.Tencent_SecretId;
            string tencent_SecretKey = FaceRecognitionHelper.Tencent_SecretKey;
            long num = Convert.ToInt64((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds);
            long num2 = Convert.ToInt64((DateTime.UtcNow.AddMinutes(180.0) - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds);
            int random = FaceRecognitionHelper.GetRandom();
            return FaceRecognitionHelper.Hmacsha1Encrypt(string.Format("a={0}&k={1}&e={2}&t={3}&r={4}&f=", tencent_appId, tencent_SecretId, num2, num, random), tencent_SecretKey);
        }

        private static string Hmacsha1Encrypt(string encryptText, string encryptKey)
        {
            using (HMACSHA1 hMACSHA = new HMACSHA1(Encoding.UTF8.GetBytes(encryptKey)))
            {
                byte[] array = hMACSHA.ComputeHash(Encoding.UTF8.GetBytes(encryptText));
                byte[] bytes = Encoding.UTF8.GetBytes(encryptText);
                byte[] array2 = new byte[array.Length + bytes.Length];
                Array.Copy(array, 0, array2, 0, array.Length);
                Array.Copy(bytes, 0, array2, array.Length, bytes.Length);
                return Convert.ToBase64String(array2);
            }
        }

        private static int GetRandom()
        {
            return new Random().Next(10000, 999999999);
        }

        public string PicIsLegitimate(string picUrl, int picModel)
        {
            //IL_0006: Unknown result type (might be due to invalid IL)
            //IL_0038: Expected Ref, but got Unknown
            //IL_0068: Unknown result type (might be due to invalid IL)
            //IL_007d: Expected Ref, but got Unknown
            string empty = string.Empty;
           // ((NLogger)(_003F)NLogger.get_Default()).Info("TencentFace请求参数:url-" + picUrl + ",模式:" + picModel, Array.Empty<object>());
            string iPostData = JsonConvert.SerializeObject(new
            {
                appid = FaceRecognitionHelper.Tencent_appId.ToString(),
                mode = picModel,
                url = picUrl
            });
            string sign = FaceRecognitionHelper.GetSign();
            string str = WebUtil.PostWebRequestByType(FaceRecognitionHelper.Tencent_Url, iPostData, WebUtil.HttpContentType.JSON, sign);
           // ((NLogger)(_003F)NLogger.get_Default()).Info("TencentFace返回参数:" + str, Array.Empty<object>());
            return empty;
        }

        public string PicCompareByUrl(string urlA, string urlB)
        {
            //IL_0006: Unknown result type (might be due to invalid IL)
            //IL_0021: Expected Ref, but got Unknown
            //IL_0051: Unknown result type (might be due to invalid IL)
            //IL_0066: Expected Ref, but got Unknown
            //IL_0091: Unknown result type (might be due to invalid IL)
            //IL_00a7: Expected Ref, but got Unknown
            string result = "0";
            try
            {
                //((NLogger)(_003F)NLogger.get_Default()).Info("TencentFace请求参数:" + urlA + "," + urlB, Array.Empty<object>());
                string iPostData = JsonConvert.SerializeObject(new
                {
                    appid = FaceRecognitionHelper.Tencent_appId.ToString(),
                    urlA = urlA,
                    urlB = urlB
                });
                string sign = FaceRecognitionHelper.GetSign();
                string text = WebUtil.PostWebRequestByType(FaceRecognitionHelper.Tencent_Url, iPostData, WebUtil.HttpContentType.JSON, sign);
                //((NLogger)(_003F)NLogger.get_Default()).Info("TencentFace返回参数(URl):" + text, Array.Empty<object>());
                FaceDetect faceDetect = JsonConvert.DeserializeObject<FaceDetect>(text);
                if (faceDetect != null)
                {
                    if (faceDetect.data != null)
                    {
                        result = faceDetect.data.similarity;
                        return result;
                    }
                    return result;
                }
                return result;
            }
            catch (Exception arg)
            {
                //((NLogger)(_003F)NLogger.get_Default()).Info("TencentFace错误信息(Url):" + arg, Array.Empty<object>());
                return result;
            }
        }

        //public string PicCompareByBinary(HttpPostedFile imageA, HttpPostedFile imageB)
        //{
        //    //IL_00b4: Unknown result type (might be due to invalid IL)
        //    //IL_00ca: Expected Ref, but got Unknown
        //    //IL_00f6: Unknown result type (might be due to invalid IL)
        //    //IL_010c: Expected Ref, but got Unknown
        //    string result = "0";
        //    try
        //    {
        //        string sign = FaceRecognitionHelper.GetSign();
        //        byte[] file = new BinaryReader(imageA.InputStream).ReadBytes(10000000);
        //        byte[] file2 = new BinaryReader(imageB.InputStream).ReadBytes(10000000);
        //        Dictionary<string, object> dictionary = new Dictionary<string, object>();
        //        dictionary.Add("appid", FaceRecognitionHelper.Tencent_appId.ToString());
        //        dictionary.Add("imageA", new HttpHelperMultipartForm.FileParameter(file, imageA.FileName, imageA.ContentType));
        //        dictionary.Add("imageB", new HttpHelperMultipartForm.FileParameter(file2, imageB.FileName, imageB.ContentType));
        //        string text = new StreamReader(HttpHelperMultipartForm.MultipartFormDataPost(FaceRecognitionHelper.Tencent_Url, "", dictionary, sign).GetResponseStream()).ReadToEnd();
        //       // ((NLogger)(_003F)NLogger.get_Default()).Info("TencentFace返回参数(Binary):" + text, Array.Empty<object>());
        //        FaceDetect faceDetect = JsonConvert.DeserializeObject<FaceDetect>(text);
        //        if (faceDetect != null)
        //        {
        //            if (faceDetect.data != null)
        //            {
        //                result = faceDetect.data.similarity;
        //                return result;
        //            }
        //            return result;
        //        }
        //        return result;
        //    }
        //    catch (Exception arg)
        //    {
        //        //((NLogger)(_003F)NLogger.get_Default()).Info("TencentFace错误信息(Binary):" + arg, Array.Empty<object>());
        //        return result;
        //    }
        //}
    }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值