I have a JFrame
window that contains a few buttons. I would also like to add the video feed from javacv (opencv) into the middle of the frame. My current example puts the video onto a canvas. I am wondering how I can add the canvas to the center of the frame or if I can add the video to a JPanel
or something else and then add it to the frame.
Any suggestions would be appreciated
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) {
}
}
}