Click here to Skip to main content
Click here to Skip to main content

Desktop Reverse Image Search

By , 25 Jun 2012
 

Sample Image

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:

  1. Open the software via Jar file (one time process)
  2. Open the image for which you want to do the search in your local computer or website
  3. Press F8 to activate the software
  4. Drag using the mouse the area of the image for which you want to perform search
  5. After a small delay, an internet browser will open with your image search results
  6. 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

  1. commons-logging-1.1.1.jar
  2. httpclient-4.2.jar
  3. httpcore-4.2.jar
  4. 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:

  1. We define the variable drag_status to know when the dragging of image starts
  2. c1, c2, c3, c4 hold the area of the image which has been dragged by the user
  3. lock ensures that the user is able to search one query at a time
  4. 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:

  1. We define a frame which shows the help information.
  2. 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)//on F8
    {
         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)//on F8
    {
        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)//on F9
    {
         javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)
                 null, "Exiting...", "Exit",
                 javax.swing.JOptionPane.DEFAULT_OPTION);
        System.exit(0);
    }
    
}
}

Here:

  1. 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.
  2. When the key pressed is F8, value is 119.
  3. Now we check if there is a lock, then we inform the user that only one search query is permitted.
  4. 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:

  1. 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.
  2. We make use of the ImagePanel class to show the image on the JFrame.
  3. We make setUndecorated true so that there is no title bar.
  4. We add a mouse listener to know when dragging occurs.
@Override
public void mouseClicked(MouseEvent arg0) {
    // TODO Auto-generated method stub
    
}

@Override
public void mouseEntered(MouseEvent arg0) {
    // TODO Auto-generated method stub
    
}

@Override
public void mouseExited(MouseEvent arg0) {
    // TODO Auto-generated method stub
    
}

@Override
public void mousePressed(MouseEvent arg0) {
    // TODO Auto-generated method stub
    repaint();
    c1=arg0.getX();
    c2=arg0.getY();
}

@Override
public void mouseReleased(MouseEvent arg0) {
    // TODO Auto-generated method stub
    repaint();
    if(drag_status==1)
    {
    c3=arg0.getX();
    c4=arg0.getY();
    try
    {
    draggedScreen();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    }
}

@Override
public void mouseDragged(MouseEvent arg0) {
    // TODO Auto-generated method stub
    repaint();
    drag_status=1;
    c3=arg0.getX();
    c4=arg0.getY();
}

@Override
public void mouseMoved(MouseEvent arg0) {
    // TODO Auto-generated method stub
    
}

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:

  1. We set the coordinates variables when the mouse is dragged using c1, c2, c3, c4
  2. 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:

  1. We obtain the dragged image and save it on the local computer
  2. 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:

  1. We open a temp frame which will let the user know that an image has been searched
  2. Now we submit the image on searchbyimage/upload and get the search result URL from the location header
  3. 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));
   // Dimension size = new Dimension(10,10);
    setPreferredSize(size);
    setMinimumSize(size);
    setMaximumSize(size);
    setSize(size);
    setLayout(null);
  }

  public void paintComponent(Graphics g) {
    g.drawImage(img, 0, 0, null);
  }
}
ImageResolver.java
/*
 *   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;


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)//on F8
    {
         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)//on F8
    {
        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)//on F9
    {
         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) {
    // TODO Auto-generated method stub
    
}

@Override
public void mouseEntered(MouseEvent arg0) {
    // TODO Auto-generated method stub
    
}

@Override
public void mouseExited(MouseEvent arg0) {
    // TODO Auto-generated method stub
    
}

@Override
public void mousePressed(MouseEvent arg0) {
    // TODO Auto-generated method stub
    repaint();
    c1=arg0.getX();
    c2=arg0.getY();
}

@Override
public void mouseReleased(MouseEvent arg0) {
    // TODO Auto-generated method stub
    repaint();
    if(drag_status==1)
    {
    c3=arg0.getX();
    c4=arg0.getY();
    try
    {
    draggedScreen();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    }
}

@Override
public void mouseDragged(MouseEvent arg0) {
    // TODO Auto-generated method stub
    repaint();
    drag_status=1;
    c3=arg0.getX();
    c4=arg0.getY();
}

@Override
public void mouseMoved(MouseEvent arg0) {
    // TODO Auto-generated method stub
    
}

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

csanuragjain
Software Developer
India India
Member
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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 25 Jun 2012
Article Copyright 2012 by csanuragjain
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid