2

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

Kara
  • 6,115
  • 16
  • 50
  • 57
user1383845
  • 183
  • 1
  • 2
  • 8

1 Answers1

10

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();
        }
    }
}
tenorsax
  • 21,123
  • 9
  • 60
  • 107
  • Hey, thank you for your answer but my problem is I get in the url fields the sites which contains the image and not the link to the image itself. example: if i search The Godfather i get http://www.imdb.com/title/tt0068646/ – user1383845 Jul 15 '12 at 20:08
  • @user1383845 please see if my edit can help you. I included a tiny sample that makes a search and displays the first image. – tenorsax Jul 16 '12 at 00:09
  • google search has been deprecated, is there any alternative to that ? thanks. – Majid Laissi Apr 17 '13 at 15:03
  • @MajidL There is [Custom Search API](https://developers.google.com/custom-search/v1/overview) that supports image search. – tenorsax Apr 17 '13 at 15:20
  • 1
    @Aqua thank you. I came across this library but I thought it was used to search in a list of specific websites using a custom search engine, is it possible not to restrict the search ? – Majid Laissi Apr 17 '13 at 16:01
  • @MajidL Hmm, true. But looks like there is a trick to enable *Google Custom Search* to search the entire web. See this [thread](http://stackoverflow.com/a/11206266/1048330). – tenorsax Apr 17 '13 at 16:41
  • 1
    @Aqua Thank you very much. I managed to add wildcard domains like `*.com` and `*.org` etc... That shoud do it for now. – Majid Laissi Apr 17 '13 at 23:05
  • 1
    @Aqua Thank you. I came across your solution and it helps me a lot. However, I tried to modify the code to try and print out every image's url returned by the search but it seems that it will only return 3 urls at most. Why is that so? – jl90 Sep 04 '14 at 05:57
  • @jl90 you're welcome! :) may be it is due to the [deprecation](https://developers.google.com/image-search/) of this API, see comments above for more details. – tenorsax Sep 04 '14 at 22:07
  • @Aqua Do you have a working Java version that involves the new API? – Mike Warren Oct 16 '14 at 05:29
  • @MikeWarren unfortunately no, see if [this](http://stackoverflow.com/q/10257276/1048330) is helpful, also explore [google-custom-search](http://stackoverflow.com/questions/tagged/google-custom-search) tag. – tenorsax Oct 17 '14 at 21:41
  • Did you see my comment on there? – Mike Warren Oct 17 '14 at 22:07
  • @MikeWarren There are limitations, see [pricing](https://developers.google.com/custom-search/json-api/v1/overview#Pricing) for details. – tenorsax Oct 17 '14 at 22:16
  • Also, given what I am trying to do, which is outlined here: http://stackoverflow.com/questions/26393873/searching-google-images-from-java , what type of API key would I have to obtain??? – Mike Warren Oct 17 '14 at 22:19
  • Can anyone tell me where is JSONObject class – bit-shashank Apr 25 '18 at 11:37