openCV播放视频或调用摄像头
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.videoio.VideoCapture;
import org.opencv.imgproc.Imgproc;
public class ShowVedio {
static{
System.load( "G:\\OpenCV\\opencv\\build\\java\\x64\\opencv_java430.dll");
}
public static void main(String[] args) {
VideoCapture camera=new VideoCapture();
camera.release();
camera.open(0,700);//设置700才行
//camera.open("G:\\124.avi");//读取视频
while(!camera.isOpened()) {
System.out.println("摄像头未打开");
try{
Thread.sleep(1000);
}
catch(InterruptedException e){
e.printStackTrace();
}
}
System.out.println("摄像头成功开启");
System.out.println("摄像机名字:"+camera.getBackendName());
Mat frame=new Mat();
while(true){
camera.read(frame);
HighGui.imshow("OpenCV摄像头", frame);
HighGui.waitKey(5);
try{
Thread.sleep(5);
}
catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
若读取视频,必须将opencv目录下build/bin目录下的binopencv_videoio_ffmpeg430_64.dll那个dll文件复制到C:windows/system32。
apiPreference 接口的设备ID对应:
enum VideoCaptureAPIs {
CAP_ANY = 0, //!< Auto detect == 0
CAP_VFW = 200, //!< Video For Windows (platform native)
CAP_V4L = 200, //!< V4L/V4L2 capturing support via libv4l
CAP_V4L2 = CAP_V4L, //!< Same as CAP_V4L
CAP_FIREWIRE = 300, //!< IEEE 1394 drivers
CAP_FIREWARE = CAP_FIREWIRE, //!< Same as CAP_FIREWIRE
CAP_IEEE1394 = CAP_FIREWIRE, //!< Same as CAP_FIREWIRE
CAP_DC1394 = CAP_FIREWIRE, //!< Same as CAP_FIREWIRE
CAP_CMU1394 = CAP_FIREWIRE, //!< Same as CAP_FIREWIRE
CAP_QT = 500, //!< QuickTime
CAP_UNICAP = 600, //!< Unicap drivers
CAP_DSHOW = 700, //!< DirectShow (via videoInput)
CAP_PVAPI = 800, //!< PvAPI, Prosilica GigE SDK
CAP_OPENNI = 900, //!< OpenNI (for Kinect)
CAP_OPENNI_ASUS = 910, //!< OpenNI (for Asus Xtion)
CAP_ANDROID = 1000, //!< Android - not used
CAP_XIAPI = 1100, //!< XIMEA Camera API
CAP_AVFOUNDATION = 1200, //!< AVFoundation framework for iOS (OS X Lion will have the same API)
CAP_GIGANETIX = 1300, //!< Smartek Giganetix GigEVisionSDK
CAP_MSMF = 1400, //!< Microsoft Media Foundation (via videoInput)
CAP_WINRT = 1410, //!< Microsoft Windows Runtime using Media Foundation
CAP_INTELPERC = 1500, //!< Intel Perceptual Computing SDK
CAP_OPENNI2 = 1600, //!< OpenNI2 (for Kinect)
CAP_OPENNI2_ASUS = 1610, //!< OpenNI2 (for Asus Xtion and Occipital Structure sensors)
CAP_GPHOTO2 = 1700, //!< gPhoto2 connection
CAP_GSTREAMER = 1800, //!< GStreamer
CAP_FFMPEG = 1900, //!< Open and record video file or stream using the FFMPEG library
CAP_IMAGES = 2000, //!< OpenCV Image Sequence (e.g. img_%02d.jpg)
CAP_ARAVIS = 2100, //!< Aravis SDK
CAP_OPENCV_MJPEG = 2200, //!< Built-in OpenCV MotionJPEG codec
CAP_INTEL_MFX = 2300, //!< Intel MediaSDK
CAP_XINE = 2400, //!< XINE engine (Linux)
};