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

 更新时间:2020年05月26日 10:52:46   作者:明月心~  
这篇文章主要介绍了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);
  }

}

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Java日常练习题,每天进步一点点(54)

    Java日常练习题,每天进步一点点(54)

    下面小编就为大家带来一篇Java基础的几道练习题(分享)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧,希望可以帮到你
    2021-08-08
  • SpringBoot实现人脸识别等多种登录方式

    SpringBoot实现人脸识别等多种登录方式

    本文主要介绍了SpringBoot实现人脸识别等多种登录方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-05-05
  • 引入QQ邮箱发送验证码进行安全校验功能实现

    引入QQ邮箱发送验证码进行安全校验功能实现

    最近遇到这样的需求用户输入自己的邮箱,点击获取验证码,后台会发送一封邮件到对应邮箱中,怎么实现呢?下面小编给大家带来了引入QQ邮箱发送验证码进行安全校验功能,需要的朋友可以参考下
    2023-02-02
  • Mybatis 多对一查询的实现方法

    Mybatis 多对一查询的实现方法

    这篇文章主要介绍了Mybatis 多对一查询,本文通过场景分析示例代码相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-02-02
  • IDEA插件推荐之Maven-Helper的教程图解

    IDEA插件推荐之Maven-Helper的教程图解

    这篇文章主要介绍了IDEA插件推荐之Maven-Helper的相关知识,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考
    2020-07-07
  • Java实现的剪刀石头布游戏示例

    Java实现的剪刀石头布游戏示例

    这篇文章主要介绍了Java实现的剪刀石头布游戏,涉及java随机数生成及逻辑判定等相关操作技巧,需要的朋友可以参考下
    2017-12-12
  • Spring Batch读取txt文件并写入数据库的方法教程

    Spring Batch读取txt文件并写入数据库的方法教程

    这篇文章主要给大家介绍了Spring Batch读取txt文件并写入数据库的方法,SpringBatch 是一个轻量级、全面的批处理框架。这里我们用它来实现文件的读取并将读取的结果作处理,处理之后再写入数据库中的功能。需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-04-04
  • SpringBoot自定义Starter的教程指南

    SpringBoot自定义Starter的教程指南

    SpringBoot的Starter自动配置机制极大地简化了依赖管理和应用配置,使得开发者可以以最少的配置快速启动和运行Spring应用,有时,标准的Starter可能无法满足特定需求,需要创建自定义Starter,所以本文给大家介绍了SpringBoot自定义Starter的教程指南
    2024-11-11
  • Spring Boot 定制与优化内置的Tomcat容器实例详解

    Spring Boot 定制与优化内置的Tomcat容器实例详解

    本文主要记录对内置容器优化和定制的方式,用于自己加深对SpringBoot理解。本文给大家介绍的非常详细,具有参考借鉴价值,需要的朋友参考下吧
    2017-12-12
  • javaDSL简单实现示例分享

    javaDSL简单实现示例分享

    DSL领域定义语言,用来描述特定领域的特定表达。比如画图从起点到终点;路由中的从A到B。这是关于画图的一个简单实现
    2014-03-03

最新评论