Proof of concept decoding of an QR code works (but is not displayed yet).
[toast/barcode2cash.git] / qtapp / barcode2cash / mainwindow.cpp
1 #include <cstddef>
2 #include <zbar.h>
3 #include "mainwindow.h"
4 #include "ui_mainwindow.h"
5
6 bool checkCameraAvailability()
7 {
8     if (QCameraInfo::availableCameras().count() > 0)
9         return true;
10     else
11         return false;
12 }
13
14 MainWindow::MainWindow(QWidget *parent) :
15     QMainWindow(parent),
16     ui(new Ui::MainWindow)
17 {
18     ui->setupUi(this);
19
20     QCameraInfo info = QCameraInfo::defaultCamera();
21     if (!info.isNull()) {
22         camera = new QCamera(info);
23         camera->setCaptureMode(QCamera::CaptureStillImage);
24         camera->setViewfinder(ui->viewFinder);
25         imageCapture = new QCameraImageCapture(camera);
26         connect(imageCapture, SIGNAL(readyForCaptureChanged(bool)), SLOT(updateCameraInfo()));
27         connect(imageCapture, SIGNAL(imageSaved(int,QString)), SLOT(showCaptureSuccess(int,QString)));
28         connect(imageCapture, SIGNAL(error(int,QCameraImageCapture::Error,QString)), SLOT(showCaptureError(int,QCameraImageCapture::Error,QString)));
29         updateCameraInfo();
30         connect(ui->pbCapture, SIGNAL(clicked()), SLOT(capture()));
31         QImageEncoderSettings imageSettings;
32         imageSettings.setCodec("image/jpeg");
33         imageCapture->setEncodingSettings(imageSettings);
34         camera->start();
35     }
36
37
38     ui->statusBar->showMessage(tr("There are %1 cameras").arg(checkCameraAvailability()));
39
40 }
41
42 MainWindow::~MainWindow() {
43     delete imageCapture;
44     delete camera;
45     delete ui;
46 }
47
48
49 void MainWindow::updateCameraInfo() {
50     ui->labCameraReady->setText(imageCapture->isReadyForCapture() ? "ready" : "not ready");
51 }
52
53
54 void MainWindow::capture() {
55     camera->searchAndLock();
56     imageCapture->capture("test.jpg");
57     camera->unlock();
58 }
59
60
61 void MainWindow::showCaptureSuccess(int, QString fileName) {
62     ui->statusBar->showMessage(tr("Image captured and saved as '%1'.").arg(fileName));
63 }
64
65
66 void MainWindow::showCaptureError(int, QCameraImageCapture::Error error, QString errorString) {
67     ui->statusBar->showMessage(tr("Error when capturing image (error id %2): %1.").arg(errorString).arg(error));
68 }
69
70
71 void MainWindow::analyze() {
72     QImage qimage("test.jpg");
73     qimage = qimage.convertToFormat(QImage::Format_Grayscale8);
74     auto zbimage = zbar::Image(qimage.width(), qimage.height(), "GREY", qimage.constBits(), qimage.byteCount());
75     auto scanner = zbar::ImageScanner();
76     auto scan_results = scanner.scan(zbimage);
77     if (scan_results > 0) {
78         auto results = scanner.get_results();
79         for (auto resit = results.symbol_begin(); resit != results.symbol_end(); ++ resit) {
80             if (resit->get_type() == zbar::ZBAR_QRCODE) {
81                 QString qrcode_data = QString::fromStdString(resit->get_data());
82                 // https://github.com/a-sit-plus/at-registrierkassen-mustercode/releases/download/1.2-DOK/2016-09-05-Detailfragen-RKSV-V1.2.pdf
83                 auto parts = qrcode_data.split("_");
84                 if (parts.size() != 14) continue; // QR code found but not RKSV
85                 parts.takeFirst();
86                 auto rlat = parts.takeFirst();
87                 auto device_id = parts.takeFirst();
88                 auto receipt_id = parts.takeFirst();
89                 auto date_time = parts.takeFirst();
90                 auto cash0 = parts.takeFirst();
91                 auto cash1 = parts.takeFirst();
92                 auto cash2 = parts.takeFirst();
93                 auto cash3 = parts.takeFirst();
94                 auto cash4 = parts.takeFirst();
95                 auto counter = parts.takeFirst();
96                 auto cert_serial = parts.takeFirst();
97                 auto sig_prev = parts.takeFirst();
98                 auto sig = parts.takeFirst();
99
100                 break;
101             }
102         }
103     }
104
105     if (qimage.width() > 500) qimage = qimage.scaledToWidth(500);
106     ui->labAnalyzeImage->setPixmap(QPixmap::fromImage(qimage));
107 }