赞
踩
Face++融合时融合是整体的 例如你戴眼镜 模板图不带眼睛 融合后就是戴眼镜的 融合上传的图片可以是文件流也可以是Base64
private const string KEY = "XP9kdJPbF-4UugXEDr*********R"; private const string SECRET = "T5mccUDL6GHc-gR**********"; private const string URL = "https://api-cn.faceplusplus.com/imagepp/v1/mergeface"; Dictionary<string, object> verifyPostParameters = new Dictionary<string, object>(); verifyPostParameters.Add("api_key", KEY); verifyPostParameters.Add("api_secret", SECRET); //添加模板图片参数 verifyPostParameters.Add("template_file", new HttpHelper4MultipartForm.FileParameter(Texture2DToBytes(template), "1.jpg", "application/octet-stream")); // verifyPostParameters.Add("template_file",base64); //添加合并图片参数 verifyPostParameters.Add("merge_file", new HttpHelper4MultipartForm.FileParameter(Texture2DToBytes(merget), "2.jpg", "application/octet-stream")); // verifyPostParameters.Add(""merge_file"",base64); //添加融合比例参数 merge_rate = gameManager.config.mergeRate; verifyPostParameters.Add("merge_rate", merge_rate.ToString()); //添加五官融合比例 feature_rate = gameManager.config.featureRate; verifyPostParameters.Add("feature_rate", feature_rate.ToString()); HttpWebResponse verifyResponse = HttpHelper4MultipartForm.MultipartFormDataPost(URL, "", verifyPostParameters); using (StreamReader reader = new StreamReader(verifyResponse.GetResponseStream(), System.Text.Encoding.UTF8)) { //返回值处理 A a = JsonUtility.FromJson<A>(reader.ReadToEnd()); Debug.Log(a.time_used); byte[] b; Texture2D temp; if (string.IsNullOrEmpty(a.result)) { //如果返回的纹理是null,则使用模版纹理 temp = template; Debug.Log("没有检测到人,使用模版纹理。"); } else { //如果返回的纹理非null,则使用返回纹理 b = Convert.FromBase64String(a.result); temp = new Texture2D(0, 0); temp.LoadImage(b); temp.Apply(); Debug.Log("检测到人,使用返回纹理。"); } }
中间使用了HttpHelper4MultipartForm函数
using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Net.Security; using System.Security.Cryptography.X509Certificates; using System.Text; using System.Threading; namespace MgLiveCOM_Eyes { public static class HttpHelper4MultipartForm { public class FileParameter { public byte[] File { get; set; } public string FileName { get; set; } public string ContentType { get; set; } public FileParameter(byte[] file) : this(file, null) { } public FileParameter(byte[] file, string filename) : this(file, filename, null) { } public FileParameter(byte[] file, string filename, string contenttype) { this.File = file; this.FileName = filename; this.ContentType = contenttype; } } private static readonly Encoding encoding = Encoding.UTF8; /// <summary> /// MultipartForm请求 /// </summary> /// <param name="postUrl">服务地址</param> /// <param name="userAgent"></param> /// <param name="postParameters">参数</param> /// <returns></returns> public static HttpWebResponse MultipartFormDataPost(string postUrl, string userAgent, Dictionary<string, object> postParameters) { string text = string.Format("----------{0:N}", Guid.NewGuid()); string contentType = "multipart/form-data; boundary=" + text;//multipart类型 byte[] multipartFormData = HttpHelper4MultipartForm.GetMultipartFormData(postParameters, text); return HttpHelper4MultipartForm.PostForm(postUrl, userAgent, contentType, multipartFormData); } private static HttpWebResponse PostForm(string postUrl, string userAgent, string contentType, byte[] formData) { HttpWebRequest httpWebRequest = WebRequest.Create(postUrl) as HttpWebRequest; if (httpWebRequest == null) { throw new NullReferenceException("request is not a http request"); } httpWebRequest.Method = "POST";//post方式 httpWebRequest.SendChunked = false; httpWebRequest.KeepAlive = true; httpWebRequest.Proxy = null; httpWebRequest.Timeout = Timeout.Infinite; httpWebRequest.ReadWriteTimeout = Timeout.Infinite; httpWebRequest.AllowWriteStreamBuffering = false; httpWebRequest.ProtocolVersion = HttpVersion.Version11; httpWebRequest.ContentType = contentType; httpWebRequest.CookieContainer = new CookieContainer(); httpWebRequest.ContentLength = (long)formData.Length; try { using (Stream requestStream = httpWebRequest.GetRequestStream()) { int bufferSize = 4096; int position = 0; while (position < formData.Length) { bufferSize = Math.Min(bufferSize, formData.Length - position); byte[] data = new byte[bufferSize]; Array.Copy(formData, position, data, 0, bufferSize); requestStream.Write(data, 0, data.Length); position += data.Length; } requestStream.Close(); } } catch (Exception ex) { return null; } HttpWebResponse result; try { result = (httpWebRequest.GetResponse() as HttpWebResponse); } catch (WebException arg_9C_0) { result = (arg_9C_0.Response as HttpWebResponse); } return result; } public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; } /// <summary> /// 从表单中获取数据 /// </summary> /// <param name="postParameters"></param> /// <param name="boundary"></param> /// <returns></returns> private static byte[] GetMultipartFormData(Dictionary<string, object> postParameters, string boundary) { Stream stream = new MemoryStream(); bool flag = false; foreach (KeyValuePair<string, object> current in postParameters) { if (flag) { stream.Write(HttpHelper4MultipartForm.encoding.GetBytes("\r\n"), 0, HttpHelper4MultipartForm.encoding.GetByteCount("\r\n")); } flag = true; if (current.Value is HttpHelper4MultipartForm.FileParameter) { HttpHelper4MultipartForm.FileParameter fileParameter = (HttpHelper4MultipartForm.FileParameter)current.Value; string s = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\"\r\nContent-Type: {3}\r\n\r\n", new object[] { boundary, current.Key, fileParameter.FileName ?? current.Key, fileParameter.ContentType ?? "application/octet-stream" }); stream.Write(HttpHelper4MultipartForm.encoding.GetBytes(s), 0, HttpHelper4MultipartForm.encoding.GetByteCount(s)); stream.Write(fileParameter.File, 0, fileParameter.File.Length); } else { string s2 = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}", boundary, current.Key, current.Value); stream.Write(HttpHelper4MultipartForm.encoding.GetBytes(s2), 0, HttpHelper4MultipartForm.encoding.GetByteCount(s2)); } } string s3 = "\r\n--" + boundary + "--\r\n"; stream.Write(HttpHelper4MultipartForm.encoding.GetBytes(s3), 0, HttpHelper4MultipartForm.encoding.GetByteCount(s3)); stream.Position = 0L; byte[] array = new byte[stream.Length]; stream.Read(array, 0, array.Length); stream.Close(); return array; } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。