Welcome to my GitHub

Here classification and summary of xinchen all original (including supporting source code) : github.com/zq2599/blog…

This paper gives an overview of

  • This article is the fourth “JavaCV camera actual combat”, is also the most simple and easy series of one, a few lines of code to achieve the function of capturing images from the camera;

coding

  • “JavaCV camera one of actual combat: basis” to create simple – grab – had written the superclass AbstractCameraApplication push the project and we continue to use the project, create subclasses implement the abstract methods
  • Before coding, we should review the basic structure of the parent class, as shown in the figure below. The bold is the method defined by the parent class, and the red block is the method that needs to be subclassed to implement the abstract method. Therefore, we should implement the three red methods with the goal of local window preview:

  • Although the parent class requires that subclasses implement the three methods: initOutput, Output, and releaseOutputResource, only the output method actually has code. The other two methods are empty.

  • New file GrabImageFromCamera Java, this is a subclass of AbstractCameraApplication, its code is very simple, the next order, according to the above instructions

  • Define three member variables, the function is: to specify the picture file storage path (please adjust), picture format, the current process has stored the number of pictures:

	// The prefix of the image storage path (please adjust according to your own computer situation)
    protected String IMAGE_PATH_PREFIX = "E:\\temp\\202111\\28\\camera-"
            + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())
            + "-";

    // Image format
    private final static String IMG_TYPE = "jpg";

    /** * The number of images stored by the current process */
    private int saveNums = 0;
Copy the code
  • InitOutput and releaseOutputResource are null methods:
   @Override
    protected void initOutput(a) throws Exception {
        // Do nothing
    }
    
	@Override
    protected void releaseOutputResource(a) {
        // Do nothing
    }
Copy the code
  • Next comes the output method, which uses a frame object to generate an image:
	@Override
    protected void output(Frame frame) throws Exception {
        // Where to save the image
        String imagePath = IMAGE_PATH_PREFIX + (saveNums++) + "." + IMG_TYPE;

        // Convert a frame object to an Image object
        BufferedImage bufferedImage = converter.getBufferedImage(frame);

        // Save the image
        ImageIO.write(bufferedImage, IMG_TYPE, new FileOutputStream(imagePath));

        log.info("Save completed: {}", imagePath);
    }
Copy the code
  • Finally, rewrite the getInterval method to sleep one second for each image saved:
    @Override
    protected int getInterval(a) {
        // Save an image and then sleep for one second
        return 1000;
    }
Copy the code
  • At this point, the capture function is developed, and then write the main method, note that the parameter 10 indicates that the execution continues for 10 seconds:
    public static void main(String[] args) {
        // Perform the capture operation for ten consecutive seconds
        new GrabImageFromCamera().action(10);
    }
Copy the code
  • Run the main method and the console output looks like this:
. 08:57:42. [the main] INFO 393 com. Bolingcavalry. Grabpush. Camera. AbstractCameraApplication - initialization is complete, time-consuming [8515] milliseconds, frame rate [30.0]. Image width [1280], [720] highly 08:57:43, 110 [main] INFO com. Bolingcavalry. Grabpush. Camera. GrabImageFromCamera - save your: E: \ temp \ \ \ camera 28-20211130085733-0. 202111. JPG 08:57:44 155 [main] INFO Com. Bolingcavalry. Grabpush. Camera. GrabImageFromCamera - save complete: E: \ temp \ 202111 \ \ camera 28-20211130085733-1. JPG 08:57:45, 193 [main] INFO Com. Bolingcavalry. Grabpush. Camera. GrabImageFromCamera - save complete: E: \ temp \ 202111 \ \ camera 28-20211130085733-2. JPG 08:57:46, 243 [main] INFO Com. Bolingcavalry. Grabpush. Camera. GrabImageFromCamera - save complete: E: \ temp \ 202111 \ \ camera 28-20211130085733-3. JPG 08:57:47, 287 [main] INFO Com. Bolingcavalry. Grabpush. Camera. GrabImageFromCamera - save complete: E: \ temp \ 202111 \ \ camera 28-20211130085733-4. JPG 08:57:48, 348 [main] INFO Com. Bolingcavalry. Grabpush. Camera. GrabImageFromCamera - save complete: E: \ temp \ 202111 \ \ camera 28-20211130085733-5. JPG 08:57:49, 430 [main] INFO Com. Bolingcavalry. Grabpush. Camera. GrabImageFromCamera - save complete: E: \ temp \ 202111 \ \ camera 28-20211130085733-6. JPG 08:57:50, 479 [main] INFO Com. Bolingcavalry. Grabpush. Camera. GrabImageFromCamera - save complete: E: \ temp \ 202111 \ \ camera 28-20211130085733-7. JPG 08:57:51, 547 [main] INFO Com. Bolingcavalry. Grabpush. Camera. GrabImageFromCamera - save complete: E: \ temp \ 202111 \ \ camera 28-20211130085733-8. JPG 08:57:52, 551 [main] INFO Com. Bolingcavalry. Grabpush. Camera. AbstractCameraApplication - end [WARN: 0] global output 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 0Copy the code
  • Open the directory where the picture file resides, as shown below, and the picture has been successfully generated:

  • The details on one of them are as expected:

  • At this point, let’s capture the function is complete, next please continue to pay attention to Xinchen original, “JavaCV camera actual combat” series will show more rich applications;

Download the source code

  • The full source code for JavaCV Camera In Action is available on GitHub at github.com/zq2599/blog…
The name of the link note
Project home page Github.com/zq2599/blog… The project’s home page on GitHub
Git repository address (HTTPS) Github.com/zq2599/blog… The project source warehouse address, HTTPS protocol
Git repository address (SSH) [email protected]:zq2599/blog_demos.git The project source warehouse address, SSH protocol
  • The Git project has multiple folders. The source code for this project is in the Javacv-tutorials folder, as shown in the red box below:

  • Javacv-tutorials have many subprojects. The Code for the JavacV Camera Hands-on series was based on the Simply-grab-push project:

You are not alone, Xinchen original accompany all the way

Github.com/zq2599/blog…