how do save image from google images using google API?

守給你的承諾、 提交于 2019-12-13 16:07:35

问题


i'm trying to search in Google-images some different and save the first result for every query with 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

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)

10x


回答1:


JSON interface is described at JSON Developer's Guide. In particular, JSON reference section outlines response format and guaranteed fields. You can use a value of url property.

Given the URL, you can read the image and write it to the disk using 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.

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();
        }
    }
}


来源:https://stackoverflow.com/questions/11485578/how-do-save-image-from-google-images-using-google-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!