Select moving pixels

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: May 2008
Posts: 129
Reputation: PhiberOptik is an unknown quantity at this point 
Solved Threads: 4
PhiberOptik's Avatar
PhiberOptik PhiberOptik is offline Offline
Junior Poster

Select moving pixels

 
0
  #1
Apr 12th, 2009
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.

Thanks
PO
History will be kind to me for I intend to write it.
---------------------------------- Sir Winston Churchill
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 129
Reputation: PhiberOptik is an unknown quantity at this point 
Solved Threads: 4
PhiberOptik's Avatar
PhiberOptik PhiberOptik is offline Offline
Junior Poster

Re: Select moving pixels

 
0
  #2
Apr 12th, 2009
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?

  1. package toolsPKG;
  2.  
  3. import java.awt.*;
  4. import java.awt.image.*;
  5. import java.io.*;
  6.  
  7. import javax.imageio.*;
  8.  
  9. public class Screenshot {
  10. private static String[] argument = new String[2];
  11. public Screenshot(){}
  12. public void TakeScreen(String waitSecs, String fileName) throws Exception{
  13. argument[0] = waitSecs;
  14. argument[1] = fileName;
  15. // make sure we have exactly two arguments,
  16. // a waiting period and a file name
  17. if (argument.length != 2) {
  18. System.err.println("Usage: java Screenshot " +
  19. "WAITSECONDS OUTFILE.png");
  20. System.exit(1);
  21. }
  22. // check if file name is valid
  23. String outFileName = argument[1];
  24. if (!outFileName.toLowerCase().endsWith(".png")) {
  25. System.err.println("Error: output file name must " +
  26. "end with \".png\".");
  27. System.exit(1);
  28. }
  29. // wait for a user-specified time
  30. try {
  31. long time = Long.parseLong(argument[0]) * 1000L;
  32. System.out.println("Waiting " + (time / 1000L) +
  33. " second(s)...");
  34. Thread.sleep(time);
  35. } catch(NumberFormatException nfe) {
  36. System.err.println(argument[0] + " does not seem to be a " +
  37. "valid number of seconds.");
  38. System.exit(1);
  39. }
  40. // determine current screen size
  41. Toolkit toolkit = Toolkit.getDefaultToolkit();
  42. Dimension screenSize = toolkit.getScreenSize();
  43. Rectangle screenRect = new Rectangle(screenSize);
  44. // create screen shot
  45. Robot robot = new Robot();
  46. BufferedImage image = robot.createScreenCapture(screenRect);
  47. // save captured image to PNG file
  48. /*
  49. ImageIO.write(image, "png", new File(outFileName));
  50. // give feedback
  51. System.out.println("Saved screen shot (" + image.getWidth() +
  52. " x " + image.getHeight() + " pixels) to file \"" +
  53. outFileName + "\".");
  54. // use System.exit if the program hangs after writing the file;
  55. // that's an old bug which got fixed only recently
  56. // System.exit(0);
  57. */
  58. }
  59. }
Last edited by PhiberOptik; Apr 12th, 2009 at 9:02 pm. Reason: forgot to add the code
History will be kind to me for I intend to write it.
---------------------------------- Sir Winston Churchill
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,637
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is online now Online
Posting Virtuoso

Re: Select moving pixels

 
0
  #3
Apr 12th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 129
Reputation: PhiberOptik is an unknown quantity at this point 
Solved Threads: 4
PhiberOptik's Avatar
PhiberOptik PhiberOptik is offline Offline
Junior Poster

Re: Select moving pixels

 
0
  #4
Apr 13th, 2009
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?
History will be kind to me for I intend to write it.
---------------------------------- Sir Winston Churchill
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 129
Reputation: PhiberOptik is an unknown quantity at this point 
Solved Threads: 4
PhiberOptik's Avatar
PhiberOptik PhiberOptik is offline Offline
Junior Poster

Re: Select moving pixels

 
0
  #5
Apr 13th, 2009
I made this script up but it returns java.awt.Color[r=0,g=0,b=0]
  1. private static byte maskByteOne = (byte)(-128 << 24);
  2. private static byte maskByteTwo = (byte)(-128 << 16);
  3. private static byte maskByteThree = (byte)(-128 << 8);
  4. private static byte maskByteFour = (byte)(-128);
  5. public static Color colorFromArgb(int argb)
  6. {
  7. byte r = (byte)((argb & maskByteOne) >> 24);
  8. byte g = (byte)((argb & maskByteTwo) >> 16);
  9. byte b = (byte)((argb & maskByteThree) >> 8);
  10. byte a = (byte)((argb & maskByteFour));
  11. return new Color(r,g,b,a);
  12. }

AAAH I'm going crazy!

Thanks for the help!
History will be kind to me for I intend to write it.
---------------------------------- Sir Winston Churchill
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1,016
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 150
JamesCherrill JamesCherrill is offline Offline
Veteran Poster

Re: Select moving pixels

 
0
  #6
Apr 13th, 2009
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:
  1. int pixel = // whatever, eg getPixel(x, y);
  2. // int alpha = (pixel >> 24) & 0xff;
  3. int r = (pixel >> 16) & 0xff;
  4. int g = (pixel >> 8) & 0xff;
  5. int b = (pixel) & 0xff;

  1. public class PixelArray{
  2.  
  3. // useful (?) stuff for image processing via PixelGrabber's pixel array
  4.  
  5. private Image image;
  6. private int w, h;
  7. private int[] pixels;
  8.  
  9. public PixelArray(Image im) {
  10. image = im;
  11. w = image.getWidth(null);
  12. h = image.getHeight(null);
  13. pixels = new int[w * h];
  14. PixelGrabber pxg = new PixelGrabber(image, 0, 0, w, h, pixels, 0, w);
  15. try {
  16. pxg.grabPixels();
  17. } catch (InterruptedException e) {
  18. e.printStackTrace();
  19. }
  20. }
  21.  
  22. public int getPixel(int x, int y) {
  23. return pixels[x + y * w];
  24. }
  25. }
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 129
Reputation: PhiberOptik is an unknown quantity at this point 
Solved Threads: 4
PhiberOptik's Avatar
PhiberOptik PhiberOptik is offline Offline
Junior Poster

Re: Select moving pixels

 
0
  #7
Apr 13th, 2009
Awesome thanks JamesCherril, it seems to be outputting the correct information now.
History will be kind to me for I intend to write it.
---------------------------------- Sir Winston Churchill
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC