413763b37cd7bf03cfdf082ab8d19e67bc4c4f81
[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
54 void CamShooter::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 CamShooter::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 CamShooter::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 CamShooter::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 CamShooter::slot_cap_imageSaved(int id, const QString &fileName) {
116     qDebug() << "imageSaved" << id << fileName;
117     emit fileReady(fileName);
118 }
119
120 void CamShooter::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