Log in or register to post comments

Unity c# use WWW for upload image target to Cloud

November 19, 2015 - 6:46pm #1

I searched a lot of pages, however they do not has a full code. Of course, they had already dicuss about that, so I collect and combine new code for my self and now share for any one who need to upload image target to cloud. Sorry, my English is not very well ! 

Thank For the code of  #lukiller from https://developer.vuforia.com/forum/cloud-reco-beta/problem-connecting-web-service

I just modified a little bit, but use the easier way.


public class PostNewTrackableRequest
    public string name;
    public float width;
    public string image;
    public string application_metadata;

public class CloudUpLoading : MonoBehaviour 

 public Texture2D texture;

 private string access_key = "Your Server Access Key";
 private string secret_key = "Your Server Secret Key";
 private string url = @"https://vws.vuforia.com";
 private string targetName = "MyTarget"; // must change when upload another Image Target, avoid same as exist Image on cloud

 private byte[] requestBytesArray;

 public void CallPostTarget()
     StartCoroutine (PostNewTarget());

 IEnumerator PostNewTarget()

     string requestPath = "/targets";
     string serviceURI = url + requestPath;
     string httpAction = "POST";
     string contentType = "application/json";
     string date = string.Format("{0:r}", DateTime.Now.ToUniversalTime()); 
     // if your texture2d has RGb24 type, don't need to redraw new texture2d
     Texture2D tex = new Texture2D(texture.width,texture.height,TextureFormat.RGB24,false);
     byte[] image = tex.EncodeToPNG();
     string metadataStr = "Vuforia metadata";//May use for key,name...in game
     byte[] metadata = System.Text.ASCIIEncoding.ASCII.GetBytes(metadataStr);
     PostNewTrackableRequest model = new PostNewTrackableRequest();
     model.name = targetName;
     model.width = 64.0f; // don't need same as width of texture
     model.image = System.Convert.ToBase64String(image);
     model.application_metadata = System.Convert.ToBase64String(metadata);
     string requestBody = JsonWriter.Serialize(model);

     WWWForm form = new WWWForm ();

     var headers = form.headers;
     byte[] rawData = form.data;
     headers[ "Host"]=url;
     headers["Date"] = date;
     headers["Content-Type"]= contentType;

     HttpWebRequest httpWReq = (HttpWebRequest)HttpWebRequest.Create(serviceURI);
     MD5 md5 = MD5.Create();
     var contentMD5bytes = md5.ComputeHash(System.Text.Encoding.ASCII.GetBytes(requestBody));
     System.Text.StringBuilder sb = new System.Text.StringBuilder();
     for (int i = 0; i < contentMD5bytes.Length; i++)
     string contentMD5 = sb.ToString();
     string stringToSign = string.Format("{0}\n{1}\n{2}\n{3}\n{4}", httpAction, contentMD5, contentType, date, requestPath);
     HMACSHA1 sha1 = new HMACSHA1(System.Text.Encoding.ASCII.GetBytes(secret_key));
     byte[] sha1Bytes = System.Text.Encoding.ASCII.GetBytes(stringToSign);
     MemoryStream stream = new MemoryStream(sha1Bytes);
     byte[] sha1Hash = sha1.ComputeHash(stream);
     string signature = System.Convert.ToBase64String(sha1Hash);

     headers["Authorization"]=string.Format("VWS {0}:{1}", access_key, signature);
     Debug.Log("<color=green>Signature: "+signature+"</color>");

     WWW request =new WWW(serviceURI,System.Text.Encoding.UTF8.GetBytes(JsonWriter.Serialize(model)), headers);
     yield return request;

     if (request.error != null)
         Debug.Log("request error: " + request.error);
         Debug.Log("request success");
         Debug.Log("returned data" + request.data);




you can go to https://breakdownblogs.wordpress.com/2015/11/13/adding-image-target-to-cloud-database-use-api-vuforia-and-www-unity/

for read more and get link for download dll support for the code. Cheer !

Unity c# use WWW for upload image target to Cloud

November 23, 2015 - 2:15am #2

Very thanks man! :)

Log in or register to post comments