Tag Archives: google

Google Reverse Image Upload and Search Using Java Desktop


Google Reverse Image Upload and Search Using Java Desktop

Cross-platform compatibility like Windows, Linux, Unix and Mac too

google image reverse search
Google Image reverse search

Google.java

import java.awt.Component;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.JOptionPane;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;

@SuppressWarnings("deprecation")
public class Google {

	/**
	 * @param args
	 */
		private static String OS = System.getProperty("os.name").toLowerCase();

		public static void main(String[] args) {

			Component frame = null;

			String exePath="ned.jpg";
			String imageFile = exePath;
			File exeFile=new File(imageFile);

			// TODO Auto-generated method stub
			try {
				@SuppressWarnings("resource")
				HttpClient client = new DefaultHttpClient();
				String url = "https://www.google.com/searchbyimage/upload";

				if (isWindows()) {

					exeFile.getAbsoluteFile();
				}
				else if (isMac()) {
					exeFile.getAbsoluteFile();
				} else if (isUnix()) {
					exeFile.getAbsoluteFile();
				}
				HttpPost post = new HttpPost(url);
				Runtime rt = Runtime.getRuntime();

				MultipartEntity entity = new MultipartEntity();
				entity.addPart("encoded_image", new FileBody(new File(imageFile)));
				entity.addPart("image_url", new StringBody(""));
				entity.addPart("image_content", new StringBody(""));
				entity.addPart("filename", new StringBody(""));
				entity.addPart("h1", new StringBody("en"));
				entity.addPart("bih", new StringBody("179"));
				entity.addPart("biw", new StringBody("1600"));

				post.setEntity(entity);
				HttpResponse response = client.execute(post);
				BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

				String line = "";
				String someStr = "";
				while ((line = rd.readLine()) != null) {
					if (line.indexOf("HREF") > 0)
						try {
							someStr = line.substring(9,line.lastIndexOf("\">here</A>."))
									+ "";
							if (isWindows()) {

								// this doesn't support showing URL in the form of page.html#nameLink"
								System.out.println(OS);
								rt.exec("rundll32 url.dll,FileProtocolHandler "+ someStr);

							} else if (isMac()) {
								System.out.println(OS);
								rt.exec("open " + someStr);

							} else if (isUnix()) {
								// Do a best guess on Unix until we get a platform independent way
								// Build a list of browsers to try, in this order.
								String[] browsers = { "epiphany", "firefox","mozilla", "konqueror", "netscape","opera", "links", "lynx" };

								// Build a command string which looks like browser1 "URL" || browser2 "URL" ||..."
								StringBuffer cmd = new StringBuffer();
								for (int i = 0; i < browsers.length; i++)
									cmd.append((i == 0 ? "" : " || ") + browsers[i]+ " \"" + someStr + "\" ");

								rt.exec(new String[] { "sh", "-c", cmd.toString() });
								System.out.println(OS);
							} else {
								return;
							}
						} catch (Exception e) {
							System.out.println("Ponka");
							JOptionPane.showMessageDialog(frame,"Wrong Operating System " + e + OS,"Inane error", JOptionPane.ERROR_MESSAGE);
						}

				}

			} catch (ClientProtocolException cpx) {

				JOptionPane.showMessageDialog(frame, "Error " + cpx, "Inane error",JOptionPane.ERROR_MESSAGE);
				cpx.printStackTrace();

			} catch (IOException ioex) {
				JOptionPane.showMessageDialog(frame, "File not Found at " + ioex,"Inane error", JOptionPane.ERROR_MESSAGE);
				ioex.printStackTrace();
			}
		}

		public static boolean isWindows() {

			return (OS.indexOf("win") >= 0);

		}

		public static boolean isMac() {

			return (OS.indexOf("mac") >= 0);

		}

		public static boolean isUnix() {

			return (OS.indexOf("nix") >= 0 || OS.indexOf("nux") >= 0 || OS.indexOf("aix") > 0);

		}

		public static boolean isSolaris() {

			return (OS.indexOf("sunos") >= 0);

		}

}

click-here

click-here-jar

Advertisements