Desktop: OpenCV manual a take picture

Goal
Manually capture webcam preview and save to folder.

TakeAPictureManual.java


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.videoio.VideoCapture;

public class TakeAPictureManual {

	static {
		System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
	}
	
	public static void main(String[] args) {
		VideoCapture camera = new VideoCapture();
		camera.open(0);
		JFrame frame1 = new JFrame("Show image");
		frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame1.setTitle("從 webcam 讀 image image arrival Java swing window");
		frame1.setSize(600,480);
		frame1.setBounds(0,0,frame1.getWidth(),frame1.getHeight());
		Panel panel1 = new Panel();
		frame1.setContentPane(panel1);
		frame1.setVisible(true);
		
		JButton btnCapture = new JButton("Beat");
		btnCapture.setEnabled(false);
		btnCapture.setBounds(680,97,87,23);
		frame1.getContentPane().add(btnCapture);
		
		if(camera.isOpened()) {
			final Mat webcam_preview = new Mat();
			camera.read(webcam_preview);
			frame1.setSize(webcam_preview.width()+50,webcam_preview.height()+70);
			btnCapture.setEnabled(true);
			btnCapture.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					Imgcodecs.imwrite("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\manual_take_webcam_frame.jpg ",webcam_preview);
					System.out.println("Manual rhythm");
				}
			});
			
			while (true) {
				
				try {
					camera.read(webcam_preview);
					Thread.sleep(500);
					camera.read(webcam_preview);
					Thread.sleep(500);
					camera.read(webcam_preview);
					Thread.sleep(500);
				} catch (InterruptedException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			
				System.out.println("Used camcorder");
				panel1.setimagewithMat(webcam_preview);
				frame1.repaint();
			}
			
		}else {
			System.out.println("Error");
		}
		
		
	}

}

Reault
![manual_take_webcam_frame.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/276243/4d2d58d3-911f-73ce-1529-f8898655adf6.jpeg)

Recommended Posts

Desktop: OpenCV manual a take picture
Desktop: OpenCV take a picture
Desktop: OpenCV take a clear picture
Desktop: OpenCV merge Picture
Desktop: OpenCV Threshold
Desktop: OpenCV BilateralFilterBlur
Desktop: OpenCV Dilate
Desktop: OpenCV Affine
Desktop: OpenCV Emboss
Desktop: OpenCV CLAHE
Desktop: OpenCV Ellipse2Poly
Desktop: OpenCV HDR
Desktop: OpenCV Denoise
Desktop: OpenCV Concat
Desktop: OpenCV Mosaic
Desktop: OpenCV Erode
Desktop: OpenCV Denoise
Desktop: OpenCV Rectangle
Desktop: OpenCV Watershed
Desktop: OpenCV Inpaint
Desktop: OpenCV NormalizeBlur
Desktop: OpenCV StereoSGBM
Desktop: OpenCV Spot
Desktop: OpenCV Canny
Desktop: OpenCV Denoise3
Desktop: OpenCV Histogram
Desktop: OpenCV Dft
Desktop: OpenCV Decolor
Desktop: OpenCV FaceDetector
Desktop: OpenCV Denoise2
Desktop: OpenCV StereoBM
Desktop: OpenCV Illumination Change
Desktop: OpenCV Add WaterMark
Desktop: OpenCV Fill ConvexPoly
Desktop: OpenCV Grab Cut
Desktop: OpenCV Sharpness Gui
Desktop: OpenCV Color Change
Desktop: OpenCV Freichennel Filter
Desktop: OpenCV Adaptive Threshold
Desktop: OpenCV Draw Circle
Desktop: OpenCV Fill Poly
Desktop: OpenCV Mean Filter
Desktop: OpenCV Java Repository
Desktop: OpenCV Sobel Filter2
Desktop: OpenCV pyrMeanShift Filter
Desktop: OpenCV Virtual Piano
Desktop: OpenCV Scharr Filter
Desktop: Opencv webcam preview
Desktop: OpenCV Median Blur
Desktop: OpenCV Add Broad
Desktop: OpenCV Robinson Filter
Desktop: OpenCV record video
Desktop: OpenCV Texture Flatting
Desktop: OpenCV Flood Fill
Desktop: OpenCV color space
Desktop: OpenCV SqrBox Filter
Desktop: OpenCV resize image
Desktop: OpenCV Pencil Sketch
Desktop: OpenCV Color Map
Desktop: OpenCV Seamless Clone
Desktop: OpenCV Sobel Filter