14 Replies - 2267 Views - Last Post: 25 August 2010 - 08:53 AM Rate Topic: -----

#1 Guest_nub*


Reputation:

Check if desktop contains certain image

Posted 24 August 2010 - 12:22 PM

I am trying to create a boolean method, weather or not the desktop contains certain pixels.

For example I would like it to check if the desktop contains this image:
Posted Image

So when I go to google.com it returns true.

I tried making an array of the pixels of each and comparing them. That did not work. Any other ideas?

Is This A Good Question/Topic? 0

Replies To: Check if desktop contains certain image

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10392
  • View blog
  • Posts: 38,458
  • Joined: 27-December 08

Re: Check if desktop contains certain image

Posted 24 August 2010 - 01:42 PM

You may find the PixelGrabber class to be helpful. As for comparing images, are you looking at image files or the desktop background? Can you post your code as well?
Was This Post Helpful? 0
  • +
  • -

#3 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Check if desktop contains certain image

Posted 24 August 2010 - 01:55 PM

The Robot class has a method createScreenCapture() that takes a screenshot...I think you mean whatever is on the screen, not the desktop b/c in your example you said "if i go to Google..."

You will also need to provide a little margin of error when comparing.

Posting your code would be a great start :)
Was This Post Helpful? 1
  • +
  • -

#4 Guest_nub*


Reputation:

Re: Check if desktop contains certain image

Posted 24 August 2010 - 02:47 PM

View Postbcranger, on 24 August 2010 - 12:55 PM, said:

The Robot class has a method createScreenCapture() that takes a screenshot...I think you mean whatever is on the screen, not the desktop b/c in your example you said "if i go to Google..."

You will also need to provide a little margin of error when comparing.

Posting your code would be a great start :)


1.jpg would be replaced with a bufferedimage of the desktop

import java.awt.image.BufferedImage;
import java.awt.image.PixelGrabber;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;

import javax.imageio.ImageIO;

public class Main {
	public int[] pixels(BufferedImage image) {
		PixelGrabber grabber;
		int[] pixels = new int[image.getWidth() * image.getHeight()];
		try {
			grabber = new PixelGrabber(image, 0, 0, image.getWidth(),
					image.getHeight(), pixels, 0, image.getWidth());
			grabber.grabPixels(0);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return pixels;
	}

	public void program() {
		BufferedImage image = null;
		BufferedImage image2 = null;
		try {
			image = ImageIO.read(new File("1.jpg"));
		} catch (IOException e) {
		}
		try {
			image2 = ImageIO.read(new File("2.jpg"));
		} catch (IOException e) {
		}
		int[] a = pixels(image);
		int[] b = pixels(image2);
		System.out.println(Boolean.toString(compare(a, B)/>));
	}

	public boolean compare(int[] a, int[] B)/> {
		Arrays.sort(a);
		for (int i : B)/> {
			if (Arrays.binarySearch(a, i) < 0) {
				return false;
			}
		}
		return true;

	}

	public static void main(String[] args) {
		Main main = new Main();
		main.program();

	}
}


Was This Post Helpful? 0

#5 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Check if desktop contains certain image

Posted 24 August 2010 - 02:54 PM

Your code is comparing two separate images, nothing related to the desktop or computer screen...

Clarify this: You want to compare two images OR You want to compare desktop (just background, what else?) to image OR You want to compare whatever is on screen to image?
Was This Post Helpful? 0
  • +
  • -

#6 Guest_nub*


Reputation:

Re: Check if desktop contains certain image

Posted 24 August 2010 - 02:59 PM

View Postbcranger, on 24 August 2010 - 01:54 PM, said:

Your code is comparing two separate images, nothing related to the desktop or computer screen...

Clarify this: You want to compare two images OR You want to compare desktop (just background, what else?) to image OR You want to compare whatever is on screen to image?



I would like it to return true if I go to google.com just like in the example I explained. The code I have is from another thread (but still relevant); I want it to compare the desktop to an image to see if the desktop contains the image
Was This Post Helpful? 0

#7 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: Check if desktop contains certain image

Posted 24 August 2010 - 03:08 PM

bcranger, you can make it simpler by grabbing the site HTML, finding the images and then downloading the images into an array. Then compare each image to the one provided in the way that mac suggested. Perhaps finding it in a larger image *might* be easier, but I'm not sure how it would handle having pixels around it. I guess that's something else I have to test eh?

View Postnub, on 24 August 2010 - 04:59 PM, said:

I would like it to return true if I go to google.com just like in the example I explained. The code I have is from another thread (but still relevant); I want it to compare the desktop to an image to see if the desktop contains the image


I think you're using the word desktop incorrectly....It makes little sense. Could you clarify?
Was This Post Helpful? 1
  • +
  • -

#8 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Check if desktop contains certain image

Posted 24 August 2010 - 03:14 PM

Dogstopper's idea would be more efficient I believe. As for his usage of the word "desktop" he means "screen"...how in the world does his "desktop" go to Google idk :helpsmilie:

Edit: Would only work if he is using web browser.

This post has been edited by bcranger: 24 August 2010 - 03:19 PM

Was This Post Helpful? 0
  • +
  • -

#9 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: Check if desktop contains certain image

Posted 24 August 2010 - 03:22 PM

View Postbcranger, on 24 August 2010 - 05:14 PM, said:

Dogstopper's idea would be more efficient I believe.


It might, it might not. It depends.
Steps in your method:
1. Take a screenshot and save as image.
2. Search for the needed block of pixels defined by the given image in the screenshot

My method:
1. Parse site HTML, finding locations of images
2. For each image, download it and save in an array.
3. Compare each image DIRECTLY to the other.

My first and second steps may take longer than your second one does. However, your method DOES NOT take into account if the page scrolls. If there is too much information to fit on the screen, your method is COMPLETELY invalid. Mine still will work.
Was This Post Helpful? 0
  • +
  • -

#10 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1940
  • View blog
  • Posts: 4,027
  • Joined: 11-December 07

Re: Check if desktop contains certain image

Posted 24 August 2010 - 05:35 PM

Depends what question you're trying to answer. "Can I see the google logo?" should not search below the fold.

I'm interested to see how this performs. I'm already thinking of scanning for the highest frequency line in the smaller image, finding a match in the larger image and checking those above and below. It may be a premature optimisation though.
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Check if desktop contains certain image

Posted 24 August 2010 - 05:50 PM

Don't really understand your problem
If your image is displayed by your application this application should know which images it displayed
if the image is displayed by another application, Java, despite its Robot class, is not really the good tool/language for that
Was This Post Helpful? 0
  • +
  • -

#12 Guest_nub*


Reputation:

Re: Check if desktop contains certain image

Posted 24 August 2010 - 09:38 PM

View Postpbl, on 24 August 2010 - 04:50 PM, said:

Don't really understand your problem
If your image is displayed by your application this application should know which images it displayed
if the image is displayed by another application, Java, despite its Robot class, is not really the good tool/language for that



I say desktop because I don't just mean the browser. I mean everything on the screen. I would like it to return true if the google logo appears anywhere on the screen.
Was This Post Helpful? 0

#13 Guest_nub*


Reputation:

Re: Check if desktop contains certain image

Posted 24 August 2010 - 09:55 PM

I got it to work. Instead of doing prtscrn and getting the google logo in paint, I downloaded the logo straight from their site.

Then I used this code and it worked great:
import java.awt.AWTException;
import java.awt.HeadlessException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.PixelGrabber;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;

import javax.imageio.ImageIO;

public class Main {
	public int[] pixels(BufferedImage image) {
		PixelGrabber grabber;
		int[] pixels = new int[image.getWidth() * image.getHeight()];
		try {
			grabber = new PixelGrabber(image, 0, 0, image.getWidth(),
					image.getHeight(), pixels, 0, image.getWidth());
			grabber.grabPixels(0);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return pixels;
	}

	public void program() {
		BufferedImage screencapture = null;
		BufferedImage image = null;
		try {
			screencapture = new Robot().createScreenCapture(new Rectangle(
					Toolkit.getDefaultToolkit().getScreenSize()));
		} catch (HeadlessException e1) {
		} catch (AWTException e1) {
		}

		try {
			image = ImageIO.read(new File("google.jpg"));
		} catch (IOException e) {
		}
		int[] a = pixels(screencapture);
		int[] b = pixels(image);

		System.out.println(Boolean.toString(compare(a, B)/>));
	}

	public boolean compare(int[] a, int[] B)/> {
		Arrays.sort(a);
		for (int i : B)/> {
			if (Arrays.binarySearch(a, i) < 0) {
				return false;
			}
		}
		return true;

	}

	public static void main(String[] args) {
		Main main = new Main();
		main.program();

	}
}



So the reason it wasn't working before I guess was because prtscrn doesn't capture exactly what is on the screen? How can I create like a 5% threshold that it still will return true.
Was This Post Helpful? 0

#14 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: Check if desktop contains certain image

Posted 25 August 2010 - 03:35 AM

View Postnub, on 24 August 2010 - 11:38 PM, said:

I say desktop because I don't just mean the browser. I mean everything on the screen. I would like it to return true if the google logo appears anywhere on the screen.


Even if the image IS on the page...you just need to scroll down to put it onto the screen?
Was This Post Helpful? 0
  • +
  • -

#15 Guest_nub*


Reputation:

Re: Check if desktop contains certain image

Posted 25 August 2010 - 08:53 AM

View PostDogstopper, on 25 August 2010 - 02:35 AM, said:

View Postnub, on 24 August 2010 - 11:38 PM, said:

I say desktop because I don't just mean the browser. I mean everything on the screen. I would like it to return true if the google logo appears anywhere on the screen.


Even if the image IS on the page...you just need to scroll down to put it onto the screen?



Yes I understand that. How can I create a percent error that it will still return true. Like if the pixels are about 5% the same.
Was This Post Helpful? 0

Page 1 of 1