我正在使用Google Vision API制作一个小项目。我想检测在POST请求中发送到API的base64编码图像的面。我的代码基于Google的这个教程:https://cloud.google.com/community/tutorials/make-an-http-request-to-the-cloud-vision-api-from-java。显然可以发送base64编码图像。
这是我的代码:
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();
如您所见,我用字符串替换了“content”字段。我只是不知道我做错了什么。 提前致谢。