In our previous blog we know that how to generate signature to perform valid api request. Now we ll see how to get target image id from vuforia database.
We know that how to find the path of an image after taking it from camera or gallery so we have that image path.
public class uploadToServer extends AsyncTask<Void, Void, String> {
String mPath;
JSONObject jsonObject;
JSONArray jsonArray;
String targetId;
ProgressDialog pd;
uploadToServer(String path){
mPath = path;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(CameraActivity.this);
pd.setMessage("Searching Wine...");
pd.show();
}
@Override
protected String doInBackground(Void... params) {
//create a new file for that image path
File file = new File(mPath);
// create a file body so that we can send it in multipart request
FileBody bin1 = new FileBody(file, ContentType.create("image/jpeg"),file.getName());
Log.e("Enter", "Filebody complete " + bin1);
// Multipart entity is basically used to perform multipart request using simple http post request and add file body in it.
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart("image", bin1);
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost();
httppost.setURI(new URI(AppConstant.URL_VUFORIA_IMAGE_SEARCH));
httppost.setEntity(multipartEntity);
Log.e("Enter", "Image send complete");
// put all those method we shared in previous blog in a class named SignatureBuilder.
SignatureBuilder sb = new SignatureBuilder();
// put Date in header and this should be same in signature
httppost.setHeader(new BasicHeader("Date", DateUtils.formatDate(new Date()).replaceFirst("[+]00:00$", "")));
// create encrypt signature using previous blog
httppost.setHeader("Authorization", "VWS " + accessKey + ":" + sb.tmsSignature(httppost, secretKey));
Log.e("Authorization","VWS " + accessKey + ":" + sb.tmsSignature(httppost, secretKey));
HttpResponse response = httpclient.execute(httppost);
String st = EntityUtils.toString(response.getEntity());
Log.e("log_tag", "In the try Loop" + st);
jsonObject = new JSONObject(st);
jsonArray = jsonObject.getJSONArray("results");
}
catch (IllegalArgumentException e1) {
e1.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
Log.e("log_tag", "Error in http connection " + e.toString());
}
return "Success";
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
pd.hide();
pd.dismiss();
if (jsonArray.length() > 0){
Log.e("array length vuforia", "" + jsonArray.length());
try {
targetId = jsonArray.getJSONObject(jsonArray.length()-1).getString("target_id");
// Here you will get target id in response
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("array length vuforia", "" + jsonArray.length());
showAlertDialogForEntry();
}
}
}
with this blog you will get list of matching image targets in json array, you can also set some meta deta information during image upload like name, date, id etc.
0 Comment(s)