歡迎您光臨本站 註冊首頁

Java調用計算機攝像頭拍照實現過程解析

←手機掃碼閱讀     hongdian2012 @ 2020-06-08 , reply:0

Java調用計算機攝像頭照相(Rest API的頁面操作)

使用開源組件webcam-capture:https://github.com/sarxos/webcam-capture

項目源碼GitHub:https://github.com/muphy1112/RuphyRecorder

本例子使用基於Java rest API的頁面操作,方便遠程拍照

新建Spring Boot項目

pop.xml

  4.0.0org.springframework.bootspring-boot-starter-parent2.2.5.RELEASEme.muphyrecorder0.0.1-SNAPSHOTrecorderDemo project for Spring Boot1.8org.springframework.bootspring-boot-starter-webcom.github.sarxoswebcam-capture0.3.12org.springframework.bootspring-boot-starter-testtestorg.junit.vintagejunit-vintage-engineorg.springframework.bootspring-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 "" + msg + "" +          "點擊查看所有照片" +          "返回首頁";    }  }

 

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);    }    }

 

當前電腦沒有攝像頭,因此是正常的

                                                       

   


[hongdian2012 ] Java調用計算機攝像頭拍照實現過程解析已經有263次圍觀

http://coctec.com/docs/java/show-post-237447.html