Capturing Images

One of the key features of the Camera element is that is can be used to take pictures. We will use this in a simple stop-motion application. By building the application, you will learn how to show a viewfinder, switch between cameras, snap photos and keep track of the pictures taken.

The user interface is shown below. It consists of three major parts. In the background, you will find the viewfinder, to the right, a column of buttons and at the bottom, a list of images taken. The idea is to take a series of photos, then click the Play Sequence button. This will play the images back, creating a simple stop-motion film.

image

The viewfinder

The viewfinder part of the camera is made using a VideoOutput element as video output channel of a CaptureSession. The CaptureSession in turns uses a Camera component to configure the device. This will display a live video stream from the camera.

  1. CaptureSession {
  2. id: captureSession
  3. videoOutput: output
  4. camera: Camera {}
  5. imageCapture: ImageCapture {
  6. /* ... */
  7. }
  8. }
  9. VideoOutput {
  10. id: output
  11. anchors.fill: parent
  12. }

TIP

You can have more control on the camera behaviour by using dedicated Camera properties such as exposureMode, whiteBalanceMode or zoomFactor.

The captured images list

The list of photos is a ListView oriented horizontally that shows images from a ListModel called imagePaths. In the background, a semi-transparent black Rectangle is used.

  1. ListModel {
  2. id: imagePaths
  3. }
  4. ListView {
  5. id: listView
  6. anchors.left: parent.left
  7. anchors.right: parent.right
  8. anchors.bottom: parent.bottom
  9. anchors.bottomMargin: 10
  10. height: 100
  11. orientation: ListView.Horizontal
  12. spacing: 10
  13. model: imagePaths
  14. delegate: Image {
  15. height: 100
  16. source: path
  17. fillMode: Image.PreserveAspectFit
  18. }
  19. Rectangle {
  20. anchors.fill: parent
  21. anchors.topMargin: -10
  22. color: "black"
  23. opacity: 0.5
  24. }
  25. }

For the shooting of images, the CaptureSession element contains a set of sub-elements for various tasks. To capture still pictures, the CaptureSession.imageCapture element is used. When you call the captureToFile method, a picture is taken and saved in the user’s local pictures directory. This results in the CaptureSession.imageCapture emitting the imageSaved signal.

  1. Button {
  2. id: shotButton
  3. width: parent.buttonWidth
  4. height: parent.buttonHeight
  5. text: qsTr("Take Photo")
  6. onClicked: {
  7. captureSession.imageCapture.captureToFile()
  8. }
  9. }

In this case, we don’t need to show a preview image, but simply add the resulting image to the ListView at the bottom of the screen. Shown in the example below, the path to the saved image is provided as the path argument with the signal.

  1. CaptureSession {
  2. /* ... */
  3. imageCapture: ImageCapture {
  4. onImageSaved: function (id, path) {
  5. imagePaths.append({"path": path})
  6. listView.positionViewAtEnd()
  7. }
  8. }
  9. }

TIP

For showing a preview, connect to the imageCaptured signal and use the preview signal argument as source of an Image element. An id signal argument is sent along both the imageCaptured and imageSaved. This value is returned from the capture method. Using this, the capture of an image can be traced through the complete cycle. This way, the preview can be used first and then be replaced by the properly saved image. This, however, is nothing that we do in the example.

Switching between cameras

If the user has multiple cameras, it can be handy to provide a way of switching between those. It’s possible to achieve this by using the MediaDevices element in conjunction with a ListView. In our case, we’ll use a ComboBox component:

  1. MediaDevices {
  2. id: mediaDevices
  3. }
  4. ComboBox {
  5. id: cameraComboBox
  6. width: parent.buttonWidth
  7. height: parent.buttonHeight
  8. model: mediaDevices.videoInputs
  9. textRole: "description"
  10. displayText: captureSession.camera.cameraDevice.description
  11. onActivated: function (index) {
  12. captureSession.camera.cameraDevice = cameraComboBox.currentValue
  13. }
  14. }

The model property of the ComboBox is set to the videoInputs property of our MediaDevices. This last property contains the list of usable video inputs. We then set the displayText of the control to the description of the camera device (captureSession.camera.cameraDevice.description).

Finally, when the user switches the video input, the cameraDevice is updated to reflect that change: captureSession.camera.cameraDevice = cameraComboBox.currentValue.

The playback

The last part of the application is the actual playback. This is driven using a Timer element and some JavaScript. The _imageIndex variable is used to keep track of the currently shown image. When the last image has been shown, the playback is stopped. In the example, the root.state is used to hide parts of the user interface when playing the sequence.

  1. property int _imageIndex: -1
  2. function startPlayback() {
  3. root.state = "playing"
  4. setImageIndex(0)
  5. playTimer.start()
  6. }
  7. function setImageIndex(i) {
  8. _imageIndex = i
  9. if (_imageIndex >= 0 && _imageIndex < imagePaths.count) {
  10. image.source = imagePaths.get(_imageIndex).path
  11. } else {
  12. image.source = ""
  13. }
  14. }
  15. Timer {
  16. id: playTimer
  17. interval: 200
  18. repeat: false
  19. onTriggered: {
  20. if (_imageIndex + 1 < imagePaths.count) {
  21. setImageIndex(_imageIndex + 1)
  22. playTimer.start()
  23. } else {
  24. setImageIndex(-1)
  25. root.state = ""
  26. }
  27. }
  28. }