java 如何使用谷歌 API 从谷歌图像中保存图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11485578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how do save image from google images using google API?
提问by user1383845
i'm trying to search in Google-images some different and save the first result for every query with java Google API.
我正在尝试在 Google 图像中搜索一些不同的图像,并使用 java Google API 保存每个查询的第一个结果。
I managed to search in Google and get the json object which contains the search results. the object contains the web sites which contains the images,and not the image address
我设法在 Google 中搜索并获得包含搜索结果的 json 对象。该对象包含包含图像的网站,而不是图像地址
code:
代码:
URL url = new URL("https://ajax.googleapis.com/ajax/services/search/images?" +
"v=1.0&q="+properties.getProperty(Integer.toString(i))+"&userip=IP");
URLConnection connection = url.openConnection();
connection.addRequestProperty("Referer", "images.google.com");
String line;
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while((line = reader.readLine()) != null) {
builder.append(line);
}
JSONObject json = new JSONObject(builder.toString())
I'm also know how to save image if i had the image link.
如果我有图片链接,我也知道如何保存图片。
my problem is how to get the first (or second or whatever) image right address and not the web address (example www.yadayadayada.com/image.png)
我的问题是如何获得第一个(或第二个或其他)图像正确地址而不是网址(例如 www.yadayadayada.com/image.png)
10x
10倍
回答by tenorsax
JSON interface is described at JSON Developer's Guide. In particular, JSON referencesection outlines response format and guaranteed fields. You can use a value of url
property.
JSON 接口在JSON Developer's Guide 中有描述。特别是,JSON 参考部分概述了响应格式和保证字段。您可以使用url
属性值。
Given the URL, you can read the image and write it to the disk using ImageIO. Here is the relevant tutorial.
给定 URL,您可以使用ImageIO读取图像并将其写入磁盘。这是相关教程。
If the image manipulation and presentation is not required, then you could use HttpURLConnectionto simply download the file.
如果不需要图像处理和演示,那么您可以使用HttpURLConnection来简单地下载文件。
EDIT: example
编辑:示例
Below is a simple example based on the code included in the question. It performs a search and displays the first image.
下面是一个基于问题中包含的代码的简单示例。它执行搜索并显示第一张图像。
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class TestImage {
public static void main(String[] args) {
try{
URL url = new URL("https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=Godfather");
URLConnection connection = url.openConnection();
String line;
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while((line = reader.readLine()) != null) {
builder.append(line);
}
JSONObject json = new JSONObject(builder.toString());
String imageUrl = json.getJSONObject("responseData").getJSONArray("results").getJSONObject(0).getString("url");
BufferedImage image = ImageIO.read(new URL(imageUrl));
JOptionPane.showMessageDialog(null, "", "", JOptionPane.INFORMATION_MESSAGE, new ImageIcon(image));
} catch(Exception e){
JOptionPane.showMessageDialog(null, e.getMessage(), "Failure", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
}
}