我有一个JFrame
窗口,其中包含几个按钮。我还想将javacv(opencv)中的视频源添加到帧的中间。我当前的示例将视频放在画布上。我想知道如何将画布添加到画面的中心,或者我是否可以将视频添加到JPanel
或其他内容,然后将其添加到画面中。
任何建议都将不胜感激
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) {
}
}
}