JavaCV调用摄像头并保存图片

首先要引入javaCV的Maven依赖


    org.bytedeco
    javacv-platform
    1.4.3

在引入该依赖后javaCV会加载相应平台的jar包,打开相应平台的jar包可以看到其中包含相应的dll文件。
 

package com.wxj233.bean;

import org.bytedeco.javacv.FrameGrabber.Exception;
import org.bytedeco.javacv.OpenCVFrameGrabber;

/**
 * @version 0.0.1
 * @since 2018/12/18
 * @author wxj233
 * 摄像头参数设置
 */
public class Camera {

	/**
	 * 是否存储图片
	 * @author wxj233
	 */
	public static enum Img{
		save,
		nosave
	}
	
	/**
	 * 是否正常启动摄像头 true:正常启动/false:异常启动
	 */
	public static boolean InitCamera;
	
	/**
	 * 摄像头资源
	 */
	public static OpenCVFrameGrabber grabber;
	
	/**
	 * 是否保存图片,默认不保存
	 */
	public static Img saveImage = Img.nosave;
	
	/**
	 * 开始摄像头
	 * @throws Exception 摄像头初始化失败!
	 */
	public static void StartCamera() throws Exception {
			grabber.start();
	}
	
	/**
	 * 停止摄像头
	 * @throws Exception 摄像头关闭异常!
	 */
	public static void StopCamera() throws Exception {
			grabber.stop();
	}
}
package com.wxj233.System.init;

import javax.annotation.PostConstruct;


import org.bytedeco.javacv.FrameGrabber.Exception;
import org.bytedeco.javacv.OpenCVFrameGrabber;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.wxj233.bean.Camera;
import com.wxj233.serviceImp.SyLogServiceImp;

/**
 * @version 0.0.1
 * @since 2018/12/18
 * @author wxj233
 *
 */
@Component
public class CameraTimerInit {

	@Autowired
	SyLogServiceImp syLogService;
	
	/**
	 * 初始化摄像头
	 */
	@PostConstruct
	void CameraInit(){
		Camera.grabber = new OpenCVFrameGrabber(0);
		try {
			Camera.StartCamera();
			Camera.InitCamera = true; //摄像头正常启动
			syLogService.info(CameraTimerInit.class, "初始化摄像头!");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			Camera.InitCamera = false;  //摄像头启动失败
			syLogService.info(CameraTimerInit.class, "请检查摄像头是否正常或驱动是否更新!");
			//e.printStackTrace();
		}
		
	}
	
}
package com.wxj233.Timer;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicLong;
import org.bytedeco.javacpp.opencv_core.Mat;
import org.bytedeco.javacpp.opencv_imgcodecs;
import org.bytedeco.javacv.FrameGrabber.Exception;
import org.bytedeco.javacv.OpenCVFrameConverter;
import org.bytedeco.javacv.OpenCVFrameGrabber;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.wxj233.bean.Camera;
import com.wxj233.util.SystemUtilServiceImp;

/**
 * @version 0.0.1
 * @since 2018/12/18
 * @author wxj233
 * 开启摄像头抓拍
 */
@Component
public class CameraTimer {

	@Autowired
	SystemUtilServiceImp systemUtilService;
	
	//原子类产生不重复数据
	AtomicLong generator = new AtomicLong();
	
	public static OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage();
	
	@Scheduled(cron = "0/5 * * * * ?")
	public void getPhoto() {
		//System.out.println("抓拍!");
		OpenCVFrameGrabber grabber = Camera.grabber;
		try {
			if(Camera.InitCamera) {
				Mat mat = converter.convertToMat(grabber.grabFrame());
				
				if(Camera.saveImage == Camera.Img.save) {
				//SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");javaCV不支持中文路径
				SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
				Date date = new Date();
				String imgname = format.format(date) + "Num" + String.valueOf(generator.incrementAndGet());
				opencv_imgcodecs.imwrite(systemUtilService.getWebPath("/WEB-INF/CameraPhoto/") + imgname +".jpg", mat);
				}
			}
			} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

相关链接:

javacv开发详解之1:调用本机摄像头视频(建议使用javaCV最新版本)

javacv实现人脸检测

JAVA中通过JavaCV实现跨平台视频/图像处理-调用摄像头

javaCV下载链接:https://www.softpedia.com/get/Programming/Other-Programming-Files/JavaCV.shtml

JavaCV读写包含中文路径的图片

 

 

你可能感兴趣的:(java,J2EE)