java使用javacv库实现USB摄像头拍照

84 阅读1分钟

导入maven依赖

<dependency>
    <groupId>org.bytedeco</groupId>
    <artifactId>javacv</artifactId>
    <version>1.5.5</version>
</dependency>

<!-- JavaCV platform -->
<dependency>
    <groupId>org.bytedeco</groupId>
    <artifactId>javacv-platform</artifactId>
    <version>1.5.5</version>
</dependency>

拍照

```
//第一个摄像头
OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);

try {

    grabber.start();
    Frame frame = grabber.grab();
    if (frame != null) {

        OpenCVFrameConverter.ToMat converterToMat = new OpenCVFrameConverter.ToMat();
        Mat mat = converterToMat.convertToMat(frame);

        opencv_imgcodecs.imwrite(imagePath, mat);
        grabber.stop();
    }
} catch (FrameGrabber.Exception e) {
    e.printStackTrace();
}