简体   繁体   English

如何使用谷歌API从谷歌图像保存图像?

[英]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.

相关问题 Google条码API,保存图像 - Google Barcode API, Save image 如何获取使用Google API获得的每条新闻的图像 - How do I get the images of every news that I get using Google api 使用Java从Google Image Search下载图像 - Downloading images from Google Image search using Java 如何从Android上传文件(图片)并使用GAE和Google端点保存到Blobstore? - How do I upload a file(picture) from android and save to the blobstore using GAE and google endpoints? 如何下载Google Maps卫星图像并保存与该图像相关的坐标信息? - How to download Google Maps satellite images and save coordinates information related to the image? 如何使用Java中的改造从Google附近的搜索API检索图像网址 - How to retrieve an image url from Google nearby search API using retrofit in java 如何使用 Google Cloud DNS API 从项目中获取所有“managerzone”或从“managerzone”中获取“记录”? - How do I get all the 'managerzone' from the project or 'record' from the 'managerzone' using the Google Cloud DNS API? 使用Google App Engine图像Java API减小图像大小失败 - fail reducing Image size using google app engine images java API 如何使用objectify在Google数据存储区中正确保存Google用户? - How do I correctly save a google user in google datastore using objectify? 如何使用Google API Java客户端对联系人进行部分更新? - How to do a partial update on contacts using Google API Java client?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM