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:
- Add the grabber object definition to the
ofApp
class:ofVideoGrabber camera;
- 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...