🙌
SpringBootからWebカメラを操作するサンプル
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>
参考記事
Udemyで講座を公開中!
Discussion