Java调用计算机摄像头拍照实现过程解析

网友投稿 495 2022-12-06


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

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.2.5.RELEASE

me.muphy

recorder

0.0.1-SNAPSHOT

recorder

Demo project for Spring Boot

1.8

org.springframework.boot

spring-boot-starter-web

com.github.sarxos

webcam-capture

0.3.12

org.springframework.boot

spring-boot-starter-test

test

org.junit.vintage

junit-vintage-engine

org.springframework.boot

spring-boot-maven-plugin

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.2.5.RELEASE

me.muphy

recorder

0.0.1-SNAPSHOT

recorder

Demo project for Spring Boot

1.8

org.springframework.boot

spring-boot-starter-web

com.github.sarxos

webcam-capture

0.3.12

org.springframework.boot

spring-boot-starter-test

test

org.junit.vintage

junit-vintage-engine

org.springframework.boot

spring-boot-maven-plugin

server.port=8080

#保存根路径

record.path=E:/workspace/share/

index.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 "

"点击查看所有照片" +

"返回首页

}

}

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.CbGhYHspringframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class CameraApplication {

public static void main(String[] args) {

SpringApplication.run(CameraApplication.class, args);

}

}

当前电脑没有摄像头,因此是正常的


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java WindowBuilder 安装及基本使用的教程
下一篇:Java ServletContext对象用法解析
相关文章

 发表评论

暂时没有评论,来抢沙发吧~