(no commit message)
[nnokia.git] / nshooter.cpp
1 #include <QtDebug>
2 #include <QCameraFocusControl>
3
4 #include "nshooter.h"
5
6
7
8 NShooter::NShooter(QObject *parent) : QObject(parent) {
9     camera = NULL;
10     imageCapture = NULL;
11     encoderSettings.setCodec("image/jpeg");
12     encoderSettings.setQuality(QtMultimediaKit::LowQuality);
13     //encoderSettings.setQuality(QtMultimediaKit::VeryLowQuality);
14 }
15
16
17 NShooter::~NShooter() {
18     if (imageCapture) {
19         delete imageCapture;
20         imageCapture = NULL;
21     }
22     if (camera) {
23         delete camera;
24         camera = NULL;
25     }
26 }
27
28
29 void NShooter::debug_dump(const char *msg) {
30     qDebug() << msg;
31
32     qDebug() << "\t------------------";
33     if (camera) {
34         if (camera->error())
35             qDebug() << "\tCamera error" << camera->errorString();
36         else
37             qDebug() << "\tCamera no error";
38
39         qDebug() << "\tCamera state" << camera->state();
40         qDebug() << "\tCamera status" << camera->status();
41         qDebug() << "\tCamera lockstatus" << camera->lockStatus();
42     }
43
44     if (imageCapture) {
45         if (imageCapture->error())
46             qDebug() << "\tImageCapture error" << imageCapture->errorString();
47         else
48             qDebug() << "\tImageCapture no error";
49     }
50     qDebug() << "\t------------------";
51 }
52
53
54 void NShooter::slot_cam_state(QCamera::State state) {
55     if (state == QCamera::LoadedState)
56         debug_dump("camera->state changed to LoadedState");
57     else
58         qDebug() << "camera->state changed to" << state;
59 }
60
61 void NShooter::slot_cam_status(QCamera::Status status) {
62     if (status == QCamera::ActiveStatus) {
63
64         // set manual focus on infinity
65         qDebug() << "focus mode is now " << camera->focus()->focusMode();
66         camera->focus()->setFocusMode(QCameraFocus::InfinityFocus);
67         debug_dump("focus set to infinity");
68         qDebug() << "focus mode is now " << camera->focus()->focusMode();
69
70         // deactivating flash
71         QCameraExposure *camExposure = camera->exposure();
72         qDebug() << "Flash mode is now" << camExposure->flashMode();
73         qDebug() << "Setting flash mode to off";
74         camExposure->setFlashMode(QCameraExposure::FlashOff);
75         qDebug() << "Flash mode is now" << camExposure->flashMode();
76
77         debug_dump("Camera state is ActiveStatus. Calling searchAndLock.");
78         // disable autofocus
79         QCamera::LockTypes wantedlocks = camera->supportedLocks() & ~QCamera::LockFocus;
80         if (wantedlocks) {
81             camera->searchAndLock(wantedlocks);
82             debug_dump("after camera->searchAndLock");
83         } else {
84             qDebug() << "wantedlocks is 0. Calling slot_cam_lockstatus directly";
85             slot_cam_lockstatus(QCamera::Locked, QCamera::UserRequest);
86         }
87     }
88     else
89         qDebug() << "camera->status changed to" << status;
90 }
91
92 void NShooter::slot_cam_lockstatus(QCamera::LockStatus status, QCamera::LockChangeReason reason) {
93     if (status == QCamera::Locked) {
94         qDebug() << "Lock acquired. Shooting.";
95         imageCapture->capture(); //QString("c:\Data\Images\image.jpg"));
96         debug_dump("after imageCapture->capture");
97     }
98     qDebug() << "camera->lockstatus changed to" << status << "because" << reason;
99 }
100
101
102 void NShooter::slot_cap_captured(int id, const QImage&) {
103     qDebug() << "imageCaptured" << id;
104     qDebug() << "unlocking camera";
105     camera->unlock();
106     this->debug_dump("after camera->unlock");
107     qDebug() << "stoping camera";
108     camera->stop();
109     this->debug_dump("after camera->stop");
110     qDebug() << "unloading camera";
111     camera->unload();
112     this->debug_dump("after camera->unload");
113 }
114
115 void NShooter::slot_cap_imageSaved(int id, const QString &fileName) {
116     qDebug() << "imageSaved" << id << fileName;
117     emit fileReady(fileName);
118 }
119
120 void NShooter::go() {
121     if (!camera) {
122         camera = new QCamera(this);
123         if (!camera) {
124             qDebug() << "new QCamera failed";
125             return;
126         }
127         QObject::connect(camera, SIGNAL(stateChanged (QCamera::State)),
128                          this, SLOT(slot_cam_state(QCamera::State)));
129         QObject::connect(camera, SIGNAL(statusChanged (QCamera::Status)),
130                          this, SLOT(slot_cam_status(QCamera::Status)));
131         QObject::connect(camera, SIGNAL(lockStatusChanged(QCamera::LockStatus, QCamera::LockChangeReason)),
132                          this, SLOT(slot_cam_lockstatus(QCamera::LockStatus, QCamera::LockChangeReason)));
133
134         camera->setCaptureMode(QCamera::CaptureStillImage);
135         debug_dump("after camera->setCaptureMode");
136     }
137
138     if (!imageCapture) {
139         imageCapture = new QCameraImageCapture(camera, this);
140         if (!imageCapture) {
141             qDebug() << "new QCameraImageCapture failed";
142             return;
143         }
144         QObject::connect(imageCapture, SIGNAL(imageCaptured(int, const QImage&)),
145                          this, SLOT(slot_cap_captured(int, const QImage&)));
146         QObject::connect(imageCapture, SIGNAL(imageSaved(int, const QString&)),
147                          this, SLOT(slot_cap_imageSaved(int, const QString&)));
148         imageCapture->setEncodingSettings(encoderSettings);
149     }
150
151     QCamera::Status curstatus = camera->status();
152     if (curstatus != QCamera::ActiveStatus) {
153         qDebug() << "camera status is" << curstatus << "Starting cameara...";
154         camera->start();
155         debug_dump("after camera->start");
156     } else {
157         // direct call to slot
158         slot_cam_status(QCamera::ActiveStatus);
159     }
160 }
161