OpenCV_recordVideo.java
import javax.swing.JFrame;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.video.Video;
import org.opencv.videoio.VideoCapture;
import org.opencv.videoio.VideoWriter;
public class OpenCV_record_video {
static {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
public static void main(String arg[]) {
/*
* CV_FOURCC('P', 'I', 'M', '1') = MPEG-1 codec
CV_FOURCC('M', 'J', 'P', 'G') = motion-jpeg codec
CV_FOURCC('M', 'P', '4', '2') = MPEG-4.2 codec
CV_FOURCC('D', 'I', 'V', '3') = MPEG-4.3 codec
CV_FOURCC('D', 'I', 'V', 'X') = MPEG-4 codec
CV_FOURCC('U', '2', '6', '3') = H263 codec
CV_FOURCC('I', '2', '6', '3') = H263I codec
CV_FOURCC('F', 'L', 'V', '1') = FLV1 codec
http://www.fourcc.org/codecs.php
CV_FOURCC('P','I','M','1') = MPEG-1 codec
CV_FOURCC('M','J','P','G') = motion-jpeg codec (does not work well)
CV_FOURCC('M', 'P', '4', '2') = MPEG-4.2 codec
CV_FOURCC('D', 'I', 'V', '3') = MPEG-4.3 codec
CV_FOURCC('D', 'I', 'V', 'X') = MPEG-4 codec
CV_FOURCC('U', '2', '6', '3') = H263 codec
CV_FOURCC('I', '2', '6', '3') = H263I codec
CV_FOURCC('F', 'L', 'V', '1') = FLV1 codec
*/
System.out.println("VideoWriter.fourcc('M', 'J', 'P', 'G')="+VideoWriter.fourcc('M', 'J', 'P', 'G'));
System.out.println("VideoWriter.fourcc('M', 'P', '4', '2')="+VideoWriter.fourcc('M', 'P', '4', '2'));
System.out.println("VideoWriter.fourcc('D', 'I', 'V', '3')="+VideoWriter.fourcc('D', 'I', 'V', '3'));
System.out.println("VideoWriter.fourcc('D', 'I', 'V', 'X')="+VideoWriter.fourcc('D', 'I', 'V', 'X'));
System.out.println("VideoWriter.fourcc('U', '2', '6', '3')="+VideoWriter.fourcc('U', '2', '6', '3'));
System.out.println("VideoWriter.fourcc('I', '2', '6', '3')="+VideoWriter.fourcc('I', '2', '6', '3'));
VideoCapture capture = new VideoCapture();
capture.open(0);
VideoWriter vw=new VideoWriter("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\test.avi", VideoWriter.fourcc('M', 'J', 'P', 'G'), 25, new Size(640, 480));
JFrame frame1 = new JFrame("show image");
frame1.setTitle("Java Swing");
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setSize(640, 480);
frame1.setBounds(0, 0, frame1.getWidth(), frame1.getHeight());
Panel panel1 = new Panel();
frame1.setContentPane(panel1);
frame1.setVisible(true);
if (!capture.isOpened()) {
System.out.println("Error");
} else {
Mat webcam_image = new Mat();
capture.read(webcam_image);
frame1.setSize(webcam_image.width() + 40,webcam_image.height() + 60);
while (true) {
capture.read(webcam_image);
vw.write(webcam_image);
System.out.println("Java Swing");
panel1.setimagewithMat(webcam_image);
frame1.repaint();
}
}
}
}
Panel.java
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
import org.opencv.core.Mat;
class Panel extends JPanel {
private BufferedImage image;
public Panel() {
super();
}
private BufferedImage getimage() {
return image;
}
public void setimage(BufferedImage newimage) {
image = newimage;
return;
}
public void setimagewithMat(Mat newimage) {
image = this.matToBufferedImage(newimage);
return;
}
public BufferedImage matToBufferedImage(Mat matrix) {
int cols = matrix.cols();
int rows = matrix.rows();
int elemSize = (int) matrix.elemSize();
byte[] data = new byte[cols * rows * elemSize];
int type;
matrix.get(0, 0, data);
switch (matrix.channels()) {
case 1:
type = BufferedImage.TYPE_BYTE_GRAY;
break;
case 3:
type = BufferedImage.TYPE_3BYTE_BGR;
// bgr to rgb
byte b;
for (int i = 0; i < data.length; i = i + 3) {
b = data[i];
data[i] = data[i + 2];
data[i + 2] = b;
}
break;
default:
return null;
}
BufferedImage image2 = new BufferedImage(cols, rows, type);
image2.getRaster().setDataElements(0, 0, cols, rows, data);
return image2;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
BufferedImage temp = getimage();
if (temp != null)
g.drawImage(temp, 10, 10, temp.getWidth(), temp.getHeight(), this);
}
}
Recommended Posts