Results 1 to 3 of 3
  1. #1
    drasgear is offline Member
    Join Date
    Nov 2008
    Posts
    2
    Rep Power
    0

    Default Newbie - Robot Class

    Hello,
    This is probably an easy one for an experienced Java programmer. I have a limited programming background and Java is very new to me, so the switch to an OO language is different than what I am used to. Most of my problems have been syntax related so far. I searched around this site and found a lot of information already, but I've spent spare time over two weeks searching google, forums, etc., without any more progress. As much as I'd like to figure it out on my own, eventually a person has to know when to ask for help. Anyway, what I am trying to do is make a Class that creates a .png image from a specified are using screencapture. I then want to load the image, turn it into a pixel arrary, and then compare it to other saved images to see if it is a match. So I need a class with three methods - getimage, loadimage, and cmprimage. I have been able to get the getimage to work, but I cannot get the image to load. No matter what I try, it returns null. TIA to anyone who can help me out. It does compile & run without error. Here is some code:

    //This works and saves the correct png image
    public void getimage () throws AWTException, IOException{
    //get image
    Robot robot = new Robot();
    Rectangle rect = new Rectangle(1075,556,30,14);
    BufferedImage scn = robot.createScreenCapture(rect);

    // Save image as PNG
    File file = new File("test.png");
    ImageIO.write(scn, "png", file);
    System.out.println("wrote new rect png file");
    }

    //This is where I get a null value all the time. If I try and do anything with img, it has a null value
    public void loadimage() throws IOException, AWTException{
    InputStream img = getClass().getResourceAsStream("test.png");
    BufferedImage bi = ImageIO.read(img);
    System.out.println("read buffered image");
    }
    Last edited by drasgear; 11-14-2008 at 02:42 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Your problem may be that the current directory for standard IO is different for that used by the getResourceAsStream. Why not load the image via ImageIO.read("test.png")?

  3. #3
    drasgear is offline Member
    Join Date
    Nov 2008
    Posts
    2
    Rep Power
    0

    Default

    You know, I just knew it would be something simple like that and I was just overthinking the problem. I tried to save and call explicitly, but it never worked for some reason. 5 min after I read your post, it was working fine. Thanks again - I should have posted sooner.

Similar Threads

  1. newbie newbie newbie
    By krislogy in forum New To Java
    Replies: 9
    Last Post: 08-15-2008, 12:28 AM
  2. Capturing video with Robot
    By russ2620 in forum AWT / Swing
    Replies: 0
    Last Post: 06-03-2008, 04:49 PM
  3. Robot Class
    By Jessie Madman in forum New To Java
    Replies: 3
    Last Post: 05-15-2008, 04:13 AM
  4. Newbie
    By CSnoob87 in forum Introductions
    Replies: 2
    Last Post: 02-18-2008, 08:49 AM
  5. Robot Class
    By jfredrickson in forum New To Java
    Replies: 2
    Last Post: 07-11-2007, 09:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •