Java调用计算机摄像头拍照实现过程解析
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://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>me.muphy</groupId> <artifactId>recorder</artifactId> <version>0.0.1-SNAPSHOT</version> <name>recorder</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.github.sarxos</groupId> <artifactId>webcam-capture</artifactId> <version>0.3.12</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
server.port=8080
#保存根路径
record.path=E:/workspace/share/
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>莫非照相机</title> </head> <body> <div> <div><span><a href="/tp" rel="external nofollow" >拍照</a></span></div> </div> </body> </html>
CameraController.java
package me.muphy.camera; import me.muphy.servicce.CameraService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class CameraController { @Autowired private CameraService cameraService; @GetMapping("/tp") public String takePictures(String[] args) { String msg = cameraService.takePictures(); return "<div><span>" + msg + "</span></div><div>" + "<span><a href=\"/ll?d=/picture\" >点击查看所有照片</a></span>" + "<span style=\"margin-left: 20px;\"><a href=\"/\" >返回首页</a></span></div>"; } }
CameraService.java
package me.muphy.servicce; import com.github.sarxos.webcam.Webcam; import com.github.sarxos.webcam.WebcamResolution; import com.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; @Service public 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; @SpringBootApplication public class CameraApplication { public static void main(String[] args) { SpringApplication.run(CameraApplication.class, args); } }
当前电脑没有摄像头,因此是正常的
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
SpringCloudAlibaba Nacos开启鉴权解决跳过登录页面问题
对于Nacos,如果需要开启权限控制,可以在 Nacos 控制台上进行配置,本文主要介绍了SpringCloudAlibaba Nacos开启鉴权解决跳过登录页面问题,感兴趣的可以了解一下2023-10-10一段代码搞懂关于Java中List、Set集合及Map的使用
这篇文章主要介绍了关于Java中List、Set集合及Map的使用及list,set和map三者的区别介绍,非常不错,具有参考借鉴价值,需要的朋友可以参考下2016-08-08java基本教程之常用的实现多线程的两种方式 java多线程教程
下面开始学习“常用的实现多线程的2种方式”:Thread 和 Runnable。之所以说是常用的,是因为通过还可以通过java.util.concurrent包中的线程池来实现多线程2014-01-01mybatisplus where QueryWrapper加括号嵌套查询方式
这篇文章主要介绍了mybatisplus where QueryWrapper加括号嵌套查询方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教。2022-01-01
最新评论