openEventsPlatformApp/plugins/cordova-plugin-camera/src/ubuntu/camera.cpp
plugins/cordova-plugin-camera/src/ubuntu/camera.cppRaw
1/*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20*/
21
22#include "camera.h"
23#include <cordova.h>
24
25#include <QCameraViewfinder>
26#include <QCameraImageCapture>
27#include <QGraphicsObject>
28#include <QCloseEvent>
29#include <QQuickItem>
30
31const char code[] = "\
32var component, object; \
33function createObject() { \
34 component = Qt.createComponent(%1); \
35 if (component.status == Component.Ready) \
36 finishCreation(); \
37 else \
38 component.statusChanged.connect(finishCreation); \
39} \
40function finishCreation() { \
41 CordovaWrapper.global.cameraPluginWidget = component.createObject(root, \
42 {root: root, cordova: cordova}); \
43} \
44createObject()";
45
46
47Camera::Camera(Cordova *cordova):
48 CPlugin(cordova),
49 _lastScId(0),
50 _lastEcId(0) {
51}
52
53bool Camera::preprocessImage(QString &path) {
54 bool convertToPNG = (*_options.find("encodingType")).toInt() == Camera::PNG;
55 int quality = (*_options.find("quality")).toInt();
56 int width = (*_options.find("targetWidth")).toInt();
57 int height = (*_options.find("targetHeight")).toInt();
58
59 QImage image(path);
60 if (width <= 0)
61 width = image.width();
62 if (height <= 0)
63 height = image.height();
64 image = image.scaled(width, height, Qt::KeepAspectRatio, Qt::SmoothTransformation);
65
66 QFile oldImage(path);
67 QTemporaryFile newImage;
68
69 const char *type;
70 if (convertToPNG) {
71 path = generateLocation("png");
72 type = "png";
73 } else {
74 path = generateLocation("jpg");
75 type = "jpg";
76 }
77
78 image.save(path, type, quality);
79
80 oldImage.remove();
81
82 return true;
83}
84
85void Camera::onImageSaved(QString path) {
86 bool dataURL = _options.find("destinationType")->toInt() == Camera::DATA_URL;
87
88 QString cbParams;
89 if (preprocessImage(path)) {
90 QString absolutePath = QFileInfo(path).absoluteFilePath();
91 if (dataURL) {
92 QFile image(absolutePath);
93 image.open(QIODevice::ReadOnly);
94 QByteArray content = image.readAll().toBase64();
95 cbParams = QString("\"%1\"").arg(content.data());
96 image.remove();
97 } else {
98 cbParams = CordovaInternal::format(QString("file://localhost") + absolutePath);
99 }
100 }
101
102 this->callback(_lastScId, cbParams);
103
104 _lastEcId = _lastScId = 0;
105}
106
107void Camera::takePicture(int scId, int ecId, int quality, int destinationType, int/*sourceType*/, int targetWidth, int targetHeight, int encodingType,
108 int/*mediaType*/, bool/*allowEdit*/, bool/*correctOrientation*/, bool/*saveToPhotoAlbum*/, const QVariantMap &/*popoverOptions*/, int/*cameraDirection*/) {
109 if (_camera.isNull()) {
110 _camera = QSharedPointer<QCamera>(new QCamera());
111 }
112
113 if (((_lastScId || _lastEcId) && (_lastScId != scId && _lastEcId != ecId)) || !_camera->isAvailable() || _camera->lockStatus() != QCamera::Unlocked) {
114 this->cb(_lastEcId, "Device is busy");
115 return;
116 }
117
118 _options.clear();
119 _options.insert("quality", quality);
120 _options.insert("destinationType", destinationType);
121 _options.insert("targetWidth", targetWidth);
122 _options.insert("targetHeight", targetHeight);
123 _options.insert("encodingType", encodingType);
124
125 _lastScId = scId;
126 _lastEcId = ecId;
127
128 QString path = m_cordova->get_app_dir() + "/../qml/CaptureWidget.qml";
129
130 // TODO: relative url
131 QString qml = QString(code).arg(CordovaInternal::format(path));
132 m_cordova->execQML(qml);
133}
134
135void Camera::cancel() {
136 m_cordova->execQML("CordovaWrapper.global.cameraPluginWidget.destroy()");
137 this->cb(_lastEcId, "canceled");
138
139 _lastEcId = _lastScId = 0;
140}