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