【问题标题】:reading image text using google vision api使用 google vision api 读取图像文本
【发布时间】:2017-04-28 19:52:35
【问题描述】:

我正在尝试使用以下代码从图像中读取文本,但它正在读取 400 作为响应。

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

我在下面的代码中也隐藏了密钥。

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

【问题讨论】:

    标签: java google-api


    【解决方案1】:

    添加这一行:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-24
      • 2019-05-25
      • 2019-10-06
      • 2019-02-05
      • 2017-02-16
      • 2021-12-08
      • 2020-03-17
      • 2019-04-08
      相关资源
      最近更新 更多