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