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;
// }
//}
}
.NETCore(人脸识别帮助类)
最新推荐文章于 2024-05-06 10:45:23 发布