Welcome to my GitHub

(including supporting source code) are classified and summarized here: 161f9d092e1c47 https://github.com/zq2599/blog_demos

Overview of this article

在这里插入图片描述

  • 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 ):
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