Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to reverse your Desktop Image Search with Java

With the program that you will make in this article, 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.

Desktop Reverse Image Search lets you search any person or thing from your album, wallpaper, or even from websites like Facebook.

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.

You can download a working demo of the software from the project homepage at http://sourceforge.net/projects/desktopreversei/

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

Listing 1: Importing classes

/*
 *   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;

Listing 2: Defining classes

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

Listing 3: Defining main method

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.

Listing 4: Defining thread

@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.

Listing 5: Defining showFrame method

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.

Listing 6: Defining methods for mouse

@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

Listing 7: Defining methods draggedScreen

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

Listing 8: Defining upload method

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

Listing 9: Full Source code 1

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);
  }
}

Listing 10: Full Source code 2

/*
 *   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

DesktopReverseImageSearch1
DesktopReverseImageSearch2
DesktopReverseImageSearch3

You can download a working demo of the software from the project homepage at http://sourceforge.net/projects/desktopreversei/



My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login