Pretend using libzbar.
[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     QPixmap image("test.jpg");
73     ui->labAnalyzeImage->setPixmap(image);
74 }