Results 1 to 9 of 9
  1. #1
    Roshani is offline Member
    Join Date
    Jul 2016
    Posts
    18
    Rep Power
    0

    Default Robot Class to get screen capture

    Hi All,

    I am going to capture my application screen using robot class.

    To do this i have add one button in my gui app & 1 check box.

    When user will press button then program will save image of my application.

    If user have selected checkbox option then i am receiving serial data every second & same time as data received , program will automatic take snap shot of my application. I am calling below method every time when i received serial data.

    frame number is auto increment.

    Here is my method & it is working, problem is saving image time is too much.



    Java Code:
    // capture frame
        // pass file name as para
        // must pass 3 parameter
        // saveFrame(filePath,fileName,fileFormat)
        // fileFormat may be jpg or png
        public static void saveFrame(String fPath,String fName, String fFormat){
            long before = System.currentTimeMillis();
            try {
                Robot robot = new Robot(); 
                String format = fFormat;
                String fileName = fPath + fName + "." + format;
                 
               Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
               BufferedImage screenFullImage = robot.createScreenCapture(screenRect);
                ImageIO.write(screenFullImage, format, new File(fileName));
                 
                System.out.println("A full screenshot saved!");
            } catch (AWTException | IOException ex) {
                System.err.println(ex);
            }
    
            long after = System.currentTimeMillis();
            System.out.println("Operation took " + (after - before) + " milliseconds.");
        }

    To Save frame in button event here is my code, same is calling from my serial event.

    Java Code:
    System.out.println("Capture Image Button is pressed");
            frameCnt++;
            saveFrame("C:\\Temp\\","Frame" + frameCnt,"png");
    My problem is save frame timing is too much. It takes approx 500ms to 600 ms.

    How can i reduce time ?

    If i want to make movie after some time then at least program can save 20 frames per second.

    Pls. Help.

  2. #2
    Pavlo is offline Member
    Join Date
    Dec 2012
    Posts
    28
    Rep Power
    0

    Default Re: Robot Class to get screen capture

    First I would think about is an objects instantiation:

    Java Code:
    Robot robot = new Robot(); 
    Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
    Is it possible to not instantiate them each time you call saveFrame() ? May be it possible to pass them as parameter to saveFrame() or have class property instntiated just once.

    Though I not sure it is the bottleneck. Please give some info about your images size and hardware you have. Try to know what takes more time:

    Java Code:
    robot.createScreenCapture(screenRect);
    or

    Java Code:
    ImageIO.write(screenFullImage, format, new File(fileName));
    If second case: it could be useful to store all images in one tmp file (or in RAM if possible), so it will be opened once and not closed until all data saved. After that you will create one more method and it will read from tmp and save separate png files. This should make IO faster for temp file, but will take some additional time to save png after temp file with captured images saved.

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Robot Class to get screen capture

    I am not sure if this idea of recording your screen will work with Java, but if you had to do it at gunpoint, I'd say use (at least) two threads. One for creating the images and storing them in a buffer and the second thread will take an image and save it to disk. Obviously disk is going to be the slowest factor here. I doubt you can get it running up to 24 frames/second. Also, prepare to set -Xmx to a high number because screencaptures are huge. Creating 24 frames per second will eat your memory fast.

    For example: my screen is 3440 1440 with 32 bit colour. That means 4.953.600pixels, with each having 32 bits in colour/alpha, means 158.515.200 bits, or 19.814.400 bytes, roughly 18,9 MB a pop. 18,9 24 fps = 453,5 MB per second. I doubt your SATA spinning disk will keep up with that. Perhaps an SSD will save you here.

    Let us know how you did. I am curious if it works.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  4. #4
    Roshani is offline Member
    Join Date
    Jul 2016
    Posts
    18
    Rep Power
    0

    Default Re: Robot Class to get screen capture

    Dear Sir,

    Thanks for replay & idea.

    Actually my app will cover whole screen, according to me it is best to capture image of my JFrame .

    It is not necessary to use robot class, if another option is available then i will go for that.

    Thanks

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Robot Class to get screen capture

    Have a look at the answer here on SO.

    You essentially call you frame.paint() method and pass in the Graphics of the BufferedImage you want to draw to.
    Then write that out using the ImageIO stuff.

    That should be quicker, and smaller, but I still doubt you'll get to 20fps.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Roshani is offline Member
    Join Date
    Jul 2016
    Posts
    18
    Rep Power
    0

    Default Re: Robot Class to get screen capture

    Hi All,

    Currently I have implement following method to capture my screen. I chooses file format png & it took approx time 150ms to 200 ms.

    Not bad, if i need one frame per second.

    Java Code:
    // capture Jpanel
        public static void capturePanel (JPanel argPanel, String fileName) {
            long before = System.currentTimeMillis();
            Rectangle rec = argPanel.getBounds();        
            BufferedImage capture = new BufferedImage(rec.width, rec.height,
                                            BufferedImage.TYPE_INT_ARGB);
            argPanel.paint(capture.getGraphics());
    
           try {
            
            ImageIO.write(capture, "png", new File(fileName));
            
        } catch (IOException ioe) {
            System.out.println(ioe);
        } 
           long after = System.currentTimeMillis();
            System.out.println("Operation took " + (after - before) + " milliseconds.");
    }

    still working on it.

    What should i do if i want to record movie ?

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Robot Class to get screen capture

    It's not that complex. It's basically multiple Threads pushing/popping a queue.

    - Create a buffer that holds the frames. A ConcurrentLinkedQueue looks like it will give you the Queue functions you are looking for (Push a frame at the end while other threads pop it off the top). Maybe other classes from java.util.concurrent will work better.
    - Thread 1: Push the BufferedImage at the end of the queue
    - Thread 2-8: Pop the image at the top of the queue and save it. Here you can use multiple threads to speed up the work. Make sure to have a counter that keeps track of the order.

    It's vital that you do this in multiple threads because when the ImageIO is doing its work, NOTHING else will happen but only that, so you will lose frames during disk I/O.

    Something like this....
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  8. #8
    Pavlo is offline Member
    Join Date
    Dec 2012
    Posts
    28
    Rep Power
    0

    Default Re: Robot Class to get screen capture

    to (SurfMan)

    Hi. I am agree with second thread for disc IO. But will it be faster to write to disk with few threads? I doubt that usual SATA drive will do its job significantly faster, that is why I asked about hardware and image size... But if few hard drives connected to PC, this could give much larger speed growth, so each thread writes to one of them. Also your counted 450 MB/sec write speed need ... Well files archived with png format, so this will be much smaller, but the ratio depends of image entropy.

    2(Roshani) Looks like we still need more info about your hardware and images

  9. #9
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Robot Class to get screen capture

    Quote Originally Posted by Pavlo View Post
    to (SurfMan)

    Hi. I am agree with second thread for disc IO. But will it be faster to write to disk with few threads? I doubt that usual SATA drive will do its job significantly faster, that is why I asked about hardware and image size... But if few hard drives connected to PC, this could give much larger speed growth, so each thread writes to one of them. Also your counted 450 MB/sec write speed need ... Well files archived with png format, so this will be much smaller, but the ratio depends of image entropy.

    2(Roshani) Looks like we still need more info about your hardware and images
    You are probably correct that several threads saving images on harddisk is not faster than one thread. It's trivial to create a test program to see what the performance is. It also depends on the size of the image ofcourse. Obviously 800600 is stored smaller and thus faster than 34401440. Go create a test program :)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. [HELP] Java screen capture App
    By irreconhecivell in forum Advanced Java
    Replies: 4
    Last Post: 10-24-2014, 01:06 AM
  2. Robot screen capture returns black image
    By Jaeela in forum New To Java
    Replies: 1
    Last Post: 11-30-2012, 09:51 AM
  3. Replies: 9
    Last Post: 09-06-2012, 09:37 AM
  4. Capture the screen in java
    By PatriciaLopes in forum New To Java
    Replies: 3
    Last Post: 05-05-2012, 01:22 AM
  5. Screen Capture Recursion
    By Bestsanchez in forum New To Java
    Replies: 17
    Last Post: 03-18-2012, 09:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •