2016年3月9日 星期三

Image search API - url direct link method

http://stackoverflow.com/questions/15283439/google-similar-image-search-api

http://stackoverflow.com/questions/14927344/google-image-search-via-upload/15134958#15134958

http://a6350202.pixnet.net/blog/post/148400470-%E3%80%90java%E3%80%91%E4%BD%BF%E7%94%A8jsoup%E8%AE%93java%E5%8F%96%E5%BE%97%E7%B6%B2%E9%A0%81%E4%B8%8A%E7%9A%84%E6%96%87%E5%AD%97(%E6%96%87%E6%9C%AB

AsyncTask
http://aiur3908.blogspot.tw/2015/06/android-asynctask.html

2016/3/9 完成


  1. 將圖片縮小(640*480),再轉成Base64 (解決OOM問題)
  2. 利用Imgur API 把圖片傳上Imgur,獲得一個直接連結 (http://stackoverflow.com/questions/17352866/imgur-api-uploading)
  3. 用JSOUP把圖片搜尋連結+剛剛的圖片傳上去,並下載HTML
  4. 擷取HTML中 </a> 中包含 _gUb字串的內容
  5. 即為google 圖片搜尋的預測結果

//利用 POST 去對Imgur API 進行傳送

 public static String getImgurContent(String clientID) throws Exception {
    URL url;
    url = new URL("https://api.imgur.com/3/image");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    String data = URLEncoder.encode("image", "UTF-8") + "="
            + URLEncoder.encode(IMAGE_URL, "UTF-8");

    conn.setDoOutput(true);
    conn.setDoInput(true);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Authorization", "Client-ID " + clientID);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type",
            "application/x-www-form-urlencoded");

    conn.connect();
    StringBuilder stb = new StringBuilder();
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write(data);
    wr.flush();

    // Get the response
    BufferedReader rd = new BufferedReader(
            new InputStreamReader(conn.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {
        stb.append(line).append("\n");
    }
    wr.close();
    rd.close();

    return stb.toString();
}
https://api.imgur.com/

沒有留言:

張貼留言