· 9 years ago · Nov 04, 2016, 11:58 AM
1public class PostNewTrackableRequest
2{
3 public string name;
4 public float width;
5 public string image;
6 public string application_metadata;
7}
8
9public class CloudUpLoading : MonoBehaviour
10{
11
12 public Texture2D texture;
13
14 private string access_key = "Your Server Access Key";
15 private string secret_key = "Your Server Secret Key";
16 private string url = @"https://vws.vuforia.com";
17 private string targetName = "MyTarget"; // must change when upload another Image Target, avoid same as exist Image on cloud
18
19 private byte[] requestBytesArray;
20
21 public void CallPostTarget()
22 {
23 StartCoroutine (PostNewTarget());
24 }
25
26 IEnumerator PostNewTarget()
27 {
28
29 string requestPath = "/targets";
30 string serviceURI = url + requestPath;
31 string httpAction = "POST";
32 string contentType = "application/json";
33 string date = string.Format("{0:r}", DateTime.Now.ToUniversalTime());
34
35 Debug.Log(date);
36
37 // if your texture2d has RGb24 type, don't need to redraw new texture2d
38 Texture2D tex = new Texture2D(texture.width,texture.height,TextureFormat.RGB24,false);
39 tex.SetPixels(texture.GetPixels());
40 tex.Apply();
41 byte[] image = tex.EncodeToPNG();
42
43 string metadataStr = "Vuforia metadata";//May use for key,name...in game
44 byte[] metadata = System.Text.ASCIIEncoding.ASCII.GetBytes(metadataStr);
45 PostNewTrackableRequest model = new PostNewTrackableRequest();
46 model.name = targetName;
47 model.width = 64.0f; // don't need same as width of texture
48 model.image = System.Convert.ToBase64String(image);
49
50 model.application_metadata = System.Convert.ToBase64String(metadata);
51 string requestBody = JsonWriter.Serialize(model);
52
53 WWWForm form = new WWWForm ();
54
55 var headers = form.headers;
56 byte[] rawData = form.data;
57 headers[ "Host"]=url;
58 headers["Date"] = date;
59 headers["Content-Type"]= contentType;
60
61 HttpWebRequest httpWReq = (HttpWebRequest)HttpWebRequest.Create(serviceURI);
62
63 MD5 md5 = MD5.Create();
64 var contentMD5bytes = md5.ComputeHash(System.Text.Encoding.ASCII.GetBytes(requestBody));
65 System.Text.StringBuilder sb = new System.Text.StringBuilder();
66 for (int i = 0; i < contentMD5bytes.Length; i++)
67 {
68 sb.Append(contentMD5bytes[i].ToString("x2"));
69 }
70
71 string contentMD5 = sb.ToString();
72
73 string stringToSign = string.Format("{0}\n{1}\n{2}\n{3}\n{4}", httpAction, contentMD5, contentType, date, requestPath);
74
75 HMACSHA1 sha1 = new HMACSHA1(System.Text.Encoding.ASCII.GetBytes(secret_key));
76 byte[] sha1Bytes = System.Text.Encoding.ASCII.GetBytes(stringToSign);
77 MemoryStream stream = new MemoryStream(sha1Bytes);
78 byte[] sha1Hash = sha1.ComputeHash(stream);
79 string signature = System.Convert.ToBase64String(sha1Hash);
80
81 headers["Authorization"]=string.Format("VWS {0}:{1}", access_key, signature);
82
83 Debug.Log("<color=green>Signaturere+"</color>");
84
85 WWW request =new WWW(serviceURI,System.Text.Encoding.UTF8.GetBytes(JsonWriter.Serialize(model)), headers);
86 yield return request;
87
88 if (request.error != null)
89 {
90 Debug.Log("requestr: " + request.error);
91 }
92 else
93 {
94 Debug.Log("requestess");
95 Debug.Log("returned" + request.data);
96 }
97
98 }
99}