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

在这里插入图片描述

  • 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 ):
nameLinkRemark
Project homepagehttps://github.com/zq2599/blog_demosThe project's homepage on GitHub
git repository address (https)https://github.com/zq2599/blog_demos.gitThe warehouse address of the source code of the project, https protocol
git repository address (ssh)git@github.com:zq2599/blog_demos.gitThe 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:

在这里插入图片描述

You are not alone, Xinchen Original is with you all the way

https://github.com/zq2599/blog_demos


程序员欣宸
147 声望24 粉丝

热爱Java和Docker