Hi Guys,
I have been googling for a while, I think the problem is I don't know what I'm looking for is called.

I want to write a program that watches my screen (I assume it would take screen shots really fast) and then analyzes them finding the moving pixels.

If anyone could point me in the right direction I would appreciate that very much.


7 Years
Discussion Span
Last Post by PhiberOptik

Hey guys
I am using this to take the screen shot, and what I have is another image that is 150px by 150px that I wanna scan through the screen shot and see if I can find the small image.

Can anyone help me?

package toolsPKG;

import java.awt.*;
import java.awt.image.*;
import java.io.*;

import javax.imageio.*;

public class Screenshot {
	private static String[] argument = new String[2];
	public Screenshot(){}
	public void TakeScreen(String waitSecs, String fileName) throws Exception{
		argument[0] = waitSecs;
		argument[1] = fileName;
		// make sure we have exactly two arguments, 
		// a waiting period and a file name
		if (argument.length != 2) {
			System.err.println("Usage: java Screenshot " +
		// check if file name is valid
		String outFileName = argument[1];
		if (!outFileName.toLowerCase().endsWith(".png")) {
			System.err.println("Error: output file name must " +
				"end with \".png\".");
		// wait for a user-specified time
		try {
			long time = Long.parseLong(argument[0]) * 1000L;
			System.out.println("Waiting " + (time / 1000L) + 
				" second(s)...");
		} catch(NumberFormatException nfe) {
			System.err.println(argument[0] + " does not seem to be a " +
				"valid number of seconds.");
		// determine current screen size
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		Dimension screenSize = toolkit.getScreenSize();
		Rectangle screenRect = new Rectangle(screenSize);
		// create screen shot
		Robot robot = new Robot();
		BufferedImage image = robot.createScreenCapture(screenRect);
		// save captured image to PNG file
		ImageIO.write(image, "png", new File(outFileName));
		// give feedback
		System.out.println("Saved screen shot (" + image.getWidth() +
			" x " + image.getHeight() + " pixels) to file \"" +
			outFileName + "\".");
		// use System.exit if the program hangs after writing the file;
		// that's an old bug which got fixed only recently
		// System.exit(0); 

Huh? Are you saying you want to compare the images to see if they are duplicates? If so, you can do that by comparing pixel by pixel, since a blown up image converts every 1 pixel to 4 pixels or something like that. I don't know much about it, but I "studied" (read: didn't pay attention) to a similar topic in class, so I could point you towards some resources that would be helpful if that is what you're trying to accomplish.


okay scrap the above.

Heres what I got:

I'm using BufferedReader.getRGB(x, y); and it gives me -10452992 but what I really want is the seperate color codes, ie R=255 G=255 B=255 .

Any ideas how I can convert -10452992 to color codes?


I made this script up but it returns java.awt.Color[r=0,g=0,b=0]

private static byte maskByteOne = (byte)(-128 << 24);
	private static byte maskByteTwo = (byte)(-128 << 16);
	private static byte maskByteThree = (byte)(-128 << 8);
	private static byte maskByteFour = (byte)(-128);
	public static Color colorFromArgb(int argb)
		byte r = (byte)((argb & maskByteOne) >> 24);
		byte g = (byte)((argb & maskByteTwo) >> 16);
		byte b = (byte)((argb & maskByteThree) >> 8);
		byte a = (byte)((argb & maskByteFour));
		return new Color(r,g,b,a);

AAAH I'm going crazy!

Thanks for the help!


You can get an aray of pixels to do your scanning/looking for algorithms by using the PixelGrabber class (I've attached some code I wrote earlier that will give you a head start).
To decode an RGB pixel int, this works:

int pixel = // whatever,  eg getPixel(x, y);
// int alpha = (pixel >> 24) & 0xff;
int r = (pixel >> 16) & 0xff;
int g = (pixel >> 8) & 0xff;
int b = (pixel) & 0xff;
public class PixelArray{

	// useful (?) stuff for image processing via PixelGrabber's pixel array

	private Image image;
	private int w, h;
	private int[] pixels;

	public PixelArray(Image im) {
		image = im;
		w = image.getWidth(null);
		h = image.getHeight(null);
		pixels = new int[w * h];
		PixelGrabber pxg = new PixelGrabber(image, 0, 0, w, h, pixels, 0, w);
		try {
		} catch (InterruptedException e) {

	public int getPixel(int x, int y) {
		return pixels[x + y * w];

Awesome thanks JamesCherril, it seems to be outputting the correct information now.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.