Java調(diào)用計(jì)算機(jī)攝像頭拍照實(shí)現(xiàn)過(guò)程解析
Java調(diào)用計(jì)算機(jī)攝像頭照相(Rest API的頁(yè)面操作)
使用開(kāi)源組件webcam-capture:https://github.com/sarxos/webcam-capture
項(xiàng)目源碼GitHub:https://github.com/muphy1112/RuphyRecorder
本例子使用基于Java rest API的頁(yè)面操作,方便遠(yuǎn)程拍照
新建Spring Boot項(xiàng)目

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>莫非照相機(jī)</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\" >點(diǎn)擊查看所有照片</a></span>" +
"<span style=\"margin-left: 20px;\"><a href=\"/\" >返回首頁(yè)</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 "沒(méi)有找到攝像設(shè)備!";
}
String filePath = downloadPath + "/picture/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date());
File path = new File(filePath);
if (!path.exists()) {//如果文件不存在,則創(chuàng)建該目錄
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);
}
}
當(dāng)前電腦沒(méi)有攝像頭,因此是正常的

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(54)
下面小編就為大家?guī)?lái)一篇Java基礎(chǔ)的幾道練習(xí)題(分享)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望可以幫到你2021-08-08
SpringBoot實(shí)現(xiàn)人臉識(shí)別等多種登錄方式
本文主要介紹了SpringBoot實(shí)現(xiàn)人臉識(shí)別等多種登錄方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
引入QQ郵箱發(fā)送驗(yàn)證碼進(jìn)行安全校驗(yàn)功能實(shí)現(xiàn)
最近遇到這樣的需求用戶輸入自己的郵箱,點(diǎn)擊獲取驗(yàn)證碼,后臺(tái)會(huì)發(fā)送一封郵件到對(duì)應(yīng)郵箱中,怎么實(shí)現(xiàn)呢?下面小編給大家?guī)?lái)了引入QQ郵箱發(fā)送驗(yàn)證碼進(jìn)行安全校驗(yàn)功能,需要的朋友可以參考下2023-02-02
Mybatis 多對(duì)一查詢的實(shí)現(xiàn)方法
這篇文章主要介紹了Mybatis 多對(duì)一查詢,本文通過(guò)場(chǎng)景分析示例代碼相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02
Spring Batch讀取txt文件并寫入數(shù)據(jù)庫(kù)的方法教程
這篇文章主要給大家介紹了Spring Batch讀取txt文件并寫入數(shù)據(jù)庫(kù)的方法,SpringBatch 是一個(gè)輕量級(jí)、全面的批處理框架。這里我們用它來(lái)實(shí)現(xiàn)文件的讀取并將讀取的結(jié)果作處理,處理之后再寫入數(shù)據(jù)庫(kù)中的功能。需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-04-04
Spring Boot 定制與優(yōu)化內(nèi)置的Tomcat容器實(shí)例詳解
本文主要記錄對(duì)內(nèi)置容器優(yōu)化和定制的方式,用于自己加深對(duì)SpringBoot理解。本文給大家介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-12-12
javaDSL簡(jiǎn)單實(shí)現(xiàn)示例分享
DSL領(lǐng)域定義語(yǔ)言,用來(lái)描述特定領(lǐng)域的特定表達(dá)。比如畫圖從起點(diǎn)到終點(diǎn);路由中的從A到B。這是關(guān)于畫圖的一個(gè)簡(jiǎn)單實(shí)現(xiàn)2014-03-03

