forked from rferrazz/CvCamView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cvcamview.cpp
148 lines (127 loc) · 4.21 KB
/
cvcamview.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
Copyright (C) 2012 Riccardo Ferrazzo <[email protected]>
This file is part of CvCamView.
CvCamView is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
WebDomo is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with CvCamView. If not, see <http://www.gnu.org/licenses/>
*/
#include "cvcamview.h"
#include <QtDeclarative/qdeclarative.h>
#include <QTimer>
CvCamView::CvCamView(QDeclarativeItem *parent):
QDeclarativeItem(parent), _camera(0), _resolution(new CvCamResolution()), _cameraState(UnloadedState)
{
setFlag(ItemHasNoContents, false);
capture = NULL;
timer = new QTimer(this);
QObject::connect(timer, SIGNAL(timeout()), this, SLOT(queryFrame()));
timer->start(40);
setupCamera();
}
void CvCamView::paint(QPainter *painter, const QStyleOptionGraphicsItem *options, QWidget *widget){
QSize imageSize = _qImage.size();
imageSize.scale(boundingRect().width(), boundingRect().height(), Qt::KeepAspectRatio);
QRect ImgBoundingRect((boundingRect().width()/2)-(imageSize.width()/2),
(boundingRect().height()/2)-(imageSize.height()/2), imageSize.width(), imageSize.height());
painter->drawImage(ImgBoundingRect, _qImage, _qImage.rect());
}
void CvCamView::setCamera(int camera){
this->_camera = camera;
setupCamera();
emit cameraChanged();
}
void CvCamView::setResolution(CvCamResolution *resolution){
_resolution = resolution;
setupCamera();
}
void CvCamView::setCameraState(CameraState state){
while(state < _cameraState)
decreaseCameraState();
while(state > _cameraState)
increaseCameraState();
}
void CvCamView::increaseCameraState(){
int state = ((int)_cameraState)+1;
switch(state){
case LoadedState:
capture = cvCreateCameraCapture(_camera);
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, _resolution->width());
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, _resolution->height());
break;
case ActiveState:
timer->blockSignals(false);
break;
}
_cameraState = (CameraState)state;
emit cameraStateChanged();
}
void CvCamView::decreaseCameraState(){
int state = ((int)_cameraState)-1;
switch(state){
case LoadedState:
timer->blockSignals(true);
break;
case UnloadedState:
cvReleaseCapture(&capture);
break;
}
_cameraState = (CameraState)state;
emit cameraStateChanged();
}
void CvCamView::setupCamera(){
setCameraState(UnloadedState);
setCameraState(ActiveState);
}
void CvCamView::queryFrame(){
_iplImage = cvQueryFrame(capture);
_qImage = CvCamView::ipl2Qimg(_iplImage);
update();
emit newFrame();
}
CvCamView::~CvCamView()
{
cvReleaseCapture(&capture);
}
QImage CvCamView::ipl2Qimg(IplImage* iplImg){
int h = iplImg->height;
int w = iplImg->width;
int channels = iplImg->nChannels;
QImage qimg(w, h, QImage::Format_ARGB32);
char *data = iplImg->imageData;
for (int y = 0; y < h; y++, data += iplImg->widthStep)
{
for (int x = 0; x < w; x++)
{
char r, g, b, a = 0;
if (channels == 1)
{
r = data[x * channels];
g = data[x * channels];
b = data[x * channels];
}
else if (channels == 3 || channels == 4)
{
r = data[x * channels + 2];
g = data[x * channels + 1];
b = data[x * channels];
}
if (channels == 4)
{
a = data[x * channels + 3];
qimg.setPixel(x, y, qRgba(r, g, b, a));
}
else
{
qimg.setPixel(x, y, qRgb(r, g, b));
}
}
}
return qimg;
}