[JAVA] Desktop: Opencv webcam preview

Goal
Show Camera Preview

OpenCV_webcam3.java


import javax.swing.JFrame;

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

public class OpenCV_webcam3 {

	static {
		System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
	}
	
	public static void main(String[] args) {
	
		VideoCapture camera = null;
		camera =  initWebcam();
		JFrame frame1 = new JFrame("Show image");
		frame1.setTitle("從 webcam 讀 image image arrival Java swing window");
		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(camera.isOpened()) {
			Mat webcam_frame = new Mat();
			camera.read(webcam_frame);
			frame1.setSize(webcam_frame.width(),webcam_frame.height());
			
			while (true) {
				camera.read(webcam_frame);
				panel1.setimagewithMat(webcam_frame);
				frame1.repaint();
			}
			
		}else {
			System.out.print("Error");
		}
		
	}
	
	
	private static VideoCapture initWebcam() {
		VideoCapture cameraCapture = new VideoCapture();
		cameraCapture.open(0);
		
		return cameraCapture;
	}
}
Result
![camera_preview.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/276243/55e97568-4a77-6eb9-8d9d-ae384784d816.jpeg)

Recommended Posts

Desktop: Opencv webcam preview
Desktop: OpenCV webcam preview tutorial
Desktop: OpenCV FaceDetector By WebCam
Desktop: OpenCV Show Stylization By WebCam
Desktop: OpenCV Threshold
Desktop: OpenCV BilateralFilterBlur
Desktop: OpenCV Expand
Desktop: OpenCV Affine
Desktop: OpenCV Emboss
Desktop: OpenCV CLAHE
Desktop: OpenCV Ellipse2Poly
Desktop: OpenCV HDR
Desktop: OpenCV Polylines
Desktop: OpenCV Denoise
Desktop: OpenCV Sharpness
Desktop: OpenCV Concat
Desktop: OpenCV OpenCV_SalonUseBlurAddWeighted
Desktop: OpenCV Mosaic
Desktop: OpenCV Erode
Desktop: OpenCV Denoise
Desktop: OpenCV Rectangle
Desktop: OpenCV Watershed
Desktop: OpenCV Text
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 Find Chessboard Corners By WebCam
Desktop: OpenCV Kirsch Filter
Desktop: OpenCV Laplacian Filter 2
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 Adaptive Threshold
Desktop: OpenCV Draw Circle
Desktop: OpenCV Fill Poly
Desktop: OpenCV Mean Filter
Desktop: OpenCV Java Repository
Desktop: OpenCV pyrMeanShift Filter
Desktop: OpenCV OpticalFlow PyrLK
Desktop: OpenCV Virtual Piano
Desktop: OpenCV merge Picture
Desktop: OpenCV Scharr Filter
Desktop: OpenCV Laplacian Filter
Desktop: OpenCV Median Blur
Desktop: OpenCV Add Broad
Desktop: OpenCV Robinson Filter
Desktop: OpenCV record video
Desktop: OpenCV Texture Flatting
Desktop: OpenCV Flood Fill