使用谷歌视觉api阅读图像文本



我正试图使用下面的代码从图像中读取文本,但它的响应是读取400。

我已将图像保存在本地计算机上的c-drive中

我还把钥匙藏在下面的代码里。

下面是我编写的使用视觉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":1n"
            + "        }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;
}

添加此行:

con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");

相关内容

  • 没有找到相关文章

最新更新