-1

I am working on a javaCV project and I need to display and record what the webcam is capturing. The problem is that the output video in my code has a higher speed than the real. I tried changing the frameRate of the FrameGrabber and FrameRecorder but is not the solution.

I would appreciate if anyone could help me: Here is my code:

public static void main(String[] args) throws Exception, org.bytedeco.javacv.FrameGrabber.Exception, org.bytedeco.javacv.FrameRecorder.Exception{    

    // Display dimension
    int ancho = java.awt.Toolkit.getDefaultToolkit().getScreenSize().width;
    int alto = java.awt.Toolkit.getDefaultToolkit().getScreenSize().height;
    int screenNumber = CanvasFrame.getScreenDevices().length > 1 ? 1 : 0;


    FrameGrabber grabber = FrameGrabber.createDefault(0);
    grabber.setFrameRate(24);
    FrameRecorder recorder = FrameRecorder.createDefault("output.avi", ancho, alto);
    recorder.setFrameRate(24);
    recorder.start();

    // CanvasFrame is a JFrame containing a Canvas component, which is hardware accelerated.
    // It can also switch into full-screen mode when called with a screenNumber.
    // We should also specify the relative monitor/camera response for proper gamma correction.

    //Fullscreen mode
    CanvasFrame frame = new CanvasFrame("Prueba Webcam", CanvasFrame.getDefaultGamma()/grabber.getGamma());        
    DisplayMode displaymode = frame.getDisplayMode(screenNumber);
    frame.dispose();

    CanvasFrame frame2 = new CanvasFrame("Prueba 2", screenNumber, displaymode,CanvasFrame.getDefaultGamma()/grabber.getGamma());


    while (frame2.isVisible() ) {  

        // Exit with "Esc"
        frame2.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent ke) {  // handler
     if(ke.getKeyCode() == ke.VK_ESCAPE) {
         try {
                buildResults();
                System.out.println("escaped ?");
                System.exit(0);


            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

      } else {
         System.out.println("not escaped");
       }
} 

});

        //Display and record image
        frame2.showImage(grabber.grab());
        recorder.record(grabber.grab());

    }
    frame2.dispose();
   recorder.stop();
    grabber.stop();
}

}

1 Answers1

0

I found the solution in this API example, I had to use timestamps. https://github.com/bytedeco/javacv/blob/master/samples/WebcamAndMicrophoneCapture.java