Java调用计算机摄像头照相(Rest API的页面操作)
使用开源组件webcam-capture:https://github.com/sarxos/webcam-capture
项目源码GitHub:https://github.com/muphy1112/RuphyRecorder
本例子使用基于Java rest API的页面操作,方便远程拍照
新建Spring Boot项目
pop.xml
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://.github.sarxos.webcam.WebcamUtils;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Service;import java.io.File;import java.text.SimpleDateFormat;import java.util.Date;@Servicepublic class CameraService { @Value("${download.path:E:/workspace/download/}") private String downloadPath; public String takePictures() { Webcam webcam = Webcam.getDefault(); if (webcam == null) { return "没有找到摄像设备!"; } String filePath = downloadPath + "/picture/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()); File path = new File(filePath); if (!path.exists()) {//如果文件不存在,则创建该目录 path.mkdirs(); } String time = new SimpleDateFormat("yyyMMdd_HHmmss").format(new Date()); File file = new File(filePath + "/" + time + ".jpg"); webcam.setViewSize(WebcamResolution.VGA.getSize()); WebcamUtils.capture(webcam, file); return "拍照成功!"; }}CameraApplication.java
package me.muphy;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class CameraApplication { public static void main(String[] args) { SpringApplication.run(CameraApplication.class, args); }}当前电脑没有摄像头,因此是正常的
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。