java 调用摄像头拍照
<dependency>
<groupId>com.github.sarxos</groupId>
<artifactId>webcam-capture</artifactId>
<version>0.3.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.humble/humble-video-all -->
<dependency>
<groupId>io.humble</groupId>
<artifactId>humble-video-all</artifactId>
<version>0.3.0</version>
</dependency>
package com.sys.springbootcaputre;
import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamPanel;
import com.github.sarxos.webcam.WebcamResolution;
import lombok.extern.slf4j.Slf4j;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
/**
* @ClassName test
* @Author sys
* @Date 2020/11/4 16:49
* @Description //TODO
* @Version
**/
@Slf4j
public class test {
public static void main(String[] args) {
Webcam webcam = Webcam.getDefault();
webcam.setViewSize(WebcamResolution.VGA.getSize());
WebcamPanel panel = new WebcamPanel(webcam);
panel.setFPSDisplayed(true);
panel.setDisplayDebugInfo(true);
panel.setImageSizeDisplayed(true);
panel.setMirrored(true);
JFrame window = new JFrame("Test webcam panel");
JButton b = new JButton(new String("拍照".getBytes(), Charset.defaultCharset()));
b.setPreferredSize(new Dimension(50,30));
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Webcam webcamc = Webcam.getDefault();
webcamc.open();
BufferedImage image = webcamc.getImage();
// save image to PNG file
try {
ImageIO.write(image, "PNG", new File("e:/cap/testbut.png"));
} catch (IOException ex) {
log.info("拍照失败");
ex.printStackTrace();
}
log.info("拍照成功");
}
});
panel.add(b);
window.add(panel);
window.setResizable(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.pack();
//webcam.open();
window.setVisible(true);
// Thread t = new Thread(() -> {
// Webcam webcamc = Webcam.getDefault();
// webcamc.open();
// try {
// // get image //保存图片
// for(int i=0;i<600;i++) {
// BufferedImage image = webcamc.getImage();
// // save image to PNG file
// ImageIO.write(image, "PNG", new File("e:/cap/test"+i+".png"));
// Thread.sleep(1000);//1s
// }
// } catch (Exception e) {
// e.printStackTrace();
// }
//
// });
// t.start();
}
}
https://github.com/sarxos/webcam-capture