Adding Image Target to Cloud Database use API Vuforia and www Unity

Forget my bad English, just focus on what i did. I searched for a huge posts on the Internet, but “Add image target to cloud of Vuforia” topics are a little bit, and all of them support for native java. That made I have coding for myself (reference some code from vuforia forum)  and I succeeded. So, I want to share my code for help other people like me. Let’s begin !

Firstly, you should download a Json dll library that support for serialize a data for upload to the server. JsonFx.Json and learn a little bit about a structure from Vuforia Web Services , Vuforia example

Secondly, copy the code below, and drag image to the code for new texture. Add Your Key and build.

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 = @"";
 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, game
     byte[] metadata = System.Text.ASCIIEncoding.ASCII.GetBytes(metadataStr);
     PostNewTrackableRequest model = new PostNewTrackableRequest(); = 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 =;
     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" +;



6 thoughts on “Adding Image Target to Cloud Database use API Vuforia and www Unity

  1. hi! first of all thanks for sharing your code! Its very useful!!
    i am trying to create an application in unity, where the user can upload its own targets in the cloud. The script you wrote, do i have to attach it in a specific object in unity? And if i understand your code, you use “Texture2D tex = new Texture2D(texture.width,texture.height,TextureFormat.RGB24,false);” in order to take the image you want and to upload it as a target, correct? i managed to access unity camera, save my img in the folder and now i want to upload it. How i connect my image to the script you wrote? thanks in advance!!!!!

    Số lượt thích

    • Yes, you can attach to the specific file of image as i did. And I used Texture2D tex = new Texture2D… for redraw if your image file not rbg24 type (you can see properties of file in computer). It will refix the image which is going to upload into the cloud. You will read your images as Texture2D type, then create class CloudUpLoading and provide public, private variables as I commented in the code, then call CallPostTarget, At here you got it!

      Sory about my English It not very well, and Now I’m just online at the weekend so may I can not reply imediately!

      Số lượt thích

  2. Hi

    I’m trying to use your code on unity 5.5 (vuforia 6), and have made the steps you said, attached the image to the script on the plane gameobject. Ran the build on my android phone and nothing happens, the new image target doesn’t upload to my vuforia cloud. Can you explain me details how you made it work?


    Số lượt thích

  3. If i remember exactly, this code need to change a little bit when you want to use for android. But i lost that file. You can use console log for check a message from the server of vuforia. If it does not return the message, check accout information. If its return, see what group in the code can be wrong and use console log for it.
    Hope this guid can help you!

    Số lượt thích

Trả lời

Mời bạn điền thông tin vào ô dưới đây hoặc kích vào một biểu tượng để đăng nhập: Logo

Bạn đang bình luận bằng tài khoản Đăng xuất /  Thay đổi )

Google+ photo

Bạn đang bình luận bằng tài khoản Google+ Đăng xuất /  Thay đổi )

Twitter picture

Bạn đang bình luận bằng tài khoản Twitter Đăng xuất /  Thay đổi )

Facebook photo

Bạn đang bình luận bằng tài khoản Facebook Đăng xuất /  Thay đổi )

Connecting to %s