Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-5090776

Need more efficient screen capture.

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • 1.4.2
    • client-libs
    • x86
    • windows_2000

      Name: rmT116609 Date: 08/23/2004


      A DESCRIPTION OF THE REQUEST :
      The java.awt.Robot class allows you to capture the screen as a buffered image. This is fine when the method is occasionally called but for other uses such as creating screen recording software or something like a VNC server in java it is too slow. Furthermore, for these types of apps the raw image data as an array is much more useful and the getRGB method needs to be used to get the array data.

      JUSTIFICATION :
      It would allow for capturing the screen at higher frame rates.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I would like a way to capture the data faster similar to the getRGBPixels method for the RobotPeer. I.e.

      Robot robot=new Robot();
      int[] screen=robot.getRGBPixels(rect);

      However it would also be nice if the array could be a parameter so a new array does not need to be created for each screen capture. I.e.

      Robot robot=new Robot();
      int[] screen=new int[rect.width*rect.height];
      robot.getRGBPixels(rect, screen);
      ACTUAL -
      Robot robot=new Robot();
      BufferedImage image=robot.createScreenCapture(rect);
      int[] screen=new int[rect.width*rect.height];
      screen=image.getRGB(0, 0, rect.width, rect.height, screen, 0, rect.width);

      ---------- BEGIN SOURCE ----------
      import java.awt.*;
      import java.awt.peer.*;
      import java.awt.image.*;
      import sun.awt.SunToolkit;

      public class Test
      {
      public static void main(String[] args) throws Exception
      {
      // Get screen dimensions
      Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
      Rectangle r=new Rectangle(0, 0, d.width, d.height);

      // Old way
      long time=0;
      Robot robot=new Robot();
      int[] screen=new int[r.width*r.height];
      for(int i=0; i<100; i++)
      {
      long start=System.currentTimeMillis();
      BufferedImage image=robot.createScreenCapture(r);
      screen=image.getRGB(0, 0, r.width, r.height, screen, 0, r.width);
      for(int j=1; j<screen.length; j++)
      screen[j-1]=screen[j];
      long ctime=System.currentTimeMillis()-start;
      System.out.printf("Capture time %d: %d%n", i, ctime);
      time+=ctime;
      }
      System.out.printf("---- Average: %d%n%n", time/100);

      // Faster way using robot peer
      time=0;
      SunToolkit toolkit=(SunToolkit)Toolkit.getDefaultToolkit();
      RobotPeer robotPeer=toolkit.createRobot(new Robot(),
      GraphicsEnvironment.getLocalGraphicsEnvironment().
      getDefaultScreenDevice());
      for(int i=0; i<100; i++)
      {
      long start=System.currentTimeMillis();
      screen=robotPeer.getRGBPixels(r);
      for(int j=1; j<screen.length; j++)
      screen[j-1]=screen[j];
      long ctime=System.currentTimeMillis()-start;
      System.out.printf("Capture time %d: %d%n", i, ctime);
      time+=ctime;
      }
      System.out.printf("---- Average: %d%n%n", time/100);
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Though not perfect see the second part of the test case code using the robot peer. You have to use SunToolkit to get to the createRobot method which means using sun.* classes. Also there is no way to reuse the same array for each capture. Note on my machine the averages for the test case were 465 ms for standard robot and 247 ms when using the peer. Even with the peer (and very minimal processing of the data) only 4 frames per second could be processed and this would use a lot of CPU.
      (Incident Review ID: 300567)
      ======================================================================</TEXTAREA>
      </td>
                          </tr>
                          <TR>
                            <TD colspan="2" bgcolor="#BFBFBF"> </td>
                          </tr>

      <a name="comments"></a>
                          <!-- COMMENTS -->
                          <TR>
                            <TD bgcolor="#BFBFBF" align="left" valign="bottom" height="24">
      <img src="/bugz/images/dot.gif" width="10">Comments
      </td>
                            <TD bgcolor="#BFBFBF" align="left" valign="bottom" height="24">
      <!-- BEGIN:TBR Mohan
        <A href="javascript:doDateStampSubmit(document.editbug_general, 'comments');"><font size="-1">[ Date Stamp ]</font></A>
      <img src="/bugz/images/dot.gif" width="18">
      END:TBR -->
      <A href="javascript:doFullPageSubmit(document.editbug_general, 'comments');"><font size="-1">[ Full Page ]</font></a>
      <img src="/bugz/images/dot.gif" width="22">
      <FONT size="-1" color="darkblue">--- Enter SUN Proprietary data here ---</font>
      </td>
                          </tr>

                          <TR>
                            <TD bgcolor="#BFBFBF" colspan="2" nowrap align="left">
      <img src="/bugz/images/dot.gif" width="5">
                              <TEXTAREA rows="6" cols="95" wrap="virtual" name="comments" align="left" bgcolor="white">

      Name: rmT116609 Date: 08/23/2004


      (company - UNT , email - ###@###.###)
      ======================================================================
      ###@###.### 2004-08-25

            denis Denis Fokin (Inactive)
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: