• Unity3D 集成 Face++ FacePlusPlus httpClient http协议 byte数组转string


    //開始由于要实现跨平台。考虑过用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 );
    }	


    
    
    
       
    
  • 相关阅读:
    第15周作业
    软件工程结课作业
    第13次作业--邮箱的正则表达式
    第12次作业
    第10次作业
    Java 8 新的时间日期库
    你还在用if-else吗?
    Java并发编程:4种线程池和缓冲队列BlockingQueue
    ZooKeeper学习第八期——ZooKeeper伸缩性(转)
    ZooKeeper学习第七期--ZooKeeper一致性原理(转)
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/5259597.html
Copyright © 2020-2023  润新知