Welcome to my GitHub
https://github.com/zq2599/blog_demos
Content: All original articles are categorized and summarized and supporting source code, involving Java, Docker, Kubernetes, DevOPS, etc.;
Overview of this article
- The previous article "One of JavaCV's Camera Practices: Basics" has paved the way for the entire series. The next article will focus on how to use the data from the camera. This article will start with the simplest: local window preview
coding
- parent class <font color="blue">AbstractCameraApplication</font> has been prepared in the <font color="red">simple-grab-push</font> project created by Project, 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:
- Create a new file <font color="blue">PreviewCamera.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.
- First define the member variable previewCanvas of the CanvasFrame type, which is the local window that displays the video frame:
protected CanvasFrame previewCanvas
- Then there is the initialization operation, which is the instantiation and parameter setting of previewCanvas:
@Override
protected void initOutput() {
previewCanvas = new CanvasFrame("摄像头预览", CanvasFrame.getDefaultGamma() / grabber.getGamma());
previewCanvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
previewCanvas.setAlwaysOnTop(true);
}
- Next is the output method, which defines what to do after getting each frame of video data. Here is the display in the local window:
@Override
protected void output(Frame frame) {
// 预览窗口上显示当前帧
previewCanvas.showImage(frame);
}
- The last thing to do before the program exits after the loop that processes the video is over is to close the local window:
@Override
protected void releaseOutputResource() {
if (null!= previewCanvas) {
previewCanvas.dispose();
}
}
- So far, the function of previewing the camera in the local window has been developed, and then write the main method. Note that the parameter <font color="red"> 1000 </font> indicates that the preview duration is 1000 seconds:
public static void main(String[] args) {
new PreviewCamera().action(1000);
}
- Run the main method, as shown in the figure below, the camera works smoothly, and the time watermark in the upper left corner can also be displayed normally (it can be seen that the weather in Shenzhen is good today, you should go out for a walk, not write a blog at home...):
- So far, we have completed the local window preview function. Thanks to the power of JavaCV, the whole process is so easy and pleasant. Next, please continue to pay attention to Xinchen Originals. The "JavaCV Camera Actual" 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) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。