初試Google和Flickr圖片源

來源:互聯網
上載者:User
package com.jadyer.util;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.MalformedURLException;import java.net.URL;import java.util.ArrayList;import java.util.List;/** * @see ================================================================================================================ * @see 找到Google Image Search API頁面 * @see 1、進入'http://code.google.com'網站,點擊左側AJAX APIs後,會轉向到'https://developers.google.com/loader/?hl=zh-CN' * @see 2、展開左側APIs Using the Loader,點擊Image Search,跳轉到'https://developers.google.com/image-search/?hl=zh-cn' * @see 3、點擊頁面中的developer documentation連結,跳轉到'https://developers.google.com/image-search/v1/index?hl=zh-cn' * @see 4、點擊頁面中的JSON Developer's Guide連結,跳轉到'https://developers.google.com/image-search/v1/jsondevguide?hl=zh-cn' * @see ================================================================================================================ * @see 解析Google返回的json字串的方法 * @see Google所返回的json字串中,會直接把圖片的URL以'url'參數加入到json中返回給我們 * @see 所以我們直接解析其返回的json字串中的'url'參數值,即可。下面是Google返回的json字串的樣本格式 * @see {"responseData": {"results":[{"GsearchResultClass":"GimageSearch","width":"220","height":"204", * @see "imageId":"ANd9GcSvKWlLJwv3gayNuFIpl0eCPwIcdEwUzdFo-HsTZv-z1l5JL7WYMhacZg","tbWidth":"107","tbHeight":"99", * @see "unescapedUrl":"http://upload.wikimedia.org/wikipedia/commons/thumb/0/0e/IBM_Thinkpad_R51.jpg/220px-IBM_Thinkpad_R51.jpg", * @see "url":"http://upload.wikimedia.org/wikipedia/commons/thumb/0/0e/IBM_Thinkpad_R51.jpg/220px-IBM_Thinkpad_R51.jpg", * @see "visibleUrl":"en.wikipedia.org","title":"\u003cb\u003eThinkPad\u003c/b\u003e - Wikipedia, the free encyclopedia", * @see "titleNoFormatting":"ThinkPad - Wikipedia, the free encyclopedia","originalContextUrl":"http://en.wikipedia.org/wiki/ThinkPad", * @see "content":"IBM \u003cb\u003eThinkPad\u003c/b\u003e R51","contentNoFormatting":"IBM ThinkPad R51", * @see "tbUrl":"http://t3.gstatic.com/images?q\u003dtbn:ANd9GcSvKWlLJwv3gayNuFIpl0eCPwIcdEwUzdFo-HsTZv-z1l5JL7WYMhacZg"}, * @see {......}, * @see "responseDetails": null, "responseStatus": 200} * @see 所以我們得到的圖片URL就是第27行'url'參數的值http://upload.wikimedia.org/wikipedia/commons/thumb/0/0e/IBM_Thinkpad_R51.jpg/220px-IBM_Thinkpad_R51.jpg * @see ================================================================================================================ * @see 找到flickr.photos.search API頁面 * @see 1、點擊Flickr首頁'http://www.flickr.com'底部的'API說明檔案'連結,會轉向到'http://www.flickr.com/services/api/' * @see 2、點擊右側photos下的'flickr.photos.search'連結,跳轉到'http://www.flickr.com/services/api/flickr.photos.search.html' * @see 3、另外還有一個'http://kylerush.net/blog/flickr-api/'是我在Google上搜尋到的,在這裡可以找到Flickr的圖片伺服器的URL * @see 4、如果想查看Flickr返回的json格式,可以點擊'API說明檔案'頁面左側'回應格式'下的'JSON'連結 * @see   其具體頁面為'http://www.flickr.com/services/api/response.json.html' * @see ================================================================================================================  * @see 解析Flickr返回的json字串的方法 * @see 0、Flickr與Google不同,Google會直接在返回的json中加入圖片的URL,而Flickr需要我們解析其返回的json後,再拼裝出來圖片的URL * @see 1、點擊Flickr首頁'http://www.flickr.com'底部的'API說明檔案'連結,會轉向到'http://www.flickr.com/services/api/' * @see 2、然後點擊該頁面左側的'URL'連結,會跳轉到'http://www.flickr.com/services/api/misc.urls.html' * @see 3、閱讀該頁面得知Flickr的圖片URL需要自行拼接,如http://farm{farm-id}.staticflickr.com/{server-id}/{id}_{secret}.jpg * @see 4、以下是Flickr返回的json字串的樣本格式 * @see   jsonFlickrApi({"photos":{"page":1, "pages":429, "perpage":300, "total":"128631", * @see   "photo":[{"id":"6845488017", "owner":"71906482@N04", "secret":"0f4c482ced", "server":"7007", "farm":8, * @see             "title":"Mini-Mini Schnauzers", "ispublic":1, "isfriend":0, "isfamily":0}, * @see             {......}]}, * @see             "stat":"ok"}) * @see 5、那麼根據這個json字串所拼接出來的圖片URL就是'http://farm8.staticflickr.com/7007/6845488017_0f4c482ced.jpg' * @see ================================================================================================================ * @author 宏宇 * @create Feb 9, 2012 6:25:38 PM */public class ImageSearchAPIsUtil {public static void main(String[] args){//String googleBaseUrl = "https://ajax.googleapis.com/ajax/services/search/images";//String googleImgSize = "large";//String googleSearchContent = "thinkpad";//getGoogleImage(googleBaseUrl, googleImgSize, googleSearchContent);String flickrBaseUrl = "http://api.flickr.com/services/rest/";String flickrMethod = "flickr.photos.search";String flickrAPIKey = "da8e8c6840579831969d1a87d37ae545";String flickrPerPage = "200";  //設定每一頁返回的圖片數量。其預設值是100,最大值是500String flickrFormat = "json";  //指定其以json格式返回資料String flickrTags = "macbook"; //所查詢的圖片的關鍵字getFlickrImage(flickrBaseUrl, flickrMethod, flickrAPIKey, flickrPerPage, flickrFormat, flickrTags);}/** * 擷取Google圖片源 * @param googleBaseUrl       Google圖片伺服器URL * @param googleImgSize       所要顯示的圖片大小 * @param googleSearchContent 所要查詢的關鍵字 * @see 對於同一個關鍵字,Google僅會返回64張圖片,並且每次最多返回8張圖片 * @see 於是我們可以迴圈8次,來擷取這64張圖片,然後解析這64張圖片的URL地址,再將這些URL以響應的方式發送給用戶端 */public static void getGoogleImage(String googleBaseUrl, String googleImgSize, String googleSearchContent){List<String> list = new ArrayList<String>();for(int i=0; i<8; i++){String url = googleBaseUrl + "?q=" + googleSearchContent + "&rsz=" + googleImgSize + "&v=1.0&";url += "start=" + 8*i;StringBuffer sb = new StringBuffer();InputStream is = null;InputStreamReader isr = null;BufferedReader br = null;try{is = new URL(url).openConnection().getInputStream(); //獲得輸入資料流isr = new InputStreamReader(is, "UTF-8");            //位元組流到字元流的轉換br = new BufferedReader(isr);String line = null;while(null != (line=br.readLine())){sb.append(line);}list.add(sb.toString());} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{if(null != br){try {br.close();} catch (IOException e) {e.printStackTrace();}}if(null != isr){try {isr.close();} catch (IOException e) {e.printStackTrace();}}if(null != is){try {is.close();} catch (IOException e) {e.printStackTrace();}}}}System.out.println("Google返回的圖片頁數:" + list.size());for(String s : list){System.out.println(s);}}/** * 擷取Flickr圖片源 * @param flickrBaseUrl Flickr圖片伺服器URL * @param flickrMethod  Flickr提供的圖片搜尋方法 * @param flickrAPIKey  Flickr提供的免費的API_KEY * @param flickrPerPage 設定每一頁返回的圖片數量 * @param flickrFormat  指定其返回資料的格式 * @param flickrTags    查詢的圖片的關鍵字 * @see 我申請的雅虎郵箱地址是mytestflickr32@yahoo.com密碼是uygnoh * @see 我申請的Flickr API Key為da8e8c6840579831969d1a87d37ae545,其密碼是243a1b4492339c84 */public static void getFlickrImage(String flickrBaseUrl, String flickrMethod, String flickrAPIKey, String flickrPerPage, String flickrFormat, String flickrTags){String url = flickrBaseUrl + "?method=" + flickrMethod + "&api_key=" + flickrAPIKey + "&per_page=" + flickrPerPage + "&format=" + flickrFormat + "&tags=" + flickrTags + "&extras=original_format";StringBuffer sb = new StringBuffer();InputStream is = null;InputStreamReader isr = null;BufferedReader br = null;try{is = new URL(url).openConnection().getInputStream();isr = new InputStreamReader(is, "UTF-8");br = new BufferedReader(isr);String line = null;while(null != (line=br.readLine())){sb.append(line);}System.out.println(sb);} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{if(null != br){try {br.close();} catch (IOException e) {e.printStackTrace();}}if(null != isr){try {isr.close();} catch (IOException e) {e.printStackTrace();}}if(null != is){try {is.close();} catch (IOException e) {e.printStackTrace();}}}}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.