🙌

SpringBootからWebカメラを操作するサンプル

2023/10/02に公開

build.gradleに以下を追加

//Webカメラの映像を表示するライブラリ
// https://mvnrepository.com/artifact/com.github.sarxos/webcam-capture
implementation group: 'com.github.sarxos', name: 'webcam-capture', version: '0.3.12'
import javax.imageio.ImageIO;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.github.sarxos.webcam.Webcam;

@Controller
@RequestMapping(value = "/webCamera")
public class QRWebCameraController {

	@GetMapping
	public String index(Model model) {
		Webcam webcam = Webcam.getDefault();
		webcam.open();
		BufferedImage image = webcam.getImage();
		try {
			ImageIO.write(image, "JPG", new File("C:\\image\\test.jpg"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		model.addAttribute("fileName", "image/test.jpg");
		return "QRCodeData";
	}
}

QRCodeData.html

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>QRコード</title>
</head>
<body>
	<img th:src="${fileName}">
	<p>[[${fileName}]]</p>
</body>
</html>

参考記事
https://www.tabnine.com/code/java/methods/com.github.sarxos.webcam.Webcam/open




https://zenn.dev/codek2/books/d95901da7e8cd1

https://zenn.dev/codek2/articles/1bdbccfaf9b075

Udemyで講座を公開中!
https://zenn.dev/codek2/articles/e9e44f3e0023fb

Discussion