展示如何捕获静止图像或录制视频。
摄像头范例演示如何使用 Qt Multimedia 实现一些基本摄像头功能以获得静止图像和录制带音频的视频剪辑。
要运行范例从 Qt Creator ,打开 欢迎 模式,然后选择范例从 范例 。更多信息,拜访 构建和运行范例 .
The example implements a
Camera
class that acts as our camera interface. It has a user interface, control functions, setting values and a means of defining the location where the image or video clip is to be saved. It will also store the image and video settings.
The Camera class uses:
The Camera constructor does some basic initialization:
However, most of the work is done when the setCamera() function is called, passing in a QCameraDevice .
setCamera() sets up various connections between the user interface and the functionality of the Camera class using signals and slots. It also instantiates and initializes the QCamera , QImageCapture ,和 QMediaRecorder objects mentioned above. The still and video recording visual tabs are enabled and finally the start() function of the QCamera object is called.
Now that the camera is ready for user commands it waits for a suitable event. Such an event can be a key press of either the
Qt::Key_CameraFocus
or
Qt::Key_Camera
buttons on the application window. Camera focus will simply display the preview and lock the camera settings.
Key_Camera
will either call
takeImage()
if doing an image capture, or call
record()
or
stop()
(if already recording) on the
QMediaRecorder
instance when recording video.