Skip to content

Commit 9aca0f0

Browse files
committed
rotate captured image
1 parent 90f9402 commit 9aca0f0

5 files changed

Lines changed: 129 additions & 40 deletions

File tree

qml/content/CameraRenderer.qml

Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import QtQuick.Controls
44
import Qt5Compat.GraphicalEffects
55
import QtQuick.Layouts
66
import BackgroundFilter
7+
import CaptureProcessor
78

89
Item {
910
id: renderer
@@ -41,6 +42,29 @@ Item {
4142
}
4243
}
4344

45+
CaptureProcessor
46+
{
47+
id: captureProcessor
48+
49+
rotation: applicationSettings.cameraOrientation
50+
51+
onCaptureSaved: fileName =>
52+
{
53+
54+
if(backgroundFilterEnabled)
55+
{
56+
console.log("Process file: " + fileName)
57+
backgroundFilter.processCapture(fileName)
58+
}
59+
else
60+
{
61+
renderer.state = "preview"
62+
savedPhoto("file:" + fileName)
63+
console.log("Saved: " + fileName)
64+
}
65+
}
66+
}
67+
4468
CaptureSession {
4569

4670
camera: Camera {
@@ -60,25 +84,25 @@ Item {
6084
imageCapture: ImageCapture {
6185
id: imageCapture
6286

63-
onImageSaved: (_, fileName) => {
64-
65-
if(backgroundFilterEnabled)
66-
{
67-
console.log("Process file: " + fileName)
68-
backgroundFilter.processCapture(fileName)
69-
}
70-
else
87+
onImageCaptured:
7188
{
72-
renderer.state = "preview"
73-
savedPhoto("file:" + fileName)
74-
console.log("Saved: " + fileName)
89+
whiteOverlay.state = "released"
90+
renderer.state = "store"
91+
console.log("Captured")
92+
93+
console.log(applicationSettings.foldername.toString())
94+
var path = applicationSettings.foldername.toString()
95+
if(backgroundFilterEnabled)
96+
{
97+
path = path + "/raw"
98+
}
99+
path = path.replace(/^(file:\/{2})/, "")
100+
var cleanPath = decodeURIComponent(path)
101+
console.log(cleanPath)
102+
103+
captureProcessor.saveCapture(preview, cleanPath + "/Pict_" + new Date().toLocaleString(
104+
locale, "dd_MM_yyyy_hh_mm_ss") + ".jpg")
75105
}
76-
}
77-
onImageCaptured: {
78-
whiteOverlay.state = "released"
79-
renderer.state = "store"
80-
console.log("Captured")
81-
}
82106
onErrorOccurred: {
83107
renderer.state = "preview"
84108
failed()
@@ -120,14 +144,15 @@ Item {
120144
videoSink: output.videoSink
121145
background: backgroundImage
122146

123-
onCaptureProcessingFinished: {
124-
console.log("Capture processing finished")
125-
if (backgroundFilterEnabled) {
126-
renderer.state = "preview"
127-
savedPhoto("file:" + fileName)
128-
console.log("Saved: " + fileName)
129-
}
130-
}
147+
onCaptureProcessingFinished: fileName =>
148+
{
149+
console.log("Capture processing finished")
150+
if (backgroundFilterEnabled) {
151+
renderer.state = "preview"
152+
savedPhoto("file:" + fileName)
153+
console.log("Saved: " + fileName)
154+
}
155+
}
131156
}
132157

133158
Connections {
@@ -221,28 +246,17 @@ Item {
221246
function takePhoto() {
222247
if (cameraSession.imageCapture.readyForCapture) {
223248
state = "snapshot"
224-
console.log(applicationSettings.foldername.toString())
225-
var path = applicationSettings.foldername.toString()
226-
if(backgroundFilterEnabled)
227-
{
228-
path = path + "/raw"
229-
}
230-
path = path.replace(/^(file:\/{2})/, "")
231-
var cleanPath = decodeURIComponent(path)
232-
console.log(cleanPath)
233-
cameraSession.imageCapture.captureToFile(
234-
cleanPath + "/Pict_" + new Date().toLocaleString(
235-
locale, "dd_MM_yyyy_hh_mm_ss") + ".jpg")
249+
cameraSession.imageCapture.capture()
236250
} else {
237251
renderer.state = "preview"
238252
failed()
239253
}
240254
}
241255

242256
BusyIndicator {
243-
id: busyIndicator
244-
anchors.centerIn: parent
245-
visible: false
257+
id: busyIndicator
258+
anchors.centerIn: parent
259+
visible: false
246260
}
247261

248262
states: [

qtbooth.pro

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CONFIG += c++17 qml_debug
1010
}
1111

1212
SOURCES += src/collageiconmodel.cpp \
13+
src/captureprocessor.cpp \
1314
src/collageimagemodel.cpp \
1415
src/collagemodelfactory.cpp \
1516
src/fakeprinter.cpp \
@@ -57,6 +58,7 @@ INCLUDEPATH += src/ \
5758
HEADERS += \
5859
src/abstractprinter.h \
5960
src/call_once.h \
61+
src/captureprocessor.h \
6062
src/collageiconmodel.h \
6163
src/collageimagemodel.h \
6264
src/collagemodelfactory.h \

src/captureprocessor.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include "captureprocessor.h"
2+
#include <QDebug>
3+
4+
CaptureProcessor::CaptureProcessor(QObject *parent)
5+
: QObject{parent}
6+
{}
7+
8+
int CaptureProcessor::getRotation() const
9+
{
10+
return mRotation;
11+
}
12+
13+
void CaptureProcessor::setRotation(int rotation)
14+
{
15+
if (rotation < 0 || rotation >= 360) {
16+
qWarning() << "Invalid rotation value. Must be between 0 and 359.";
17+
return;
18+
}
19+
20+
if(rotation % 90)
21+
{
22+
qWarning() << "Rotation value should be a multiple of 90 degrees. Adjusting to nearest valid value.";
23+
rotation = (rotation / 90) * 90; // Adjust to nearest multiple of 90
24+
}
25+
26+
mRotation = rotation;
27+
}
28+
29+
void CaptureProcessor::saveCapture(QImage preview, const QString &filePath)
30+
{
31+
if (preview.isNull() || filePath.isEmpty()) {
32+
qWarning() << "Invalid image or file path.";
33+
return;
34+
}
35+
36+
QTransform transform;
37+
transform.rotate(mRotation);
38+
QImage rotated = preview.transformed(transform);
39+
40+
if (rotated.save(filePath)) {
41+
emit captureSaved(filePath);
42+
} else {
43+
qWarning() << "Failed to save capture to" << filePath;
44+
}
45+
}

src/captureprocessor.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef CAPTUREPROCESSOR_H
2+
#define CAPTUREPROCESSOR_H
3+
4+
#include <QObject>
5+
#include <QImage>
6+
7+
class CaptureProcessor : public QObject
8+
{
9+
Q_OBJECT
10+
Q_PROPERTY(int rotation READ getRotation WRITE setRotation)
11+
public:
12+
explicit CaptureProcessor(QObject *parent = nullptr);
13+
int getRotation() const;
14+
void setRotation(int rotation);
15+
16+
public slots:
17+
void saveCapture(QImage preview, const QString &filePath);
18+
19+
signals:
20+
void captureSaved(QString filePath);
21+
protected:
22+
int mRotation;
23+
};
24+
25+
#endif // CAPTUREPROCESSOR_H

src/main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <QQmlContext>
88
#include <QTranslator>
99
#include "translationhelper.h"
10+
#include "captureprocessor.h"
1011
#include "fakeprinter.h"
1112
#include "selphyprinter.h"
1213
#include "printerfactory.h"
@@ -124,6 +125,8 @@ int main(int argc, char *argv[])
124125

125126
qmlRegisterType<System>("System", 1, 0, "System");
126127

128+
qmlRegisterType<CaptureProcessor>("CaptureProcessor", 1, 0, "CaptureProcessor");
129+
127130
qmlRegisterInterface<AbstractPrinter>("AbstractPrinter", 1);
128131
qmlRegisterUncreatableType<AbstractPrinter>("Printer", 1, 0, "Printer", "Printer can only be created via PrinterFactory");
129132
qmlRegisterType<PrinterFactory>("Printer", 1, 0, "PrinterFactory");

0 commit comments

Comments
 (0)