Book Image

OpenFrameworks Essentials

Book Image

OpenFrameworks Essentials

Overview of this book

Table of Contents (19 chapters)
openFrameworks Essentials
Credits
Foreword
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Grabbing a live video from a camera


openFrameworks can grab live video from a built-in or external camera connected to your computer. It is accomplished using the ofVideoGrabber class.

Let's implement starting the camera to grab and draw the grabbed video frames on the screen.

Note

Starting a camera can take several seconds. Thus, if we start it in setup(), the project will take a bit longer to start. It could be quite annoying to keep the camera "on" even when we don't use it. For this reason, we will start the camera only when we need it, by pressing C.

The following are the implementation steps:

  1. Add the grabber object definition to the ofApp class:

    ofVideoGrabber camera;
  2. Add the commands to start the camera by adding the following lines to keyPressed():

    if ( key == 'c' ) {
      camera.setDeviceID( 0 );
      camera.setDesiredFrameRate( 30 );
      camera.initGrabber( 1280, 720 );
    }

    The first line is a condition checking whether C is pressed. The second line selects the camera device by its identifier, which...