Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / July 2007

Tip: Looking for answers? Try searching our database.

Screen recording needs a performance boost.

Thread view: 
pyro9219@gmail.com - 20 Jul 2007 22:20 GMT
I'm using robot to capture my screen in "real-time" and only able to
pull about 12fps, does anyone know how to improve the fps? Is the
while loop slowing it down?

long t0 = System.currentTimeMillis();
           int i = 0;
           int fps = 0;

           while (true) {
               i++;
               long t1 = System.currentTimeMillis();
               int seconds = (int) (t1-t0)/1000;

               try{
                   fps = i / seconds;
                   this.setTitle("FPS: " + fps);
               }
               catch( Exception e){
                   this.setTitle("FPS: " + fps);
               }

               Image screen_capture = new java.awt.Robot()
                       .createScreenCapture(new Rectangle(Toolkit
                       .getDefaultToolkit().getScreenSize()))
                       .getScaledInstance(Toolkit.getDefaultToolkit()
                       .getScreenSize().width / 2, Toolkit
                       .getDefaultToolkit().getScreenSize()
                       .height / 2, Image.SCALE_FAST);

               jLabel1.setIcon(new ImageIcon(screen_capture));
               jLabel1.update(jLabel1.getGraphics());
           }

Any help or idea's is appreciated.
Roedy Green - 20 Jul 2007 22:28 GMT
On Fri, 20 Jul 2007 14:20:16 -0700, "pyro9219@gmail.com"
<pyro9219@gmail.com> wrote, quoted or indirectly quoted someone who
said :

>    Image screen_capture = new java.awt.Robot()
>                        .createScreenCapture(new Rectangle(Toolkit
[quoted text clipped - 3 lines]
>                        .getDefaultToolkit().getScreenSize()
>                        .height / 2, Image.SCALE_FAST);

what happens if you don't scale, but save full size images to scale
later?
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

pyro9219@gmail.com - 21 Jul 2007 01:07 GMT
> On Fri, 20 Jul 2007 14:20:16 -0700, "pyro9...@gmail.com"
> <pyro9...@gmail.com> wrote, quoted or indirectly quoted someone who
[quoted text clipped - 13 lines]
> Roedy Green Canadian Mind Products
> The Java Glossaryhttp://mindprod.com

No Change.. I had it that way originally.

I'm going to break up the logic so the screen capture and jLabel have
their own threads, since I'm working on a dual core system, this might
help since I've noticed the performance is heavily based on the CPU
speed.

I've actually compared my results to VNC "visually", and they look the
same to my eyes when it comes to testing the start menu response. I've
found that to be a very good test of performance. I think unless it's
going to take something simple to fix it, for my project it's fine.
I'm just doing this as a fun learning experience.
Andrew Thompson - 21 Jul 2007 03:54 GMT
>I'm using robot to capture my screen in "real-time" and only able to
>pull about 12fps, does anyone know how to improve the fps? Is the
>while loop slowing it down?

Of course it is!  The code is setting up try/catch constructs,
instantiating  objects, updating a number of GUI elements,
scaling images, ..and  creating a screencapture of the entire
screen!
...
>Any help or idea's is appreciated.

Here is my altered code, and test results..

Oh, and while I recall, please post SSCCE's, rather
than code snippets..
<http://www.physci.org/codes/sscce.html>

<sscce>
import java.awt.*;
import javax.swing.*;

public class AnimatedScreenCapture extends JFrame {

 JLabel jLabel1;
 Robot robot;

 Rectangle rectangle1;

 int scaleX;
 int scaleY;

 public AnimatedScreenCapture() {
   super("Animated Screen Capture");
   setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
   jLabel1 = new JLabel();
   jLabel1.setPreferredSize(new Dimension(400,300));
   getContentPane().add( jLabel1 );

   rectangle1 = new Rectangle(100,100);
   scaleX = Toolkit
       .getDefaultToolkit()
       .getScreenSize()
       .width / 2;
   scaleY =Toolkit
       .getDefaultToolkit()
       .getScreenSize()
       .height / 2;
   try {
     robot = new java.awt.Robot();
   } catch (AWTException e) {
     e.printStackTrace();
     System.exit(-1);
   }
 }

 public void startCapture2() {
   long t0 = System.currentTimeMillis();
   int i = 0;
   int fps = 0;

   while (true) {
     i++;
     long t1 = System.currentTimeMillis();
     int seconds = (int) (t1-t0)/1000;

     if (i%1000==0 && seconds>0) {
       fps = i / seconds;
       this.setTitle("FPS: " + fps);
     }

     Image screen_capture = robot
       .createScreenCapture(rectangle1)
       .getScaledInstance(scaleX, scaleY, Image.SCALE_FAST)
       ;

     jLabel1.setIcon(new ImageIcon(screen_capture));
     jLabel1.update(jLabel1.getGraphics());
   }
 }

 public static void main(String[] args) throws AWTException {
   Thread t = new Thread() {
     public void run() {
       AnimatedScreenCapture asc = new AnimatedScreenCapture();
       asc.pack();
       asc.setVisible(true);
       asc.startCapture2();
     }
   };
   SwingUtilities.invokeLater(t);
 }
}
</sscce>

Results:
FPS   Conditions
3 original code (screen size 1024x768)
3 instantiated robot in constructor
4 replaced try/catch on division with test for 0.
20-22     screencap2 with rect of 100,100
250-290+       "       "   "   "     "    and no scale
520-600+       "       "   "   "     "    and no label update
540+           "       "   "   "     "    and no label update/scale

It seems most of the time is taken up scaling the image
and updating the GUI.  This should be done in a separate
thread.  

This is not a good project for a newbie.

Signature

Andrew Thompson
http://www.athompson.info/andrew/

pyro9219@gmail.com - 22 Jul 2007 07:05 GMT
> pyro9...@gmail.com wrote:
> >I'm using robot to capture my screen in "real-time" and only able to
[quoted text clipped - 112 lines]
>
> Message posted via JavaKB.comhttp://www.javakb.com/Uwe/Forums.aspx/java-setup/200707/1

Thanks for your time! I appreciate your efforts. I'll remember to give
usable code in the future. I guess in my head I was expecting  there
to be some sort of obvious problem with my code. Not sure about your
"not a good project for a newbie" comment though.. I'm not new to
programming, I'm just newer to Java, and I've never done anything
multi-threaded so I figured this project would be good. Either way,
I'm moving on because I'm having fun.

I changed the screen area size captured to the full desktop resolution
and found that your application isn't any faster then mine. However,
your comments and code gave me some idea's on improving mine, and I've
gained about 5fps.

This is what I've come up with for the code (the GUI is in a seperate
class file, and isn't included)

import java.awt.AWTException;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import javax.swing.ImageIcon;

public class ThreadedTest_HelperV2 extends Thread {

   ThreadTest_Form myForm;

   public ThreadedTest_HelperV2(ThreadTest_Form parent) {
       this.myForm = parent;
   }

   public void run() {
       int i = 0;
       int fps = 0;
       Robot robot = null;
       Image screen_capture = null;

       Rectangle screen = new
Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
       int scaleX =
Toolkit.getDefaultToolkit().getScreenSize().width / 4;
       int scaleY =
Toolkit.getDefaultToolkit().getScreenSize().height / 4;
       myForm.setSize(scaleX+5, scaleY+25);

       long t0 = System.currentTimeMillis();
       while (true) {
           i++;
           long t1 = System.currentTimeMillis();
           int seconds = (int) (t1-t0) / 1000;

           if (seconds > 1) {
               fps = i / seconds;
               myForm.setTitle("FPS: " + fps);
           }
           else
               myForm.setTitle("FPS: Analyzing..");

           try {
               robot = new java.awt.Robot();
               screen_capture =
robot.createScreenCapture(screen).getScaledInstance(scaleX, scaleY,
Image.SCALE_FAST);
           } catch (AWTException e) {
               e.printStackTrace();
               System.exit(-1);
           }

           myForm.jLabel1.setIcon(new ImageIcon(screen_capture));
           myForm.jLabel1.update(myForm.jLabel1.getGraphics());
       }
   }
}

The only real issue I've come up with using this code is that my
jframe/jlabel (can't tell which) flickers a bit. I've read that this
is because you aren't supposed to update swing elements inside a
thread, but I'm still working on separating the logic. Running into
problems with this since I keep getting "non-static blah blah must be
set to static" when trying to modify objects.

Any hints?
pyro9219@gmail.com - 22 Jul 2007 10:26 GMT
On Jul 21, 11:05 pm, "pyro9...@gmail.com" <pyro9...@gmail.com> wrote:

> > pyro9...@gmail.com wrote:
> > >I'm using robot to capture my screen in "real-time" and only able to
[quoted text clipped - 196 lines]
>
> Any hints?

Woohoo! I got it..

Here is my final code - No flickers, UI doesn't lock :)

import javax.swing.JLabel;
import java.awt.AWTException;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import javax.swing.ImageIcon;

public class ThreadedTest_Helper extends Thread {

   JLabel screenArea;
   ThreadTest_Form myForm;
   Image screen_capture = null;
   int i = 0;
   int fps = 0;
   Robot robot = null;

   Rectangle screen = new
Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
   int scaleX = Toolkit.getDefaultToolkit().getScreenSize().width /
3;
   int scaleY = Toolkit.getDefaultToolkit().getScreenSize().height /
3;

   public ThreadedTest_Helper(ThreadTest_Form parent) {
       myForm = parent;
       screenArea = parent.jLabel1;
       myForm.setSize(scaleX + 5, scaleY + 25);
   }

   public void run() {
       long t0 = System.currentTimeMillis();
       while (true) {
           i++;
           long t1 = System.currentTimeMillis();
           final int seconds = (int) (t1-t0) / 1000;

           java.awt.EventQueue.invokeLater(new Runnable() {
               public void run() {
                   if (seconds > 1) {
                       fps = i / seconds;
                       myForm.setTitle("FPS: " + fps);
                   } else {
                       myForm.setTitle("FPS: Analyzing..");
                   }
               }
           });

           try {
               robot = new java.awt.Robot();
               screen_capture =
robot.createScreenCapture(screen).getScaledInstance(scaleX, scaleY,
Image.SCALE_FAST);
           } catch (AWTException e) {
               e.printStackTrace();
               System.exit(-1);
           }

           java.awt.EventQueue.invokeLater(new Runnable() {
               public void run() {
                   screenArea.setIcon(new ImageIcon(screen_capture));
                   //screenArea.update(screenArea.getGraphics());
               }
           });
       }
   }
}

Now I'm just need to comment it and I'll move on to my next bit of
code. Thanks again for the help!
pyro9219@gmail.com - 22 Jul 2007 10:29 GMT
On Jul 21, 11:05 pm, "pyro9...@gmail.com" <pyro9...@gmail.com> wrote:

> > pyro9...@gmail.com wrote:
> > >I'm using robot to capture my screen in "real-time" and only able to
[quoted text clipped - 196 lines]
>
> Any hints?

Woohoo! I got it all running smooth now @ about 18-20fps while running
native res! (1680x1050)

Time to comment, then move on to the next chunk of code!

Thanks again for the help, below is my code if anyone is curious.

import javax.swing.JLabel;
import java.awt.AWTException;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import javax.swing.ImageIcon;

public class ThreadedTest_Helper extends Thread {

   JLabel screenArea;
   ThreadTest_Form myForm;
   Image screen_capture = null;
   int i = 0;
   int fps = 0;
   Robot robot = null;

   Rectangle screen = new
Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
   int scaleX = Toolkit.getDefaultToolkit().getScreenSize().width /
3;
   int scaleY = Toolkit.getDefaultToolkit().getScreenSize().height /
3;

   public ThreadedTest_Helper(ThreadTest_Form parent) {
       myForm = parent;
       screenArea = parent.jLabel1;
       myForm.setSize(scaleX + 5, scaleY + 25);
   }

   public void run() {
       long t0 = System.currentTimeMillis();
       while (true) {
           i++;
           long t1 = System.currentTimeMillis();
           final int seconds = (int) (t1-t0) / 1000;

           java.awt.EventQueue.invokeLater(new Runnable() {
               public void run() {
                   if (seconds > 1) {
                       fps = i / seconds;
                       myForm.setTitle("FPS: " + fps);
                   } else {
                       myForm.setTitle("FPS: Analyzing..");
                   }
               }
           });

           try {
               robot = new java.awt.Robot();
               screen_capture =
robot.createScreenCapture(screen).getScaledInstance(scaleX, scaleY,
Image.SCALE_FAST);
           } catch (AWTException e) {
               e.printStackTrace();
               System.exit(-1);
           }

           java.awt.EventQueue.invokeLater(new Runnable() {
               public void run() {
                   screenArea.setIcon(new ImageIcon(screen_capture));
                   //screenArea.update(screenArea.getGraphics());
               }
           });
       }
   }
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.