1 | initial version |
And this is sourcecode : -
IplImage *image = 0;
// const int MAX_COUNT = 500; // char* status = 0; int night_mode = 0; // int flags = 0;
CvCapture* capture = 0; CvVideoWriter * writer = 0;
sprintf(_window_name, "CAMERA : %d", _request_cam_id+1); cvNamedWindow( _window_name, 0 );
// Set Always on Top int x = CAPTURE_WINDOW_OFFSET; int y = CAPTURE_WINDOW_OFFSET; setWindowAlwaysTop(_window_name, x, y, CAPTURE_WINDOW_WIDTH, CAPTURE_WINDOW_HEIGHT); disableCloseWindowButton(_window_name);
capture = cvCaptureFromCAM(CV_CAP_DSHOW + _request_cam_id ); _camera = _request_cam_id;
cvSetCaptureProperty(capture, CV_CAP_PROP_FPS, CAPTURE_FPS); // 10 qDebug() << cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH) << cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT);
double f = 1.0; double w = cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH) * f; double h = cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT) * f; if(f != 1.0) { // set camera properties 320x240 is the best resolution cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, w); //opencv doesnt like 640x480 cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, h); //as most webcams use YUYV/YUY2 codecs. }
if( ! capture ) { const QString errText("Could not initialize capturing...\n"); qDebug() << errText; writeLog(errText); return; }
while( _request_cam_id >= 0 ) // for(;;) { if(_request_cam_id != _camera) { cvDestroyWindow(_window_name);
if(_camera < 0) break;
sprintf(_window_name, "CAMERA : %d", _request_cam_id+1);
cvNamedWindow( _window_name, 0 );
// Set Always on Top
int x = CAPTURE_WINDOW_OFFSET;
int y = CAPTURE_WINDOW_OFFSET;
setWindowAlwaysTop(_window_name, x, y, CAPTURE_WINDOW_WIDTH, CAPTURE_WINDOW_HEIGHT);
disableCloseWindowButton(_window_name);
capture = cvCaptureFromCAM(CV_CAP_DSHOW + _request_cam_id );
// set camera properties 320x240 is the best resolution
if(f != 1)
{
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, w); //opencv doesnt like 640x480
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, h); //as most webcams use YUYV/YUY2 codecs.
}
cvSetCaptureProperty(capture, CV_CAP_PROP_FPS, CAPTURE_FPS); //Playstation Eye Only: Max FPS goes to 120!!!
_camera = _request_cam_id;
if( ! capture )
{
const QString errText("Could capture from requested camera...\n");
qDebug() << errText;
writeLog(errText);
break;
}
}
IplImage* frame = 0;
int c;
frame = cvQueryFrame( capture );
if( ! frame )
break;
if( ! image )
{
// allocate all the buffers //
// image = cvCreateImage( cvGetSize(frame), IPL_DEPTH_8U, 3 );
qDebug() << "Depth : " << frame->depth << " Channels : " << frame->nChannels;
image = cvCreateImage(cvSize(frame->width, frame->height), frame->depth, frame->nChannels);
image->origin = frame->origin;
// status = (char*)cvAlloc(MAX_COUNT);
// flags = 0;
}
// Clipping
if( ! _camera_clip.isEmpty() )
{
// Custom Clipping
QStringList l = _camera_clip.split(",");
if(l.count() == 4)
{
// x, y, right, bottom
long right = l.at(2).toLong();
long bottom = l.at(3).toLong();
long w = l.at(2).toLong() - l.at(0).toLong();
long h = l.at(3).toLong() - l.at(1).toLong();
qDebug() << QString("Custom camera clip recangle (%1):-\n"
"Left : %2\n"
"Top : %3\n"
"Right : %4\n"
"Bottom: %5\n"
"From %6x%7")
.arg(_camera_clip).arg(l.at(0)).arg(l.at(1)).arg(right).arg(bottom)
.arg(frame->width).arg(frame->height);
if( (right > frame->width) || (bottom > frame->height) )
{
this->error = QString("Invalid clipping value, should not over than %1x%2")
.arg(frame->width).arg(frame->height);
((CaptureForm *)captureForm)->showMessage("INVALID CLIPPING VALUE", this->error);
break;
} else {
CvRect rect = cvRect(l.at(0).toLong(), l.at(1).toLong(), w, h);
cvSetImageROI(frame, rect);
image = cvCreateImage(cvSize(rect.width, rect.height), frame->depth, frame->nChannels);
}
}
}
cvCopy( frame, image, 0 );
/** Marking for Region Of Interest **/
if(_zoom < 1.0)
{
CvRect rect = getZoomRect(image);
cvRectangle(image,
cvPoint(rect.x, rect.y),
cvPoint(rect.x+rect.width, rect.y+rect.height),
CV_RGB(127, 255, 127), 2);
}
// cvShowImage( _window_name, frame );
cvShowImage( _window_name, image );
// cvMoveWindow( _window_name, 0, 0 );
if( _request_recoring && ! _recordVDO )
{
CvSize fsize = cvGetSize(image); // cvGetSize(frame);
// qDebug() << "CV SIZE : " << fsize.width << "x" << fsize.height;
writer = createVideoWriter(fsize);
// qDebug() << writer; // DO NOT REMOVE THIS LINE - IT WILL CRASH
_recordVDO = _request_recoring;
playAudioFile(AUDIO_START_RECORD);
}
if(writer && _recordVDO)
{
int result = cvWriteFrame(writer, image); // cvWriteFrame(writer, frame);
if(result == 0)
{
qDebug() << "Error write frame";
_request_recoring = false;
((CaptureForm *)captureForm)->presentStopRecording();
// QMessageBox::critical(0, "Error", "Error recording video");
}
// qDebug() << "write result : " << result; // DO NOT REMOVE THIS LINE - IT WILL CRASH
}
if( ! _request_recoring && _recordVDO )
{
cvReleaseVideoWriter( &writer );
writer = 0;
QFile::copy(QString(_video_temp_name), QString(_video_file_name)); // 22-May-2014 - copy from temp file to actual path
_recordVDO = _request_recoring;
playAudioFile(AUDIO_STOP_RECORD);
}
c = cvWaitKey(10); // Do not remove calling cvWaitKey - Program will dead
// qDebug() << c;
if( (char)c == 27 )
break;
else if(_request_capture)
c = ' ';
switch( (char) c )
{
case ' ':
case '-':
{
if(_zoom == 1.0)
writeImageFile(image);
else
{
CvRect rect = getZoomRect(frame);
cvSetImageROI(frame, rect);
IplImage *roiImg = cvCreateImage(cvSize(rect.width, rect.height), frame->depth, frame->nChannels);
cvCopy(frame, roiImg, 0);
writeImageFile(roiImg);
cvReleaseImage(&roiImg);
cvResetImageROI(frame);
}
_request_capture = false;
((CaptureForm*)captureForm)->showLastCapture(); // Added on 1-Feb-2011
playAudioFile(AUDIO_CAPTURE);
}
break;
case '+':
case 's':
case 'v':
{
_request_recoring = ! _recordVDO;
if(_request_recoring)
((CaptureForm *)captureForm)->presentStartRecording();
else
((CaptureForm *)captureForm)->presentStopRecording();
}
break;
case 'n':
night_mode ^= 1;
break;
default:
;
}
}
if(writer) cvReleaseVideoWriter( &writer ); if(capture) cvReleaseCapture( &capture );
cvDestroyWindow(_window_name);
quit(); // finished();