Implement "analyze" button that loads the test image.
[toast/barcode2cash.git] / qtapp / barcode2cash / mainwindow.cpp
1 #include "mainwindow.h"
2 #include "ui_mainwindow.h"
3
4 bool checkCameraAvailability()
5 {
6     if (QCameraInfo::availableCameras().count() > 0)
7         return true;
8     else
9         return false;
10 }
11
12 MainWindow::MainWindow(QWidget *parent) :
13     QMainWindow(parent),
14     ui(new Ui::MainWindow)
15 {
16     ui->setupUi(this);
17
18     QCameraInfo info = QCameraInfo::defaultCamera();
19     if (!info.isNull()) {
20         camera = new QCamera(info);
21         camera->setCaptureMode(QCamera::CaptureStillImage);
22         camera->setViewfinder(ui->viewFinder);
23         imageCapture = new QCameraImageCapture(camera);
24         connect(imageCapture, SIGNAL(readyForCaptureChanged(bool)), SLOT(updateCameraInfo()));
25         connect(imageCapture, SIGNAL(imageSaved(int,QString)), SLOT(showCaptureSuccess(int,QString)));
26         connect(imageCapture, SIGNAL(error(int,QCameraImageCapture::Error,QString)), SLOT(showCaptureError(int,QCameraImageCapture::Error,QString)));
27         updateCameraInfo();
28         connect(ui->pbCapture, SIGNAL(clicked()), SLOT(capture()));
29         QImageEncoderSettings imageSettings;
30         imageSettings.setCodec("image/jpeg");
31         imageCapture->setEncodingSettings(imageSettings);
32         camera->start();
33     }
34
35
36     ui->statusBar->showMessage(tr("There are %1 cameras").arg(checkCameraAvailability()));
37
38 }
39
40 MainWindow::~MainWindow() {
41     delete imageCapture;
42     delete camera;
43     delete ui;
44 }
45
46
47 void MainWindow::updateCameraInfo() {
48     ui->labCameraReady->setText(imageCapture->isReadyForCapture() ? "ready" : "not ready");
49 }
50
51
52 void MainWindow::capture() {
53     camera->searchAndLock();
54     imageCapture->capture("test.jpg");
55     camera->unlock();
56 }
57
58
59 void MainWindow::showCaptureSuccess(int, QString fileName) {
60     ui->statusBar->showMessage(tr("Image captured and saved as '%1'.").arg(fileName));
61 }
62
63
64 void MainWindow::showCaptureError(int, QCameraImageCapture::Error error, QString errorString) {
65     ui->statusBar->showMessage(tr("Error when capturing image (error id %2): %1.").arg(errorString).arg(error));
66 }
67
68
69 void MainWindow::analyze() {
70     QPixmap image("test.jpg");
71     ui->labAnalyzeImage->setPixmap(image);
72 }