0

我有一个JFrame包含几个按钮的窗口。我还想将来自 javacv (opencv) 的视频源添加到帧的中间。我当前的示例将视频放在画布上。我想知道如何将画布添加到框架的中心,或者是否可以将视频添加到 aJPanel或其他内容,然后将其添加到框架中。

任何建议,将不胜感激

import javax.swing.JFrame;
import javax.swing.JPanel;

import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.FrameGrabber;
import com.googlecode.javacv.cpp.opencv_core.*;
import com.googlecode.javacv.OpenCVFrameGrabber;
import static com.googlecode.javacv.cpp.opencv_core.cvFlip;

public class demo_video extends JFrame {

 public static void main(String[] args) {
  //Create canvas frame for displaying webcam.
     CanvasFrame canvas = new CanvasFrame("Webcam"); 
  //Set Canvas frame to close on exit
     canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);   
     //Declare FrameGrabber to import output from webcam
     FrameGrabber grabber = new OpenCVFrameGrabber(0);  
     try {      
      //Start grabber to capture video
      grabber.start();      
      //Declare img as IplImage
      IplImage img;

      while (true) {
       //inser grabed video fram to IplImage img
       img = grabber.grab();

       //Set canvas size as per dimentions of video frame.
       canvas.setCanvasSize(grabber.getImageWidth(), grabber.getImageHeight()); 

       if (img != null) {      
         //Flip image horizontally
         cvFlip(img, img, 1);
        //Show video frame in canvas
        canvas.showImage(img);               
        }
       }
      }
     catch (Exception e) {      
     }
    }
}
4

0 回答 0