Advertisement
Guest User

screen recorder / kritzikratzi :)

a guest
Sep 18th, 2012
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.63 KB | None | 0 0
  1. import java.awt.AWTException;
  2. import java.awt.BorderLayout;
  3. import java.awt.GraphicsEnvironment;
  4. import java.awt.MouseInfo;
  5. import java.awt.Point;
  6. import java.awt.Rectangle;
  7. import java.awt.Robot;
  8. import java.awt.image.BufferedImage;
  9. import java.io.File;
  10. import java.io.IOException;
  11. import java.text.SimpleDateFormat;
  12. import java.util.Collection;
  13. import java.util.Date;
  14. import java.util.concurrent.LinkedBlockingQueue;
  15.  
  16. import javax.imageio.ImageIO;
  17. import javax.swing.JFrame;
  18. import javax.swing.JLabel;
  19.  
  20.  
  21. public class CaptureTest {
  22.    
  23.     // how many fps do you want to record at ...
  24.     final static int FPS = 25;
  25.    
  26.     // how many frames into the past?
  27.     final static int HISTORY_SIZE = 50;
  28.    
  29.     // destination directory...
  30.     final static File BASEDIR = new File( "capture" );
  31.    
  32.     // record a small rectangle around the mouse, use RECORD_W=-1 to record the entire screen
  33.     final static int RECORD_W = 500;
  34.     final static int RECORD_H = 500;
  35.    
  36.    
  37.     public static void main(String[] args) throws AWTException, InterruptedException {
  38.         // Make sure directory exists ...
  39.         BASEDIR.mkdirs();
  40.        
  41.         // create a frame to display current fps
  42.         JFrame frame = new JFrame( "Screen Recorder" );
  43.         frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  44.         JLabel fpsLabel = new JLabel();
  45.         fpsLabel.setAlignmentX( 0.5f );
  46.         fpsLabel.setAlignmentY( 0.5f );
  47.         frame.getContentPane().add( fpsLabel, BorderLayout.CENTER );
  48.         frame.setSize( 300, 200 );
  49.         frame.setVisible( true );
  50.        
  51.         // we store our images here ...
  52.         LinkedBlockingQueue<Img> history = new LinkedBlockingQueue<>();
  53.        
  54.         // do the frame capturing party  
  55.         float averageDuration = 0;
  56.         long frameCount = 0;
  57.         Writer writer = new Writer();
  58.         Robot robot = new Robot();
  59.         Rectangle screenRect = GraphicsEnvironment
  60.                 .getLocalGraphicsEnvironment()
  61.                 .getDefaultScreenDevice()
  62.                 .getDefaultConfiguration()
  63.                 .getBounds();
  64.        
  65.         while( true ){
  66.             // keep a constant # of items in the history
  67.             if( history.size() > HISTORY_SIZE ){
  68.                 Img old = history.poll();
  69.                 old.image.flush(); // free memory
  70.             }
  71.            
  72.             // record next frame
  73.             long start = System.currentTimeMillis();
  74.             Rectangle userRect = new Rectangle( screenRect );
  75.             if( RECORD_W > 0 ){
  76.                 Point mouse = MouseInfo.getPointerInfo().getLocation();
  77.                 Rectangle mouseRect = new Rectangle( mouse.x - RECORD_W/2, mouse.y - RECORD_H/2, RECORD_W, RECORD_H );
  78.                 Rectangle.intersect( screenRect, mouseRect, userRect );
  79.             }
  80.            
  81.             BufferedImage img = robot.createScreenCapture( userRect );
  82.             history.add( new Img( start, img ) );
  83.            
  84.            
  85.             // check for some condition (e.g. find green spot or whatnot)
  86.             if( Math.random() > 0.99 ){
  87.                 // YES!
  88.                 System.out.println( new Date().toString() + " - Adding last " +  HISTORY_SIZE + "frames..." );
  89.                 writer.addAll( history );
  90.                 history.clear();
  91.             }
  92.            
  93.            
  94.             // compute average duration + show some stats sometimes
  95.             frameCount ++;
  96.             Thread.sleep( Math.max( 1, 1000/FPS - System.currentTimeMillis() + start ) );
  97.             averageDuration += ( System.currentTimeMillis() - start - averageDuration )/10f;
  98.            
  99.             if( frameCount % FPS == 0 ){
  100.                 System.out.println( "Recording at " + ((int)(10000/averageDuration))/10f + " fps" );
  101.                 fpsLabel.setText( "Recording at " + ((int)(10000/averageDuration))/10f + " fps" );
  102.             }
  103.         }
  104.     }
  105.    
  106.    
  107.     static class Img{
  108.         long timestamp;
  109.         BufferedImage image;
  110.        
  111.         public Img( long timestamp, BufferedImage image ){
  112.             this.timestamp = timestamp;
  113.             this.image = image;
  114.         }
  115.     }
  116.    
  117.     static class Writer extends Thread{
  118.         LinkedBlockingQueue<Img> writeQueue = new LinkedBlockingQueue<>();
  119.         SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd-HH-mm-ss-SSS" );
  120.        
  121.         public Writer(){
  122.             start();
  123.         }
  124.        
  125.         public void run(){
  126.             while( !isInterrupted() ){
  127.                 if( writeQueue.size() > 0 ){
  128.                     Img img = writeQueue.poll();
  129.                     long start = System.currentTimeMillis();
  130.                     try {
  131.                         ImageIO.write(
  132.                             img.image,
  133.                             "jpg",
  134.                             new File( BASEDIR, format.format( new Date( img.timestamp ) ) + ".jpg" )
  135.                         );
  136.                     } catch (IOException e) {
  137.                         e.printStackTrace();
  138.                     }
  139.                    
  140.                     img.image.flush();
  141.                    
  142.                     System.out.println(
  143.                         "wrote image in " + ( System.currentTimeMillis() - start ) + " ms; " +
  144.                         writeQueue.size() + " items left in write queue" );
  145.                 }
  146.                
  147.                 // some pause in between
  148.                 try {
  149.                     Thread.sleep( 10 );
  150.                 } catch (InterruptedException e) {
  151.                     System.out.println( "Was interrupted, bye!" );
  152.                 }
  153.             }
  154.         }
  155.        
  156.         public void addAll( Collection<Img> images ){
  157.             writeQueue.addAll( images );
  158.         }
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement