//開始由于要实现跨平台。考虑过用curl封装c++的dll(android *.so)的方式,在c#Dllimport实现
//后来发现Unity3D本身支持http协议。且face++的api都是http訪问返回json串的。
//在看过face++ phpsdk c#sdk androidsdk之后突发奇想,想到例如以下方法
//Unity3d 集成 Face++的好方法
public float fWaitProgress = 0;
public string strResult = "";
//POST请求(多个參数)
public IEnumerator POST(string url, Dictionary<string, string> post)
{
//表单
WWWForm form = new WWWForm();
form.AddField("api_key", FACE_KEY);
form.AddField("api_secret", FACE_SECRET);
//从集合中取出全部參数。设置表单參数(AddField()).
foreach (KeyValuePair<string, string> post_arg in post)
{
form.AddField(post_arg.Key, post_arg.Value);
}
//表单传值,就是post
WWW www = new WWW(url, form);
yield return www;
fWaitProgress = www.progress;
if (www.error != null)
{
//POST请求失败
strResult = "error :" + www.error;
}
else
{
//POST请求成功
strResult = www.text;
}
}
//POST请求(单个參数)
public IEnumerator POST(string url, string key, string value)
{
//表单
WWWForm form = new WWWForm();
form.AddField("api_key", FACE_KEY);
form.AddField("api_secret", FACE_SECRET);
form.AddField(key, value);
//表单传值,就是post
WWW www = new WWW(url, form);
yield return www;
fWaitProgress = www.progress;
if (www.error != null)
{
//POST请求失败
strResult = "error :" + www.error;
}
else
{
//POST请求成功
strResult = www.text;
}
}
//POST请求(二进制參数)
public IEnumerator POST(string url, string key, byte [] bytes,string name,string type)
{
//表单
WWWForm form = new WWWForm();
form.AddField("api_key", FACE_KEY);
form.AddField("api_secret", FACE_SECRET);
form.AddBinaryData(key, bytes, name, type);
//表单传值。就是post
WWW www = new WWW(url, form);
yield return www;
fWaitProgress = www.progress;
if (www.error != null)
{
//POST请求失败
strResult = "error :" + www.error;
}
else
{
//POST请求成功
strResult = www.text;
}
}
//使用方法
if (GUI.Button(new Rect(10, 130, 100, 60), "post"))
{
//byte[] pngData = texture2D.EncodeToPNG();
//StartCoroutine(POST(URL_DETECT ,"img",pngData,"test.png","image/png"));
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("url","http://aaaa.com/a.jpg");
//dic.Add("img","c:\\a.jpg");
StartCoroutine(POST(URL_DETECT ,dic));
//StartCoroutine(POST(URL_DETECT ,"img","c:\\a.jpg"));
//unity byte数组转string
//byte[] pngData = texture2D.EncodeToPNG();
//string strData = System.Text.Encoding.Default.GetString(pngData,0,pngData.Length );
}