溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

詳解Java如何實(shí)現(xiàn)調(diào)用計(jì)算機(jī)攝像頭拍照

發(fā)布時(shí)間:2020-07-21 16:31:05 來源:億速云 閱讀:291 作者:小豬 欄目:編程語言

小編這次要給大家分享的是詳解Java如何實(shí)現(xiàn)調(diào)用計(jì)算機(jī)攝像頭拍照,文章內(nèi)容豐富,感興趣的小伙伴可以來了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。

本例子使用基于Java rest API的頁面操作,方便遠(yuǎn)程拍照

新建Spring Boot項(xiàng)目

詳解Java如何實(shí)現(xiàn)調(diào)用計(jì)算機(jī)攝像頭拍照

pop.xml

<&#63;xml version="1.0" encoding="UTF-8"&#63;>
<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&#63;d=/picture\" >點(diǎn)擊查看所有照片</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 "沒有找到攝像設(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)前電腦沒有攝像頭,因此是正常的

詳解Java如何實(shí)現(xiàn)調(diào)用計(jì)算機(jī)攝像頭拍照

看完這篇關(guān)于詳解Java如何實(shí)現(xiàn)調(diào)用計(jì)算機(jī)攝像頭拍照的文章,如果覺得文章內(nèi)容寫得不錯(cuò)的話,可以把它分享出去給更多人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI