To access the camera when it is supported by the hardware and Qt Multimedia, use the Camera
type and its associated types to control the camera's capture behavior, exposure, flash, focus, and image processing settings. A simple use of the camera to show a viewfinder looks like the following code:
import QtQuick 2.3 import QtMultimedia 5.0 Item { width: 640 height: 480 Camera { id: camera } VideoOutput { source: camera anchors.fill: parent } }
In short, the Camera
type acts like a source for the video just as a MediaPlayer
instance does.
The Camera
type provides a few properties to control its behavior. They are:
imageCapture
: This is an instance of CameraCapture
, which defines how the camera should capture an imagevideoRecording
: This is an instance of CameraRecorder
, which defines how the camera should capture a videoexposure
: This is an instance of CameraExposure
, which controls the various options for the exposure mode of the camerafocus
: This is an instance of CameraFocus
, which controls the auto- and manual-focusing behaviorsflash
: This is an instance of CameraFlash
, which controls the camera flashimageProcessing
: This is an instance of CameraImageProcessing
, which controls the real-time image processing pipeline options such as white balance, saturation, and sharpeningThe types associated with these fields can't be instantiated directly.
To have the camera take a picture, specify the imageCapture
property and invoke its capture method, as follows:
import QtQuick 2.3 import QtMultimedia 5.0 Item { width: 640 height: 360 Camera { id: camera imageCapture { onImageCaptured: { // Show the preview in an Image photoPreview.source = preview } } } VideoOutput { source: camera focus : visible // to receive focus and capture key events when visible width: 320 height: 180 anchors.top: parent.top anchors.horizontalCenter: parent.horizontalCenter MouseArea { anchors.fill: parent; onClicked: camera.imageCapture.capture(); } } Image { id: photoPreview width: 320 height: 180 anchors.bottom: parent.bottom anchors.horizontalCenter: parent.horizontalCenter } }
Here, the camera displays its viewfinder in the top VideoOutput
item and has an Image
item at the bottom to display the captured image. When you touch the viewfinder, the QML invokes the capture
method of imageCapture
, which is part of Camera
, capturing the image and updating the bottom image.
The imageCapture
property of the Camera
item also has a capturedImagePath
property, which is a string to the path where the last captured image is stored.
Recording works in a similar manner; you specify the attributes of the recording, such as the desired codec in the videoRecording
property, and then invoke its record
and stop
methods to start and stop recording. The resulting video will be stored at the location indicated by the property's actualLocation
field.
For more information on the actual attributes available to applications using the Camera
type, see the Qt Multimedia documentation for the Camera
type at http://qt-project.org/doc/qt-5/cameraoverview.html.