using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
using System.IO;
public class FaceTest : MonoBehaviour
{
private string api_key = "f0b0a4183f4ecf32a8791e1fc4cee84d";
private string api_secret = "iAfFeuo5eH53U4Os5w1FEzS0KA-Rm4uC";
private string url = "http://apicn.faceplusplus.com/v2/detection/detect?";
private string parameters = "&attribute=glass,pose,gender,age,race,smiling";
private string imgName = "temp.jpg";
public Texture2D img = null;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnGUI()
{
if (img != null)
{
GUI.DrawTexture(new Rect(0, 35, img.width, img.height), img);
}
if (GUI.Button(new Rect(0, 0, 100, 35), "OpenDialog"))
{
OpenFileName ofn = new OpenFileName();
ofn.structSize = Marshal.SizeOf(ofn);
ofn.filter = "图片文件(*.jpg*.png)\0*.jpg;*.png";
ofn.file = new string(new char[256]);
ofn.maxFile = ofn.file.Length;
ofn.fileTitle = new string(new char[64]);
ofn.maxFileTitle = ofn.fileTitle.Length;
ofn.initialDir = UnityEngine.Application.dataPath;//默认路径
ofn.title = "Open Project";
ofn.defExt = "JPG";
ofn.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;//OFN_EXPLORER|OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST| OFN_ALLOWMULTISELECT|OFN_NOCHANGEDIR
if (WindowDll.GetOpenFileName(ofn))
{
Debug.Log("Selected file with full path: {0}" + ofn.file);
if (!Directory.Exists(UnityEngine.Application.streamingAssetsPath))
{
Directory.CreateDirectory(UnityEngine.Application.streamingAssetsPath);
}
File.Copy(ofn.file, UnityEngine.Application.streamingAssetsPath + "/temp.jpg");
Debug.Log("file://" + ofn.file);
StartCoroutine(LoadTexture("file://" + UnityEngine.Application.streamingAssetsPath + "/temp.jpg"));
}
}
}
IEnumerator LoadTexture(string localUrl)
{
WWW www = new WWW(localUrl);
yield return www;
img = www.texture;
byte[] pngData = www.texture.EncodeToJPG();
File.Delete(UnityEngine.Application.streamingAssetsPath + "/temp.jpg");
StartCoroutine(PostImage(pngData));
}
IEnumerator PostImage(byte[] imgBytes)
{
WWWForm form = new WWWForm();
form.AddField("api_key", api_key);
form.AddField("api_secret", api_secret);
form.AddBinaryData("img", imgBytes, "temp.jpg", "image/jpg");
form.AddField("attribute", "glass,pose,gender,age,race,smiling");
WWW www = new WWW(url, form);
yield return www;
Debug.Log(www.text);
}
}
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class OpenFileName
{
public int structSize = 0;
public IntPtr dlgOwner = IntPtr.Zero;
public IntPtr instance = IntPtr.Zero;
public String filter = null;
public String customFilter = null;
public int maxCustFilter = 0;
public int filterIndex = 0;
public String file = null;
public int maxFile = 0;
public String fileTitle = null;
public int maxFileTitle = 0;
public String initialDir = null;
public String title = null;
public int flags = 0;
public short fileOffset = 0;
public short fileExtension = 0;
public String defExt = null;
public IntPtr custData = IntPtr.Zero;
public IntPtr hook = IntPtr.Zero;
public String templateName = null;
public IntPtr reservedPtr = IntPtr.Zero;
public int reservedInt = 0;
public int flagsEx = 0;
}
public class WindowDll
{
[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
public static extern bool GetOpenFileName([In, Out] OpenFileName ofn);
public static bool GetOpenFileName1([In, Out] OpenFileName ofn)
{
return GetOpenFileName(ofn);
}
}