我正在尝试使用以下代码从图像中读取文本,但它的响应为400.
我已将图像保存在本地计算机上的c-drive中
我也将密钥隐藏在下面的代码中。
下面是我使用vision api编写的函数
private static String postingtogoogle() throws Exception {
// Base64.encode;
String re = "";
String url = "https://vision.googleapis.com/v1/images:annotate?key=mykey";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
BufferedImage img = ImageIO.read(new File("C:/a.png"));
String imgstr = encodeToString(img, "png");
imgstr = encodeToString(img, "png");
//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
String urlParameters = "{\n"
+ " \"requests\":[\n"
+ " {\n"
+ " \"image\":{\n"
+ " \"content\":\"" + imgstr + "\"\n"
+ " },\n"
+ " \"features\":[\n"
+ " {\n"
+ " \"type\":\"LABEL_DETECTION\",\n"
+ " \"maxResults\":1\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ " ]\n"
+ "}";
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
re = response.toString();
return re;
}
答案 0 :(得分:0)
添加以下行:
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");