Results 1 to 13 of 13

Thread: Time !

  1. #1
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Time !

    I need to make a screenshot ever 0.01 seconds. I use a beeper thing so I can hear whenever a screenshot has been made. But the beeper is beeping at different times? So that means it is not making a screebshot every 0.01 seconds. So how can I keep everything in time?

    (here is the class if you need it)
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package org.PhQProjects.capture;
    
    import java.awt.AWTException;
    import java.awt.Dimension;
    import java.awt.Rectangle;
    import java.awt.Robot;
    import java.awt.Toolkit;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.util.jar.Attributes;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    import javax.swing.JFileChooser;
    import javax.swing.filechooser.FileSystemView;
    
    /**
     *
     * @author Deividas G.
     */
    public class CoreMethods implements Runnable {
    
        public static boolean recording = false;
        Thread thread;
    
        public void startRecording() {
            recording = true;
            thread = new Thread(this);
            thread.start();
        }
    
        public void run() {
    
            try {
                Misc.println("Recording THREAD started...");
    
                //Checking
                if (!checkSCaptureDirectory()) {
                    recording = false;
                }
    
                int imagesCaptured = 0;
                JFileChooser fr = new JFileChooser();
                FileSystemView fw = fr.getFileSystemView();
    
                while (recording) {
    
                    Misc.println("Creating an image...");
                    BufferedImage screencapture = new Robot().createScreenCapture(
                            new Rectangle(new Dimension(500, 500)));
                    File file = new File(fw.getDefaultDirectory() + "\\SCapture\\" + "screencapture(" + imagesCaptured + ").png");
                    Toolkit.getDefaultToolkit().beep();
                    try {
                        ImageIO.write(screencapture, "png", file);
    
                    } catch (IOException ex) {
                        Logger.getLogger(CoreMethods.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    Misc.println("Done creating screencapture(" + imagesCaptured + ").png");
                    imagesCaptured++;
                    Misc.println("Thread is sleeping for 0.01 seconds");
                    Thread.sleep(10);
                    Misc.println("Attempting to loop");
                }
    
                Misc.println("Loop has been stopped !");
                //Loop should be finished
                //Convert to .avi
                Misc.println("Converting to .avi...");
                //Delete all images!
                Misc.println("Deleting recorded pictures...");
                for (int i = 0; i < imagesCaptured; i++) {
                    Misc.println("Deleteing image > " + i + " < ");
                    File f = new File(fw.getDefaultDirectory() + "\\SCapture\\" + "screencapture(" + i + ").png");
                    boolean success = f.delete();
    
                    if (!success) {
                        throw new IllegalArgumentException("Delete: deletion failed");
                    }else{
                        Misc.println("\t Done!");
                    }
    
                }
    
            } catch (InterruptedException ex) {
                Logger.getLogger(CoreMethods.class.getName()).log(Level.SEVERE, null, ex);
            } catch (AWTException ex) {
                Logger.getLogger(CoreMethods.class.getName()).log(Level.SEVERE, null, ex);
            }
    
    
        }
    
        public boolean checkSCaptureDirectory() {
            JFileChooser fr = new JFileChooser();
            FileSystemView fw = fr.getFileSystemView();
            File file = new File(fw.getDefaultDirectory() + "\\SCapture");
            boolean exists = file.exists();
            if (!exists) {
    
                boolean success = (new File(file.toString())).mkdir();
                if (success) {
                    return true;
                } else {
                    return false;
                }
            }
            return true;
        }
    }
    Last edited by PhQ; 09-01-2010 at 06:10 PM.

  2. #2
    Maxideon is offline Member
    Join Date
    Jun 2010
    Posts
    28
    Rep Power
    0

    Default

    See how much you are actually sleeping.

    Java Code:
    long timeStamp = System.currentTimeMillis();
    Thread.sleep(10);
    System.out.println(System.currentTimeMillis() - timeStamp);
    You may be hitting the accuracy limitation of your system's timer. On my machine, I can't get a thread to sleep anything less than 15 milliseconds.

    I would also contend that images are not exactly small objects, and that creating many of them in such short time spans will lead to lots of garbage collection. The writing to disk part probably isn't constant in time either.

  3. #3
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Quote Originally Posted by Maxideon View Post
    See how much you are actually sleeping.

    Java Code:
    long timeStamp = System.currentTimeMillis();
    Thread.sleep(10);
    System.out.println(System.currentTimeMillis() - timeStamp);
    You may be hitting the accuracy limitation of your system's timer. On my machine, I can't get a thread to sleep anything less than 15 milliseconds.

    I would also contend that images are not exactly small objects, and that creating many of them in such short time spans will lead to lots of garbage collection. The writing to disk part probably isn't constant in time either.
    Ok, I will extend my sleeping time, but do you know what fps would be good for screen capturing?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    1/100 of a second seems fast. I thought fps for viewing video was more like 20-40.

  5. #5
    Maxideon is offline Member
    Join Date
    Jun 2010
    Posts
    28
    Rep Power
    0

    Default

    I have no idea what would be a good sleep time. Try no sleep time and see how fast you can take screen shots and save them. With the garbage collection going on and the default java ImageWriters doing the writing you'll probably find the results pretty sucky. Certainly not fast enough to make a video.

    The only way I know to speed things up would require an extra library made by sun and use of some undocumented code.

  6. #6
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Quote Originally Posted by Norm View Post
    1/100 of a second seems fast. I thought fps for viewing video was more like 20-40.
    So do I have to do 1000/40 to find out the fps? Sorry for the stupid question, I'm kind of confused.


    Quote Originally Posted by Maxideon View Post
    I have no idea what would be a good sleep time. Try no sleep time and see how fast you can take screen shots and save them. With the garbage collection going on and the default java ImageWriters doing the writing you'll probably find the results pretty sucky. Certainly not fast enough to make a video.

    The only way I know to speed things up would require an extra library made by sun and use of some undocumented code.
    do you know what the library is called?

  7. #7
    Maxideon is offline Member
    Join Date
    Jun 2010
    Posts
    28
    Rep Power
    0

    Default

    The library I'm referring to is JAI-ImageIO. It's essentially a collection of the all the ImageReader and ImageWriter plugins made by Sun (including the default ones that come with the standard jdk). Two of the ImageWriters, a jpeg one and a png one, are "natively accelerated" and operate much faster then the default ones. Once installed, the natively accelerated ImageWriters become the default when using the method ImageIO#write.

    As for the undocumented and unsupported non-java code: If you look at the source code of Robot#createScreenCapture it ultimately calls

    Java Code:
    sun.awt.image.CachingSurfaceManager.restoreLocalAcceleration(image);
    to get the screen shot. In theory you can pass in the same BufferedImage every time. And by recycling images, the garbage collector would do less collecting.

  8. #8
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Quote Originally Posted by Maxideon View Post
    The library I'm referring to is JAI-ImageIO. It's essentially a collection of the all the ImageReader and ImageWriter plugins made by Sun (including the default ones that come with the standard jdk). Two of the ImageWriters, a jpeg one and a png one, are "natively accelerated" and operate much faster then the default ones. Once installed, the natively accelerated ImageWriters become the default when using the method ImageIO#write.

    As for the undocumented and unsupported non-java code: If you look at the source code of Robot#createScreenCapture it ultimately calls

    Java Code:
    sun.awt.image.CachingSurfaceManager.restoreLocalAcceleration(image);
    to get the screen shot. In theory you can pass in the same BufferedImage every time. And by recycling images, the garbage collector would do less collecting.
    Will the acceleration affect the quality?

  9. #9
    Maxideon is offline Member
    Join Date
    Jun 2010
    Posts
    28
    Rep Power
    0

    Default

    The quality would be identical to what you already have.

  10. #10
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Quote Originally Posted by Maxideon View Post
    The quality would be identical to what you already have.
    I don't get how can I add it to the program ? :confused:

  11. #11
    Maxideon is offline Member
    Join Date
    Jun 2010
    Posts
    28
    Rep Power
    0

    Default

    The page that I linked has install instructions. If you have a windows machine, then it amounts to just running the file "jai_imageio-1_1-lib-windows-i586-jdk.exe".

    Beyond that, there is nothing you have to do to your program. Your ImageIO#write calls will start using the faster image writers automatically.

  12. #12
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Quote Originally Posted by Maxideon View Post
    The page that I linked has install instructions. If you have a windows machine, then it amounts to just running the file "jai_imageio-1_1-lib-windows-i586-jdk.exe".

    Beyond that, there is nothing you have to do to your program. Your ImageIO#write calls will start using the faster image writers automatically.
    What if I want it send my program to my friends, then they will have to install all that. Can I just get the libraries and add them to the program?

  13. #13
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by PhQ View Post
    What if I want it send my program to my friends, then they will have to install all that. Can I just get the libraries and add them to the program?
    If you want your friends to be able to do on their machine as you are doing on you machine your friends need the same software as you are using on your machine; it doesn't take rocket science to figure that out. You have to find out how installers work; IzPack is a good (and free) option. Google is your friend here.

    kind regards,

    Jos

Similar Threads

  1. Replies: 3
    Last Post: 08-23-2011, 12:46 PM
  2. calculate time diff for particular time period
    By baktha.thalapathy in forum New To Java
    Replies: 2
    Last Post: 05-24-2010, 04:10 PM
  3. Class Time - represents time of day
    By verbazon in forum New To Java
    Replies: 1
    Last Post: 04-13-2009, 01:06 AM
  4. Time
    By Fireking in forum New To Java
    Replies: 3
    Last Post: 09-07-2008, 01:30 AM
  5. Replies: 21
    Last Post: 08-01-2008, 11:13 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
  •