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,最大值是500 String 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 我申请的雅虎邮箱地址是[email protected]密码是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(); } } } } }