[Java] [Java] Get images with Google Custom Search API

2 minute read

Things necessary

・ API key
・ Search engine ID
Both can be obtained in the process of activating the Google Custom Search API.

Reference site

This article was helpful for getting API key and search engine ID.
Get Google search results using Custom Search API

Click here for image search.
Image collection using Google Custom Search API

Source code

Download 10 images from the search results.
The save destination is specified in the download folder.

CustomSearch.java


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class CustomSearch {

	public static void main(String[] args) throws IOException {
		String key = "API key";
		String cx = "Search engine ID";
		String qry = "The character you want to search";
		String link = null;
		List<String> linkList = new ArrayList<String>();

		//Make a request to the Google Custom Search API
		URL url = new URL(
				"https://www.googleapis.com/customsearch/v1?key=" + key + "&cx=" + cx + "&q=" + qry
						+ "&searchType=image");
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setRequestMethod("GET");
		conn.setRequestProperty("Accept", "application/json");
		BufferedReader br = new BufferedReader(new InputStreamReader(
				(conn.getInputStream())));

		//The response is json
		//Process to find URL from json
		String output;
		System.out.println("Output from Server .... \n");
		while ((output = br.readLine()) != null) {

			if (output.contains("\"link\": \"")) {

				link = output.substring(output.indexOf("\"link\": \"") + ("\"link\": \"").length(),
						output.indexOf("\","));
				System.out.println(link);
				linkList.add(link);
			}
		}
		conn.disconnect();

	    //The process of downloading an image from a URL.
		URL imageURL = null;
		HttpURLConnection urlConnection = null;

		for (int i = 0; i < 10; i++) {

			try {
				imageURL = new URL(linkList.get(i));

				urlConnection = (HttpURLConnection) imageURL.openConnection();
				//If true, allow redirects
				urlConnection.setInstanceFollowRedirects(true);
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

			//InputStream is a superclass of all classes that represent a byte input stream.
			InputStream in = null;

			try {
				in = urlConnection.getInputStream();
			} catch (IOException e) {
				e.printStackTrace();
			}

			byte[] buf = new byte[4096];
			int readSize;
			int total = 0;
			try {

				FileOutputStream fos = new FileOutputStream("C:\\Users\\user\\Downloads\\" + "test" + i + ".jpg ");

				//The read method is a value if there are no bytes to read because the stream has reached the end of the file-1 is returned.
				//The first byte read is element b[0]Stored in, the next byte is b[1]It is stored in, and so on.
				//The maximum number of bytes that can be read is the same as the length of b.
				while (((readSize = in.read(buf)) != -1)) {
					total = total + readSize;

					//Writes readSize bytes starting at offset position 0 of the specified byte array to this file output stream.
					fos.write(buf, 0, readSize);
				}
				fos.flush();
				fos.close();
				in.close();
			} catch (FileNotFoundException e) {
				System.out.println("File error");
			} catch (IOException e) {
				e.printStackTrace();
			}

			System.out.println("Size:" + total);
		}
	}
}

How it works

Throw the search character as a request to the API.
A response is returned with json data.
Find the URL of the image from the json data.
Then connect to the URL and download!
only this!