Welcome to my GitHub
(including supporting source code) are classified and summarized here: 161f9d092e1c47 https://github.com/zq2599/blog_demos
Overview of this article
This article is the fourth part of "JavaCV's Camera Actual Combat", and it is also the simplest and easiest one in the entire series. A few lines of code realize the function of capturing pictures from the camera;
coding
- "One of JavaCV's Camera Practices: Basics" The parent class <font color="blue">AbstractCameraApplication</font> has been written in the <font color="red">simple-grab-push</font> project created by the article font>, this article will continue to use the project, just create subclasses to implement those abstract methods
- Before coding, review the basic structure of the parent class, as shown in the figure below, the bold font is each method defined by the parent class, and the red blocks all require subclasses to implement abstract methods, so next, we aim to achieve these three with the local window preview. The red method can be:
- Although the parent class requires that the subclass must implement these three methods: initOutput, output, releaseOutputResource, but in fact only the <font color="blue">output</font> method has code, and the other two are empty methods;
- Create a new file <font color="blue">GrabImageFromCamera.java</font>, which is a subclass of AbstractCameraApplication. Its code is very simple. Next, it will be explained in the order of the above figure.
- Define three member variables, the functions are: specify the storage path of the image file (please adjust it yourself), the image format, and the number of stored images in the current process:
// 图片存储路径的前缀(请根据自己电脑情况调整)
protected String IMAGE_PATH_PREFIX = "E:\\temp\\202111\\28\\camera-"
+ new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())
+ "-";
// 图片格式
private final static String IMG_TYPE = "jpg";
/**
* 当前进程已经存储的图片数量
*/
private int saveNums = 0;
- There is no need to do anything during initialization, and no resources need to be released before the corresponding end, so initOutput and releaseOutputResource are both empty methods:
@Override
protected void initOutput() throws Exception {
// 啥也不用做
}
@Override
protected void releaseOutputResource() {
// 啥也不用做
}
- Next is the output method, which uses the frame object to generate the picture:
@Override
protected void output(Frame frame) throws Exception {
// 图片的保存位置
String imagePath = IMAGE_PATH_PREFIX + (saveNums++) + "." + IMG_TYPE;
// 把帧对象转为Image对象
BufferedImage bufferedImage = converter.getBufferedImage(frame);
// 保存图片
ImageIO.write(bufferedImage, IMG_TYPE, new FileOutputStream(imagePath));
log.info("保存完成:{}", imagePath);
}
- Finally, rewrite the getInterval method to sleep for one second every time a picture is saved:
@Override
protected int getInterval() {
// 表示保存一张图片后会sleep一秒钟
return 1000;
}
- At this point, the snapshot function has been developed, and then write the main method. Note that the parameter <font color="blue">10</font> means that it will continue to execute for 10 seconds:
public static void main(String[] args) {
// 连续十秒执行抓图操作
new GrabImageFromCamera().action(10);
}
- Run the main method and the console output is as follows:
...
08:57:42.393 [main] INFO com.bolingcavalry.grabpush.camera.AbstractCameraApplication - 初始化完成,耗时[8515]毫秒,帧率[30.0],图像宽度[1280],图像高度[720]
08:57:43.110 [main] INFO com.bolingcavalry.grabpush.camera.GrabImageFromCamera - 保存完成:E:\temp\202111\28\camera-20211130085733-0.jpg
08:57:44.155 [main] INFO com.bolingcavalry.grabpush.camera.GrabImageFromCamera - 保存完成:E:\temp\202111\28\camera-20211130085733-1.jpg
08:57:45.193 [main] INFO com.bolingcavalry.grabpush.camera.GrabImageFromCamera - 保存完成:E:\temp\202111\28\camera-20211130085733-2.jpg
08:57:46.243 [main] INFO com.bolingcavalry.grabpush.camera.GrabImageFromCamera - 保存完成:E:\temp\202111\28\camera-20211130085733-3.jpg
08:57:47.287 [main] INFO com.bolingcavalry.grabpush.camera.GrabImageFromCamera - 保存完成:E:\temp\202111\28\camera-20211130085733-4.jpg
08:57:48.348 [main] INFO com.bolingcavalry.grabpush.camera.GrabImageFromCamera - 保存完成:E:\temp\202111\28\camera-20211130085733-5.jpg
08:57:49.430 [main] INFO com.bolingcavalry.grabpush.camera.GrabImageFromCamera - 保存完成:E:\temp\202111\28\camera-20211130085733-6.jpg
08:57:50.479 [main] INFO com.bolingcavalry.grabpush.camera.GrabImageFromCamera - 保存完成:E:\temp\202111\28\camera-20211130085733-7.jpg
08:57:51.547 [main] INFO com.bolingcavalry.grabpush.camera.GrabImageFromCamera - 保存完成:E:\temp\202111\28\camera-20211130085733-8.jpg
08:57:52.551 [main] INFO com.bolingcavalry.grabpush.camera.AbstractCameraApplication - 输出结束
[ WARN:0] global D:\a\javacpp-presets\javacpp-presets\opencv\cppbuild\windows-x86_64\opencv-4.5.3\modules\videoio\src\cap_msmf.cpp (438) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback
Process finished with exit code 0
- Open the directory where the image file is located, as shown below, the image has been successfully generated:
- Looking at one of the details is also in line with expectations:
- So far, we have completed the capture function. Next, please continue to pay attention to Xinchen's original work. The "JavaCV Camera Actual Combat" series will also present more abundant applications;
Source code download
- The complete source code of "Camera Combat of JavaCV" can be downloaded from GitHub. The address and link information are shown in the following table ( https://github.com/zq2599/blog_demos ):
name | Link | Remark |
---|---|---|
Project homepage | https://github.com/zq2599/blog_demos | The project's homepage on GitHub |
git repository address (https) | https://github.com/zq2599/blog_demos.git | The warehouse address of the source code of the project, https protocol |
git repository address (ssh) | git@github.com:zq2599/blog_demos.git | The warehouse address of the project source code, ssh protocol |
- There are multiple folders in this git project. The source code of this article is in the <font color="blue">javacv-tutorials</font> folder, as shown in the red box below:
- There are multiple sub-projects in <font color="blue">javacv-tutorials</font>. The code of "JavaCV Camera Actual" series is in <font color="red"> simple-grab-push </font> Under the project:
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。