1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "qquickimagecapture_p.h" |
5 | #include "qquickimagepreviewprovider_p.h" |
6 | |
7 | #include <QtCore/qurl.h> |
8 | |
9 | QT_BEGIN_NAMESPACE |
10 | |
11 | /*! |
12 | \qmltype ImageCapture |
13 | \instantiates QQuickImageCapture |
14 | \brief An interface for capturing camera images. |
15 | \ingroup multimedia_qml |
16 | \inqmlmodule QtMultimedia |
17 | \ingroup camera_qml |
18 | |
19 | This type allows you to capture still images and be notified when they |
20 | are available or saved to disk. |
21 | |
22 | \qml |
23 | Item { |
24 | width: 640 |
25 | height: 360 |
26 | |
27 | CaptureSession { |
28 | imageCapture : ImageCapture { |
29 | id: imageCapture |
30 | } |
31 | camera: Camera { |
32 | id: camera |
33 | } |
34 | |
35 | videoOutput: videoOutput |
36 | } |
37 | VideoOutput { |
38 | id: videoOutput |
39 | anchors.fill: parent |
40 | |
41 | MouseArea { |
42 | anchors.fill: parent; |
43 | onClicked: imageCapture.capture(); |
44 | } |
45 | } |
46 | |
47 | Image { |
48 | id: photoPreview |
49 | source: imageCapture.preview // always shows the last captured image |
50 | } |
51 | } |
52 | \endqml |
53 | |
54 | */ |
55 | |
56 | QQuickImageCapture::QQuickImageCapture(QObject *parent) |
57 | : QImageCapture(parent) |
58 | { |
59 | connect(sender: this, SIGNAL(imageCaptured(int,QImage)), receiver: this, SLOT(_q_imageCaptured(int,QImage))); |
60 | } |
61 | |
62 | QQuickImageCapture::~QQuickImageCapture() = default; |
63 | |
64 | /*! |
65 | \qmlproperty bool QtMultimedia::ImageCapture::readyForCapture |
66 | |
67 | This property holds a bool value indicating whether the camera |
68 | is ready to capture photos or not. |
69 | |
70 | Calling capture() or captureToFile() while \e ready is \c false is not permitted and |
71 | results in an error. |
72 | */ |
73 | |
74 | /*! |
75 | \qmlproperty string QtMultimedia::ImageCapture::preview |
76 | |
77 | This property holds a url to the latest captured image. It can be connected to the |
78 | source property of an \l Image element to show the last captured image. |
79 | |
80 | \qml |
81 | CaptureSession { |
82 | camera: Camera {} |
83 | imageCapture: ImageCapture { |
84 | id: capture |
85 | } |
86 | } |
87 | Image { |
88 | source: capture.preview |
89 | } |
90 | \endqml |
91 | |
92 | \sa saveToFile |
93 | */ |
94 | |
95 | /*! |
96 | \qmlmethod QtMultimedia::ImageCapture::capture() |
97 | |
98 | Start image capture. The \l imageCaptured and \l imageSaved signals will |
99 | be emitted when the capture is complete. |
100 | |
101 | The captured image will be available through the preview property that can be |
102 | used as the source for a QML Image item. The saveToFile() method can then be used |
103 | save the image. |
104 | |
105 | Camera saves all the capture parameters like exposure settings or |
106 | image processing parameters, so changes to camera parameters after |
107 | capture() is called do not affect previous capture requests. |
108 | |
109 | capture() returns the capture requestId parameter, used with |
110 | imageExposed(), imageCaptured(), imageMetadataAvailable() and imageSaved() signals. |
111 | |
112 | \sa readyForCapture, preview |
113 | */ |
114 | |
115 | /*! |
116 | \qmlmethod QtMultimedia::ImageCapture::captureToFile(location) |
117 | |
118 | Does the same as capture() but additionally automatically saves the captured image to the specified |
119 | \a location. |
120 | |
121 | \sa capture |
122 | */ |
123 | |
124 | QString QQuickImageCapture::preview() const |
125 | { |
126 | return m_capturedImagePath; |
127 | } |
128 | |
129 | /*! |
130 | \qmlmethod QtMultimedia::ImageCapture::saveToFile(location) |
131 | |
132 | Saves the last captured image to \a location. |
133 | |
134 | \sa capture, preview |
135 | */ |
136 | void QQuickImageCapture::saveToFile(const QUrl &location) const |
137 | { |
138 | m_lastImage.save(fileName: location.toLocalFile()); |
139 | } |
140 | |
141 | void QQuickImageCapture::_q_imageCaptured(int id, const QImage &preview) |
142 | { |
143 | QString previewId = QString::fromLatin1(ba: "preview_%1" ).arg(a: id); |
144 | QQuickImagePreviewProvider::registerPreview(id: previewId, preview); |
145 | m_capturedImagePath = QString::fromLatin1(ba: "image://camera/%2" ).arg(a: previewId); |
146 | m_lastImage = preview; |
147 | emit previewChanged(); |
148 | } |
149 | |
150 | /*! |
151 | \qmlsignal QtMultimedia::ImageCapture::errorOccurred(requestId, error, message) |
152 | |
153 | This signal is emitted when an error occurs during capture with \a requestId. |
154 | \a error is an enumeration of type ImageCapture::Error. |
155 | A descriptive message is available in \a message. |
156 | */ |
157 | |
158 | /*! |
159 | \qmlsignal QtMultimedia::ImageCapture::imageCaptured(requestId, previewImage) |
160 | |
161 | This signal is emitted when an image with \a requestId has been captured |
162 | but not yet saved to the filesystem. The \a previewImage |
163 | parameter is the captured image. |
164 | |
165 | \sa imageSaved, preview |
166 | */ |
167 | |
168 | /*! |
169 | \qmlsignal QtMultimedia::ImageCapture::imageSaved(requestId, path) |
170 | |
171 | This signal is emitted after the image with \a requestId has been written to the filesystem. |
172 | The \a path is a local file path, not a URL. |
173 | |
174 | \sa imageCaptured |
175 | */ |
176 | |
177 | |
178 | /*! |
179 | \qmlsignal QtMultimedia::ImageCapture::imageMetadataAvailable(requestId, key, value) |
180 | |
181 | This signal is emitted when the image with \a requestId has new metadata |
182 | available with the key \a key and value \a value. |
183 | |
184 | \sa imageCaptured |
185 | */ |
186 | |
187 | |
188 | QT_END_NAMESPACE |
189 | |
190 | #include "moc_qquickimagecapture_p.cpp" |
191 | |