使用FrameGrabber JavaCV输出视频的速度

时间:2016-10-19 11:34:09

标签: java opencv image-processing javacv

我正在研究javaCV项目,我需要显示和记录网络摄像头捕获的内容。问题是我的代码中的输出视频具有比真实更高的速度。我尝试更改FrameGrabber和FrameRecorder的frameRate,但不是解决方案。

如果有人能帮助我,我将不胜感激: 这是我的代码:

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 个答案:

答案 0 :(得分:0)

我在这个API示例中找到了解决方案,我不得不使用时间戳。 https://github.com/bytedeco/javacv/blob/master/samples/WebcamAndMicrophoneCapture.java