1. Overview
1.概述
Usually, Java doesn’t provide easy access to the computer hardware. That’s why we might find it tough to access the webcam using Java.
通常情况下,Java并不提供对计算机硬件的简单访问。这就是为什么我们可能发现使用Java访问网络摄像头很困难。
In this tutorial, we’ll explore a few Java libraries that allow us to capture images by accessing the webcam.
在本教程中,我们将探讨一些允许我们通过访问网络摄像头来捕捉图像的Java库。
2. JavaCV
2.JavaCV
First, we’ll examine the javacv library. This is Bytedeco‘s Java implementation of the OpenCV computer vision library.
首先,我们将检查javacv库。这是Bytedeco的OpenCV计算机视觉库的Java实现。
Let’s add the latest javacv-platform Maven dependency to our pom.xml:
让我们把最新的javacv-platformMaven依赖性添加到我们的pom.xml。
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.5.5</version>
</dependency>
Similarly, when using Gradle, we can add the javacv-platform dependency in the build.gradle file:
同样,在使用Gradle时,我们可以在build.gradle文件中添加javacv-platform依赖。
compile group: 'org.bytedeco', name: 'javacv-platform', version: '1.5.5'
Now that we’re ready with the setup, let’s use the OpenCVFrameGrabber class to access the webcam and capture a frame:
现在我们已经准备好了设置,让我们使用OpenCVFrameGrabber类来访问网络摄像头并捕获一帧。
FrameGrabber grabber = new OpenCVFrameGrabber(0);
grabber.start();
Frame frame = grabber.grab();
Here, we’ve passed the device number as 0, pointing to the default webcam of the system. However, if we have more than one camera available, then the second camera is accessible at 1, the third one at 2, and so on.
在这里,我们将设备号传递为0,指向系统的默认网络摄像头。然而,如果我们有一个以上的摄像头可用,那么第二个摄像头可以在1处访问,第三个在2处,以此类推。
Then, we can use the OpenCVFrameConverter to convert the captured frame into an image. Also, we’ll save the image using the cvSaveImage method of the opencv_imgcodecs class:
然后,我们可以使用OpenCVFrameConverter来将捕获的帧转换成图像。同时,我们将使用opencv_imgcodecs类的cvSaveImage方法来保存图像。
OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage();
IplImage img = converter.convert(frame);
opencv_imgcodecs.cvSaveImage("selfie.jpg", img);
Last, we can use the CanvasFrame class to display the captured frame:
最后,我们可以使用CanvasFrame类来显示捕获的框架。
CanvasFrame canvas = new CanvasFrame("Web Cam");
canvas.showImage(frame);
Let’s examine a full solution that accesses the webcam, captures an image, displays the image in a frame, and closes the frame automatically after two seconds:
让我们来看看一个完整的解决方案,它可以访问网络摄像头,捕捉图像,在一个框架中显示图像,并在两秒后自动关闭框架。
CanvasFrame canvas = new CanvasFrame("Web Cam");
canvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FrameGrabber grabber = new OpenCVFrameGrabber(0);
OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage();
grabber.start();
Frame frame = grabber.grab();
IplImage img = converter.convert(frame);
cvSaveImage("selfie.jpg", img);
canvas.showImage(frame);
Thread.sleep(2000);
canvas.dispatchEvent(new WindowEvent(canvas, WindowEvent.WINDOW_CLOSING));
3. webcam-capture
3.网络摄像头-捕捉
Next, we’ll examine the webcam-capture library that allows using the webcam by supporting multiple capturing frameworks.
接下来,我们将研究 webcam-capture库,它可以通过支持多种捕获框架来使用网络摄像头。
First, let’s add the latest webcam-capture Maven dependency to our pom.xml:
首先,让我们把最新的webcam-capture Maven依赖性添加到我们的pom.xml。
<dependency>
<groupId>com.github.sarxos</groupId>
<artifactId>webcam-capture</artifactId>
<version>0.3.12</version>
</dependency>
Or, we can add the webcam-capture in the build.gradle for a Gradle project:
或者,我们可以在Gradle项目的build.gradle中添加webcam-capture。
compile group: 'com.github.sarxos', name: 'webcam-capture', version: '0.3.12'
Then, let’s write a simple example to capture an image using the Webcam class:
然后,让我们写一个简单的例子,使用Webcam类捕获图像。
Webcam webcam = Webcam.getDefault();
webcam.open();
BufferedImage image = webcam.getImage();
ImageIO.write(image, ImageUtils.FORMAT_JPG, new File("selfie.jpg"));
Here, we accessed the default webcam to capture the image, and then we saved the image to a file.
在这里,我们访问了默认的网络摄像头来捕捉图像,然后我们将图像保存到一个文件中。
Alternatively, we can use the WebcamUtils class to capture an image:
另外,我们可以使用WebcamUtils类来捕获图像。
WebcamUtils.capture(webcam, "selfie.jpg");
Also, we can use the WebcamPanel class to display a captured image in a frame:
另外,我们可以使用WebcamPanel类来在一个帧中显示捕获的图像。
Webcam webcam = Webcam.getDefault();
webcam.setViewSize(WebcamResolution.VGA.getSize());
WebcamPanel panel = new WebcamPanel(webcam);
panel.setImageSizeDisplayed(true);
JFrame window = new JFrame("Webcam");
window.add(panel);
window.setResizable(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.pack();
window.setVisible(true);
Here, we set VGA as the view size of the webcam, created the JFrame object, and added the WebcamPanel component to the frame.
在这里,我们将VGA作为网络摄像头的视图尺寸,创建了JFrame对象,并将WebcamPanel组件添加到框架中。
4. Marvin Framework
4.马文框架
At last, we’ll explore the Marvin framework to access the webcam and capturing an image.
最后,我们将探索Marvin框架来访问网络摄像头和捕捉图像。
As usual, we’ll add the latest marvin dependency to our pom.xml:
像往常一样,我们将把最新的marvin依赖性添加到我们的pom.xml。
<dependency>
<groupId>com.github.downgoon</groupId>
<artifactId>marvin</artifactId>
<version>1.5.5</version>
</dependency>
Or, for a Gradle project, we’ll add the marvin dependency in the build.gradle file:
或者,对于Gradle项目,我们将在build.gradle文件中加入marvin依赖。
compile group: 'com.github.downgoon', name: 'marvin', version: '1.5.5'
Now that setup is ready, let’s use the MarvinJavaCVAdapter class to connect to the default webcam by providing 0 for the device number:
现在设置好了,让我们使用MarvinJavaCVAdapter类,通过提供0的设备号连接到默认网络摄像头。
MarvinVideoInterface videoAdapter = new MarvinJavaCVAdapter();
videoAdapter.connect(0);
Next, we can use the getFrame method to capture the frame, and then we’ll save the image using the saveImage method of the MarvinImageIO class:
接下来,我们可以使用getFrame方法来捕获帧,然后我们将使用MarvinImageIO类的saveImage方法保存图像。
MarvinImage image = videoAdapter.getFrame();
MarvinImageIO.saveImage(image, "selfie.jpg");
Also, we can use the MarvinImagePanel class to display an image in a frame:
另外,我们可以使用MarvinImagePanel类来在一个框架中显示一个图像。
MarvinImagePanel imagePanel = new MarvinImagePanel();
imagePanel.setImage(image);
imagePanel.setSize(800, 600);
imagePanel.setVisible(true);
5. Conclusion
5.总结
In this short article, we examined a few Java libraries that provide easy access to the webcam.
在这篇短文中,我们研究了几个可以轻松访问网络摄像头的Java库。
First, we explored the javacv-platform library that provides a Java implementation of the OpenCV project. Then, we saw the example implementation of the webcam-capture library to capture images using a webcam. Last, we took a look at the simple examples to capture the images using the Marvin framework.
首先,我们探索了javacv-platform库,它提供了OpenCV项目的Java实现。然后,我们看到了webcam-capture库的实例实现,用webcam捕捉图像。最后,我们看了一下使用Marvin框架捕捉图像的简单例子。
As usual, all the code implementations are available over on GitHub.
像往常一样,所有的代码实现都可以在GitHub上找到,。