[英]how do save image from google images using google API?
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. 我设法在Google中搜索并获取包含搜索结果的json对象。 the object contains the web sites which contains the images,and not the image address
该对象包含包含图像的网站,而不包含图像地址
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倍
JSON interface is described at JSON Developer's Guide . JSON 开发人员指南中描述了JSON接口。 In particular, JSON reference section outlines response format and guaranteed fields.
特别是, JSON参考部分概述了响应格式和保证字段。 You can use a value of
url
property. 您可以使用
url
属性的值。
Given the URL, you can read the image and write it to the disk using ImageIO . 给定URL,您可以使用ImageIO读取映像并将其写入磁盘。 Here is the relevant tutorial .
这是相关的教程 。
If the image manipulation and presentation is not required, then you could use HttpURLConnection to 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();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.