


Introduction
Desktop Reverse Image Search lets you search any person or thing from your album, wallpaper, or even from websites like
Facebook.
The steps are very simple:
- Open the software via Jar file (one time process)
- Open the image for which you want to do the search in your local computer or
website
- Press F8 to activate the software
- Drag using the mouse the area of the image for which you want to perform search
- After a small delay, an internet browser will open with your image search
results
- If you want to close the software, just press F9 and the software exits
giving confirmation
Using the code
Remember you need to include these jars in your path
- commons-logging-1.1.1.jar
- httpclient-4.2.jar
- httpcore-4.2.jar
- httpmime-4.2.jar
We include all the necessary imports:
/*
* Copyright [2012] [Anurag Jain]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;
import org.apache.http.HttpResponse;
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.InputStreamBody;
import org.apache.http.impl.client.DefaultHttpClient;
We define the class:
public class ImageResolver extends JFrame implements MouseListener, MouseMotionListener,Runnable
{
int drag_status=0,c1,c2,c3,c4;
static boolean lock=false;
private native int get();
static
{
System . loadLibrary ("ImageResolver");
}
Here:
- We define the variable
drag_status to know when the dragging of image starts
c1, c2, c3, c4 hold the area of the image which has been dragged by the user
lock ensures that the user is able to search one query at a time
- We define a native function
get defined in ImageResolver.dll
public static void main(String args[])
{
try
{
JFrame f1=new JFrame("ImageResolver Help (Exit the App using F9)");
JLabel l1=new JLabel("Please read this before continuing to the program");
JLabel l2=new JLabel("Begin Image Search");
JLabel l3=new JLabel("To begin Image Search,Open the image and then press F8 key");
JLabel l4=new JLabel("A window will open.Just drag the area of the image which you want to search");
JLabel l5=new JLabel("After a small delay the search results will be displayed");
JLabel l6=new JLabel("------------------------------------------------------------------");
JLabel l7=new JLabel("Exit the program");
JLabel l8=new JLabel("For exiting the peogram Press F9.A confirmation message " +
"will be displayed and after that application will close.");
JLabel l9=new JLabel("------------------------------------------------------------------");
JLabel l10=new JLabel("Note: Multiple Searches are not allowed.");
JLabel blank=new JLabel("");
JLabel startinfo7=new JLabel("Software has been Developed by....");
JLabel startinfo8=new JLabel("Anurag Jain");
JLabel startinfo9=new JLabel("Software Engineer");
JLabel startinfo10=new JLabel("(cs.anurag.jain@gmail.com)");
JLabel startinfo11=new JLabel("Project Homepage: https://sourceforge.net/p/desktopreversei/");
JLabel startinfo12=new JLabel("For any problems or feedback " +
"you may contact me directly at cs.anurag.jain@gmail.com");
f1.add(l1);
f1.add(l2);
f1.add(l3);
f1.add(l4);
f1.add(l5);
f1.add(l6);
f1.add(l7);
f1.add(l8);
f1.add(l9);
f1.add(l10);
f1.add(blank);
f1.add(blank);
f1.add(blank);
f1.add(startinfo7);
f1.add(startinfo8);
f1.add(startinfo9);
f1.add(startinfo10);
f1.add(startinfo11);
f1.add(startinfo12);
f1.setLayout(new GridLayout(19,1));
f1.setVisible(true);
f1.setSize(700,700);
f1.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
Thread t1=new Thread(new ImageResolver());
t1.start();
}
catch(Exception e)
{
javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)
null, "Some Problem Occured.Please try again", "Error",
javax.swing.JOptionPane.DEFAULT_OPTION);
}
}
Here:
- We define a frame which shows the help information.
- After that we start the thread which contains the processing part.
@Override
public synchronized void run()
{
while(true)
{
int value = get();
if(value==119 && lock) {
javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)
null, "You can only run one query at a time.Let the previous " +
"search complete then you may continue with the new search", "Error",
javax.swing.JOptionPane.DEFAULT_OPTION);
}
else
if(value==119 && !lock) {
try
{
Thread t2 = new Thread(){
public void run(){
try
{
new ImageResolver().showFrame();
}
catch(Exception e)
{
lock=false;
javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)
null, "Some Problem Occured.Please try again", "Error",
javax.swing.JOptionPane.DEFAULT_OPTION);
}
}
};
t2.start();
}
catch(Exception e)
{
lock=false;
javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)
null, "Some Problem Occured.Please try again", "Error",
javax.swing.JOptionPane.DEFAULT_OPTION);
}
}
else
if(value==120) {
javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)
null, "Exiting...", "Exit",
javax.swing.JOptionPane.DEFAULT_OPTION);
System.exit(0);
}
}
}
Here:
- We use the get method to log the key that has been pressed by the user. When
the user presses F9, the value is 120 and the program exits.
- When the key pressed is F8, value is 119.
- Now we check if there is a lock, then we inform the user that only one search query is permitted.
- Otherwise, we set the lock and call the
showFrame method which is discussed below.
public void showFrame()throws Exception
{
lock=true;
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
Robot robot = new Robot();
BufferedImage img = robot.createScreenCapture(new Rectangle(size));
ImagePanel panel=new ImagePanel(img);
add(panel);
setLocation(0,0);
setSize(size);
setLayout(new FlowLayout());
setUndecorated(true);
setVisible(true);
addMouseListener( this );
addMouseMotionListener( this );
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
Here:
- We make use of the
Robot class so that after a user presses F7, a screenshot of
the desktop is taken and a JFrame opens containing that image.
- We make use of the
ImagePanel class to show the image on the
JFrame.
- We make
setUndecorated true so that there is no title bar.
- We add a mouse listener to know when dragging occurs.
@Override
public void mouseClicked(MouseEvent arg0) {
}
@Override
public void mouseEntered(MouseEvent arg0) {
}
@Override
public void mouseExited(MouseEvent arg0) {
}
@Override
public void mousePressed(MouseEvent arg0) {
repaint();
c1=arg0.getX();
c2=arg0.getY();
}
@Override
public void mouseReleased(MouseEvent arg0) {
repaint();
if(drag_status==1)
{
c3=arg0.getX();
c4=arg0.getY();
try
{
draggedScreen();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
@Override
public void mouseDragged(MouseEvent arg0) {
repaint();
drag_status=1;
c3=arg0.getX();
c4=arg0.getY();
}
@Override
public void mouseMoved(MouseEvent arg0) {
}
public void paint(Graphics g)
{
super.paint(g);
int w = c1 - c3;
int h = c2 - c4;
w = w * -1;
h = h * -1;
if(w<0)
w = w * -1;
g.drawRect(c1, c2, w, h);
}
}
Here:
- We set the coordinates variables when the mouse is dragged using c1, c2, c3, c4
- Finally we get the width and height of the dragged image and we call the
draggedScreen method
public void draggedScreen()throws Exception
{
int w = c1 - c3;
int h = c2 - c4;
w = w * -1;
h = h * -1;
Robot robot = new Robot();
BufferedImage img = robot.createScreenCapture(new Rectangle(c1, c2,w,h));
File save_path=new File("screen1.jpg");
ImageIO.write(img, "JPG", save_path);
dispose();
upload(save_path);
}
Here:
- We obtain the dragged image and save it on the local computer
- Now we call the upload method which uploads the image to Google and gives the results
public void upload(File file) throws Exception
{
final JFrame temp=new JFrame("Searching...");
Thread t2 = new Thread(){
public void run(){
temp.setSize(250,0);
temp.setLayout(new FlowLayout());
temp.setVisible(true);
temp.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
};
t2.start();
MultipartEntity entity = new MultipartEntity();
entity.addPart("encoded_image", new InputStreamBody(new FileInputStream(file), file.getName()));
HttpPost post = new HttpPost("https://www.google.com/searchbyimage/upload");
post.setEntity(entity);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(post);
String site=response.getFirstHeader("location").getValue();
Runtime.getRuntime().exec("cmd /c start "+site);
temp.dispose();
lock=false;
}
Here:
- We open a temp frame which will let the user know that an image has been searched
- Now we submit the image on searchbyimage/upload and get the search result
URL from the location header
- Now we open the browser with that site containing the search result
Full source code
ImagePanel.java
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
class ImagePanel extends JPanel {
private Image img;
public ImagePanel(String img) {
this(new ImageIcon(img).getImage());
}
public ImagePanel(Image img) {
this.img = img;
Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
}
public void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, null);
}
}
ImageResolver.java
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;
import org.apache.http.HttpResponse;
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.InputStreamBody;
import org.apache.http.impl.client.DefaultHttpClient;
public class ImageResolver extends JFrame implements MouseListener, MouseMotionListener,Runnable
{
int drag_status=0,c1,c2,c3,c4;
static boolean lock=false;
private native int get();
static
{
System . loadLibrary ("ImageResolver");
}
public void showFrame()throws Exception
{
lock=true;
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
Robot robot = new Robot();
BufferedImage img = robot.createScreenCapture(new Rectangle(size));
ImagePanel panel=new ImagePanel(img);
add(panel);
setLocation(0,0);
setSize(size);
setLayout(new FlowLayout());
setUndecorated(true);
setVisible(true);
addMouseListener( this );
addMouseMotionListener( this );
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public void upload(File file) throws Exception
{
final JFrame temp=new JFrame("Searching...");
Thread t2 = new Thread(){
public void run(){
temp.setSize(250,0);
temp.setLayout(new FlowLayout());
temp.setVisible(true);
temp.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
};
t2.start();
MultipartEntity entity = new MultipartEntity();
entity.addPart("encoded_image", new InputStreamBody(new FileInputStream(file), file.getName()));
HttpPost post = new HttpPost("https://www.google.com/searchbyimage/upload");
post.setEntity(entity);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(post);
String site=response.getFirstHeader("location").getValue();
Runtime.getRuntime().exec("cmd /c start "+site);
temp.dispose();
lock=false;
}
public void draggedScreen()throws Exception
{
int w = c1 - c3;
int h = c2 - c4;
w = w * -1;
h = h * -1;
Robot robot = new Robot();
BufferedImage img = robot.createScreenCapture(new Rectangle(c1, c2,w,h));
File save_path=new File("screen1.jpg");
ImageIO.write(img, "JPG", save_path);
dispose();
upload(save_path);
}
@Override
public synchronized void run()
{
while(true)
{
int value = get();
if(value==119 && lock) {
javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)
null, "You can only run one query at a time.Let the previous search " +
"complete then you may continue with the new search", "Error",
javax.swing.JOptionPane.DEFAULT_OPTION);
}
else
if(value==119 && !lock) {
try
{
Thread t2 = new Thread(){
public void run(){
try
{
new ImageResolver().showFrame();
}
catch(Exception e)
{
lock=false;
javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)
null, "Some Problem Occured.Please try again", "Error",
javax.swing.JOptionPane.DEFAULT_OPTION);
}
}
};
t2.start();
}
catch(Exception e)
{
lock=false;
javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)
null, "Some Problem Occured.Please try again", "Error",
javax.swing.JOptionPane.DEFAULT_OPTION);
}
}
else
if(value==120) {
javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)
null, "Exiting...", "Exit",
javax.swing.JOptionPane.DEFAULT_OPTION);
System.exit(0);
}
}
}
public static void main(String args[])
{
try
{
JFrame f1=new JFrame("ImageResolver Help (Exit the App using F9)");
JLabel l1=new JLabel("Please read this before continuing to the program");
JLabel l2=new JLabel("Begin Image Search");
JLabel l3=new JLabel("To begin Image Search,Open the image and then press F8 key");
JLabel l4=new JLabel("A window will open.Just drag the area of the image which you want to search");
JLabel l5=new JLabel("After a small delay the search results will be displayed");
JLabel l6=new JLabel("------------------------------------------------------------------");
JLabel l7=new JLabel("Exit the program");
JLabel l8=new JLabel("For exiting the peogram Press F9.A confirmation message " +
"will be displayed and after that application will close.");
JLabel l9=new JLabel("------------------------------------------------------------------");
JLabel l10=new JLabel("Note: Multiple Searches are not allowed.");
JLabel blank=new JLabel("");
JLabel startinfo7=new JLabel("Software has been Developed by....");
JLabel startinfo8=new JLabel("Anurag Jain");
JLabel startinfo9=new JLabel("Software Engineer");
JLabel startinfo10=new JLabel("(cs.anurag.jain@gmail.com)");
JLabel startinfo11=new JLabel("Project Homepage: https://sourceforge.net/p/desktopreversei/");
JLabel startinfo12=new JLabel("For any problems or feedback " +
"you may contact me directly at cs.anurag.jain@gmail.com");
f1.add(l1);
f1.add(l2);
f1.add(l3);
f1.add(l4);
f1.add(l5);
f1.add(l6);
f1.add(l7);
f1.add(l8);
f1.add(l9);
f1.add(l10);
f1.add(blank);
f1.add(blank);
f1.add(blank);
f1.add(startinfo7);
f1.add(startinfo8);
f1.add(startinfo9);
f1.add(startinfo10);
f1.add(startinfo11);
f1.add(startinfo12);
f1.setLayout(new GridLayout(19,1));
f1.setVisible(true);
f1.setSize(700,700);
f1.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
Thread t1=new Thread(new ImageResolver());
t1.start();
}
catch(Exception e)
{
javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)
null, "Some Problem Occured.Please try again", "Error",
javax.swing.JOptionPane.DEFAULT_OPTION);
}
}
@Override
public void mouseClicked(MouseEvent arg0) {
}
@Override
public void mouseEntered(MouseEvent arg0) {
}
@Override
public void mouseExited(MouseEvent arg0) {
}
@Override
public void mousePressed(MouseEvent arg0) {
repaint();
c1=arg0.getX();
c2=arg0.getY();
}
@Override
public void mouseReleased(MouseEvent arg0) {
repaint();
if(drag_status==1)
{
c3=arg0.getX();
c4=arg0.getY();
try
{
draggedScreen();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
@Override
public void mouseDragged(MouseEvent arg0) {
repaint();
drag_status=1;
c3=arg0.getX();
c4=arg0.getY();
}
@Override
public void mouseMoved(MouseEvent arg0) {
}
public void paint(Graphics g)
{
super.paint(g);
int w = c1 - c3;
int h = c2 - c4;
w = w * -1;
h = h * -1;
if(w<0)
w = w * -1;
g.drawRect(c1, c2, w, h);
}
}
Output:


Points of Interest
With this software you can search any possible image by simply dragging that image with mouse and the software opens all relevant results making use
of Google Image. It will come under reverse image search.
I am a Java software developer. I like to make new software which can be really helpful to people. You may get in touch with me at http://www.martcode.com