Results 1 to 13 of 13
Thread: Time !
- 09-01-2010, 06:07 PM #1
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.
- 09-01-2010, 06:45 PM #2
Member
- Join Date
- Jun 2010
- Posts
- 28
- Rep Power
- 0
See how much you are actually sleeping.
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.Java Code:long timeStamp = System.currentTimeMillis(); Thread.sleep(10); System.out.println(System.currentTimeMillis() - timeStamp);
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.
- 09-01-2010, 06:50 PM #3
- 09-01-2010, 07:12 PM #4
1/100 of a second seems fast. I thought fps for viewing video was more like 20-40.
- 09-01-2010, 07:13 PM #5
Member
- Join Date
- Jun 2010
- Posts
- 28
- Rep Power
- 0
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.
- 09-01-2010, 07:52 PM #6
- 09-01-2010, 08:16 PM #7
Member
- Join Date
- Jun 2010
- Posts
- 28
- Rep Power
- 0
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
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.Java Code:sun.awt.image.CachingSurfaceManager.restoreLocalAcceleration(image);
- 09-01-2010, 08:34 PM #8
- 09-01-2010, 09:02 PM #9
Member
- Join Date
- Jun 2010
- Posts
- 28
- Rep Power
- 0
The quality would be identical to what you already have.
- 09-01-2010, 09:57 PM #10
- 09-02-2010, 03:07 PM #11
Member
- Join Date
- Jun 2010
- Posts
- 28
- Rep Power
- 0
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.
- 09-14-2010, 06:14 PM #12
- 09-14-2010, 07:17 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
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
-
JAVA Programmers Wanted - Full-Time and Part-Time Positions open
By javatrek2020 in forum Jobs OfferedReplies: 3Last Post: 08-23-2011, 12:46 PM -
calculate time diff for particular time period
By baktha.thalapathy in forum New To JavaReplies: 2Last Post: 05-24-2010, 04:10 PM -
Class Time - represents time of day
By verbazon in forum New To JavaReplies: 1Last Post: 04-13-2009, 01:06 AM -
Time
By Fireking in forum New To JavaReplies: 3Last Post: 09-07-2008, 01:30 AM -
i click on it,then first time error page comes,second time click then product page co
By 82rathi.angara in forum New To JavaReplies: 21Last Post: 08-01-2008, 11:13 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks