using LitJson;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web;
using UnityEngine;
using UnityEngine.UI;
public class OpenCamera : MonoBehaviour
{
public Image img;
private void Start()
{
}
public void OPEn()
{
Invoke("open", 2);
}
public void open()
{
Base64ToImg(img, body_seg());
}
static string APP_ID = "";
static string API_KEY = "";
static string SECRET_KEY = "";
// 人像分割 POST
public static String TOKEN = "";
// 百度云中开通对应服务应用的 API Key 建议开通应用的时候多选服务
private static String clientId = "M3GAqRj1QeQGsATjFKTsVBVI";
// 百度云中开通对应服务应用的 Secret Key
private static String clientSecret = "hXo5EdROAzzagouzoHgC07EezrytNzQI";
public String getAccessToken()
{
String authHost = "https://aip.baidubce.com/oauth/2.0/token";
HttpClient client = new HttpClient();
List<KeyValuePair<String, String>> paraList = new List<KeyValuePair<string, string>>();
paraList.Add(new KeyValuePair<string, string>("grant_type", "client_credentials"));
paraList.Add(new KeyValuePair<string, string>("client_id", clientId));
paraList.Add(new KeyValuePair<string, string>("client_secret", clientSecret));
HttpResponseMessage response = client.PostAsync(authHost, new FormUrlEncodedContent(paraList)).Result;
String result = response.Content.ReadAsStringAsync().Result;
//Debug.Log("REsult:" + result);
return result;
}
public string body_seg()
{
string token = "24.ad0cd3223538a86613ba068acb99bb73.2592000.1602990697.282335-19410153";
string host = "https://aip.baidubce.com/rest/2.0/image-classify/v1/body_seg?access_token=" + token;
Encoding encoding = Encoding.Default;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host);
request.Method = "post";
request.KeepAlive = true;
// 图片的base64编码
string base64 = getFileBase64(Application.streamingAssetsPath + "/1.png");
String str = "image=" + HttpUtility.UrlEncode(base64);
byte[] buffer = encoding.GetBytes(str);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
string result = reader.ReadToEnd();
Debug.Log(result);
string newBase64= json(result);
return newBase64;
}
public string json(string str)
{
JsonData jd;
jd = JsonMapper.ToObject(str);
return jd["foreground"].ToString();
}
public static String getFileBase64(String fileName)
{
FileStream filestream = new FileStream(fileName, FileMode.Open);
byte[] arr = new byte[filestream.Length];
filestream.Read(arr, 0, (int)filestream.Length);
string baser64 = Convert.ToBase64String(arr);
filestream.Close();
return baser64;
}
public void Base64ToImg(Image imgComponent, string base64)
{
byte[] bytes = Convert.FromBase64String(base64);
Texture2D tex2D = new Texture2D(100, 100);
tex2D.LoadImage(bytes);
Sprite s = Sprite.Create(tex2D, new Rect(0, 0, tex2D.width, tex2D.height), new Vector2(0.5f, 0.5f));
imgComponent.sprite = s;
Resources.UnloadUnusedAssets();
}
}