I'm making a little project using the Google Vision API. I want to detect the face of a base64 encoded image that a send to the API in a POST request. My code is based on this tutorial of Google: https://cloud.google.com/community/tutorials/make-an-http-request-to-the-cloud-vision-api-from-java. Apparently it is possible to send a base64 encoded image.
Here is my code:
File File = new File(args[2]);
String ImageString;
FileInputStream fileInputStreamReader = new FileInputStream(File);
byte[] bytes = new byte[(int)File.length()];
fileInputStreamReader.read(bytes);
ImageString = Base64.getEncoder().encodeToString(bytes);
URL serverUrl = new URL(TARGET_URL + API_KEY); //TARGET_URL = "https://vision.googleapis.com/v1/images:annotate?"
URLConnection urlConnection = serverUrl.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection)urlConnection;
httpConnection.setRequestMethod("POST");
httpConnection.setRequestProperty("Content-Type", "application/json");
httpConnection.setDoOutput(true);
BufferedWriter httpRequestBodyWriter = new BufferedWriter(new
OutputStreamWriter(httpConnection.getOutputStream()));
httpRequestBodyWriter.write
("{\"requests\": [{ \"features\": [ {\"type\": \"FACE_DETECTION\""
+"}], \"image\": {\"content\": \"" + ImageString + "\"}]}");
httpRequestBodyWriter.close();
As you see, I replaced the "content" field by my string. I just don't know what I am doing wrong. Thanks in advance.