Diffstat (limited to 'noncore/multimedia/camera/mainwindow.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/multimedia/camera/mainwindow.cpp | 162 |
1 files changed, 139 insertions, 23 deletions
diff --git a/noncore/multimedia/camera/mainwindow.cpp b/noncore/multimedia/camera/mainwindow.cpp index 6141fd1..e27a50e 100644 --- a/noncore/multimedia/camera/mainwindow.cpp +++ b/noncore/multimedia/camera/mainwindow.cpp | |||
@@ -14,12 +14,13 @@ | |||
14 | **********************************************************************/ | 14 | **********************************************************************/ |
15 | 15 | ||
16 | #include "mainwindow.h" | 16 | #include "mainwindow.h" |
17 | #include "previewwidget.h" | 17 | #include "previewwidget.h" |
18 | #include "zcameraio.h" | 18 | #include "zcameraio.h" |
19 | #include "imageio.h" | 19 | #include "imageio.h" |
20 | #include "avi.h" | ||
20 | 21 | ||
21 | #include <qapplication.h> | 22 | #include <qapplication.h> |
22 | #include <qaction.h> | 23 | #include <qaction.h> |
23 | #include <qvbox.h> | 24 | #include <qvbox.h> |
24 | #include <qcombobox.h> | 25 | #include <qcombobox.h> |
25 | #include <qcursor.h> | 26 | #include <qcursor.h> |
@@ -47,15 +48,19 @@ using namespace Opie; | |||
47 | #include <fcntl.h> | 48 | #include <fcntl.h> |
48 | #include <string.h> | 49 | #include <string.h> |
49 | #include <errno.h> | 50 | #include <errno.h> |
50 | #include <unistd.h> | 51 | #include <unistd.h> |
51 | 52 | ||
52 | #define CAPTUREFILE "/tmp/capture.dat" | 53 | #define CAPTUREFILE "/tmp/capture.dat" |
54 | #define OUTPUTFILE "/tmp/output.avi" | ||
53 | 55 | ||
54 | CameraMainWindow::CameraMainWindow( QWidget * parent, const char * name, WFlags f ) | 56 | CameraMainWindow::CameraMainWindow( QWidget * parent, const char * name, WFlags f ) |
55 | :QMainWindow( parent, name, f ), _capturing( false ), _pics( 0 ) | 57 | :QMainWindow( parent, name, f ), |
58 | _rotation( 270 ), // FIXME: get this from current settings (ODevice?) | ||
59 | _capturing( false ), | ||
60 | _pics( 0 ), _videos( 0 ) | ||
56 | { | 61 | { |
57 | #ifdef QT_NO_DEBUG | 62 | #ifdef QT_NO_DEBUG |
58 | if ( !ZCameraIO::instance()->isOpen() ) | 63 | if ( !ZCameraIO::instance()->isOpen() ) |
59 | { | 64 | { |
60 | QVBox* v = new QVBox( this ); | 65 | QVBox* v = new QVBox( this ); |
61 | v->setMargin( 10 ); | 66 | v->setMargin( 10 ); |
@@ -102,14 +107,14 @@ CameraMainWindow::~CameraMainWindow() | |||
102 | void CameraMainWindow::init() | 107 | void CameraMainWindow::init() |
103 | { | 108 | { |
104 | // TODO: Save this stuff in config | 109 | // TODO: Save this stuff in config |
105 | flip = 'A'; // auto | 110 | flip = 'A'; // auto |
106 | quality = 50; | 111 | quality = 50; |
107 | zoom = 1; | 112 | zoom = 1; |
108 | captureX = 640; | 113 | captureX = 480; |
109 | captureY = 480; | 114 | captureY = 640; |
110 | captureFormat = "JPEG"; | 115 | captureFormat = "JPEG"; |
111 | 116 | ||
112 | resog = new QActionGroup( 0, "reso", true ); | 117 | resog = new QActionGroup( 0, "reso", true ); |
113 | resog->setToggleAction( true ); | 118 | resog->setToggleAction( true ); |
114 | new QAction( " 64 x 48", 0, 0, resog, 0, true ); | 119 | new QAction( " 64 x 48", 0, 0, resog, 0, true ); |
115 | new QAction( "128 x 96", 0, 0, resog, 0, true ); | 120 | new QAction( "128 x 96", 0, 0, resog, 0, true ); |
@@ -158,27 +163,40 @@ void CameraMainWindow::init() | |||
158 | 163 | ||
159 | } | 164 | } |
160 | 165 | ||
161 | 166 | ||
162 | void CameraMainWindow::systemMessage( const QCString& msg, const QByteArray& data ) | 167 | void CameraMainWindow::systemMessage( const QCString& msg, const QByteArray& data ) |
163 | { | 168 | { |
169 | int _newrotation; | ||
170 | |||
164 | QDataStream stream( data, IO_ReadOnly ); | 171 | QDataStream stream( data, IO_ReadOnly ); |
165 | odebug << "received system message: " << msg << oendl; | 172 | odebug << "received system message: " << msg << oendl; |
166 | if ( msg == "setCurrentRotation(int)" ) | 173 | if ( msg == "setCurrentRotation(int)" ) |
167 | { | 174 | { |
168 | stream >> _rotation; | 175 | stream >> _newrotation; |
169 | odebug << "received setCurrentRotation(" << _rotation << ")" << oendl; | 176 | odebug << "received setCurrentRotation(" << _newrotation << ")" << oendl; |
170 | 177 | ||
171 | switch ( _rotation ) | 178 | switch ( _newrotation ) |
172 | { | 179 | { |
173 | case 270: preview->resize( QSize( 240, 288 ) ); break; | 180 | case 270: preview->resize( QSize( 240, 288 ) ); break; |
174 | case 180: preview->resize( QSize( 320, 208 ) ); break; | 181 | case 180: preview->resize( QSize( 320, 208 ) ); break; |
175 | default: QMessageBox::warning( this, "opie-camera", | 182 | default: QMessageBox::warning( this, "opie-camera", |
176 | "This rotation is not supported.\n" | 183 | "This rotation is not supported.\n" |
177 | "Supported are 180° and 270°" ); | 184 | "Supported are 180° and 270°" ); |
178 | } | 185 | } |
186 | |||
187 | if ( _newrotation != _rotation ) | ||
188 | { | ||
189 | int tmp = captureX; | ||
190 | captureX = captureY; | ||
191 | captureY = tmp; | ||
192 | _rotation = _newrotation; | ||
193 | } | ||
194 | |||
195 | updateCaption(); | ||
196 | |||
179 | } | 197 | } |
180 | } | 198 | } |
181 | 199 | ||
182 | 200 | ||
183 | void CameraMainWindow::changeZoom( int zoom ) | 201 | void CameraMainWindow::changeZoom( int zoom ) |
184 | { | 202 | { |
@@ -220,20 +238,37 @@ void CameraMainWindow::showContextMenu() | |||
220 | QPopupMenu m( this ); | 238 | QPopupMenu m( this ); |
221 | m.insertItem( "&Resolution", &reso ); | 239 | m.insertItem( "&Resolution", &reso ); |
222 | m.insertItem( "&Zoom", &zoom ); | 240 | m.insertItem( "&Zoom", &zoom ); |
223 | m.insertItem( "&Flip", &flip ); | 241 | m.insertItem( "&Flip", &flip ); |
224 | m.insertItem( "&Quality", &quality ); | 242 | m.insertItem( "&Quality", &quality ); |
225 | m.insertItem( "&Output As", &output ); | 243 | m.insertItem( "&Output As", &output ); |
244 | |||
245 | #ifndef QT_NO_DEBUG | ||
246 | m.insertItem( "&Debug!", this, SLOT( doSomething() ) ); | ||
247 | #endif | ||
248 | |||
226 | m.exec( QCursor::pos() ); | 249 | m.exec( QCursor::pos() ); |
227 | } | 250 | } |
228 | 251 | ||
229 | 252 | ||
230 | void CameraMainWindow::resoMenuItemClicked( QAction* a ) | 253 | void CameraMainWindow::resoMenuItemClicked( QAction* a ) |
231 | { | 254 | { |
232 | captureX = a->text().left(3).toInt(); | 255 | switch ( _rotation ) |
233 | captureY = a->text().right(3).toInt(); | 256 | { |
257 | case 270: | ||
258 | captureY = a->text().left(3).toInt(); | ||
259 | captureX = a->text().right(3).toInt(); | ||
260 | break; | ||
261 | case 180: | ||
262 | captureX = a->text().left(3).toInt(); | ||
263 | captureY = a->text().right(3).toInt(); | ||
264 | break; | ||
265 | default: QMessageBox::warning( this, "opie-camera", | ||
266 | "This rotation is not supported.\n" | ||
267 | "Supported are 180° and 270°" ); | ||
268 | } | ||
234 | odebug << "Capture Resolution now: " << captureX << ", " << captureY << oendl; | 269 | odebug << "Capture Resolution now: " << captureX << ", " << captureY << oendl; |
235 | updateCaption(); | 270 | updateCaption(); |
236 | } | 271 | } |
237 | 272 | ||
238 | 273 | ||
239 | void CameraMainWindow::qualityMenuItemClicked( QAction* a ) | 274 | void CameraMainWindow::qualityMenuItemClicked( QAction* a ) |
@@ -322,37 +357,40 @@ void CameraMainWindow::performCapture( const QString& format ) | |||
322 | 357 | ||
323 | void CameraMainWindow::startVideoCapture() | 358 | void CameraMainWindow::startVideoCapture() |
324 | { | 359 | { |
325 | //ODevice::inst()->touchSound(); | 360 | //ODevice::inst()->touchSound(); |
326 | ODevice::inst()->setLedState( Led_Mail, Led_BlinkSlow ); | 361 | ODevice::inst()->setLedState( Led_Mail, Led_BlinkSlow ); |
327 | 362 | ||
328 | _capturefd = ::open( CAPTUREFILE, O_WRONLY | O_CREAT ); | 363 | _capturefd = ::open( CAPTUREFILE, O_WRONLY | O_CREAT | O_TRUNC ); |
329 | if ( _capturefd == -1 ) | 364 | if ( _capturefd == -1 ) |
330 | { | 365 | { |
331 | owarn << "can't open capture file: " << strerror(errno) << oendl; | 366 | owarn << "can't open capture file: " << strerror(errno) << oendl; |
332 | return; | 367 | return; |
333 | } | 368 | } |
334 | 369 | ||
335 | _capturebuf = new unsigned char[captureX*captureY*2]; | 370 | _capturebuf = new unsigned char[captureX*captureY*2]; |
336 | _capturing = true; | 371 | _capturing = true; |
337 | _videopics = 0; | 372 | _videopics = 0; |
373 | _framerate = 0; | ||
338 | updateCaption(); | 374 | updateCaption(); |
339 | _time.start(); | 375 | _time.start(); |
340 | preview->setRefreshingRate( 1000 ); | 376 | preview->setRefreshingRate( 1000 ); |
341 | startTimer( 100 ); // too fast but that is ok | 377 | startTimer( 100 ); // too fast but that is ok |
342 | } | 378 | } |
343 | 379 | ||
344 | 380 | ||
345 | void CameraMainWindow::timerEvent( QTimerEvent* ) | 381 | void CameraMainWindow::timerEvent( QTimerEvent* ) |
346 | { | 382 | { |
347 | if ( !_capturing ) | 383 | if ( !_capturing ) |
348 | { | 384 | { |
349 | owarn << "timer event in CameraMainWindow without capturing video ?" << oendl; | 385 | odebug << "timer event in CameraMainWindow without capturing video ?" << oendl; |
350 | return; | 386 | return; |
351 | } | 387 | } |
352 | 388 | ||
389 | odebug << "timer event during video - now capturing frame #" << _videopics+1 << oendl; | ||
390 | |||
353 | ZCameraIO::instance()->captureFrame( captureX, captureY, zoom, _capturebuf ); | 391 | ZCameraIO::instance()->captureFrame( captureX, captureY, zoom, _capturebuf ); |
354 | _videopics++; | 392 | _videopics++; |
355 | ::write( _capturefd, _capturebuf, captureX*captureY*2 ); | 393 | ::write( _capturefd, _capturebuf, captureX*captureY*2 ); |
356 | setCaption( QString().sprintf( "Capturing %dx%d @ %.2f fps %d", | 394 | setCaption( QString().sprintf( "Capturing %dx%d @ %.2f fps %d", |
357 | captureX, captureY, 1000.0 / (_time.elapsed()/_videopics), _videopics ) ); | 395 | captureX, captureY, 1000.0 / (_time.elapsed()/_videopics), _videopics ) ); |
358 | } | 396 | } |
@@ -363,73 +401,151 @@ void CameraMainWindow::stopVideoCapture() | |||
363 | killTimers(); | 401 | killTimers(); |
364 | //ODevice::inst()->touchSound(); | 402 | //ODevice::inst()->touchSound(); |
365 | ODevice::inst()->setLedState( Led_Mail, Led_Off ); | 403 | ODevice::inst()->setLedState( Led_Mail, Led_Off ); |
366 | _capturing = false; | 404 | _capturing = false; |
367 | updateCaption(); | 405 | updateCaption(); |
368 | ::close( _capturefd ); | 406 | ::close( _capturefd ); |
407 | _framerate = 1000.0 / (_time.elapsed()/_videopics); | ||
369 | 408 | ||
370 | //postProcessVideo(); | 409 | postProcessVideo( CAPTUREFILE, QString().sprintf( "/tmp/video-%d_%d_%d_q%d-%dfps.avi", _videos++, captureX, captureY, quality, _framerate ) ); |
371 | 410 | ||
372 | #ifndef QT_NO_DEBUG | 411 | #ifndef QT_NO_DEBUG |
373 | preview->setRefreshingRate( 1500 ); | 412 | preview->setRefreshingRate( 1500 ); |
374 | #else | 413 | #else |
375 | preview->setRefreshingRate( 200 ); | 414 | preview->setRefreshingRate( 200 ); |
376 | #endif | 415 | #endif |
377 | 416 | ||
378 | //delete[] _capturebuf; //FIXME: close memory leak | 417 | //delete[] _capturebuf; //FIXME: close memory leak |
379 | } | 418 | } |
380 | 419 | ||
381 | void CameraMainWindow::postProcessVideo() | 420 | void CameraMainWindow::postProcessVideo( const QString& infile, const QString& outfile ) |
382 | { | 421 | { |
383 | preview->setRefreshingRate( 0 ); | 422 | preview->setRefreshingRate( 0 ); |
384 | 423 | ||
385 | /* | 424 | /* |
425 | unsigned char buf[153600]; | ||
386 | 426 | ||
387 | QDialog* fr = new QDialog( this, "splash" ); //, false, QWidget::WStyle_NoBorder | QWidget::WStyle_Customize ); | 427 | int fd = ::open( "/var/compile/opie/noncore/multimedia/camera/capture-320x240.dat", O_RDONLY ); |
428 | ::read( fd, &buf, 153600 ); | ||
429 | QImage i; | ||
430 | bufferToImage( 240, 320, (unsigned char*) &buf, &i ); | ||
431 | QPixmap p; | ||
432 | p.convertFromImage( i ); | ||
433 | preview->setPixmap( p ); | ||
434 | imageToFile( &i, "/tmp/tmpfile", "JPEG", 100 ); | ||
435 | return; | ||
436 | */ | ||
437 | |||
438 | QDialog* fr = new QDialog( this, "splash", false, QWidget::WStyle_StaysOnTop ); //, false, QWidget::WStyle_NoBorder | QWidget::WStyle_Customize ); | ||
388 | fr->setCaption( "Please wait..." ); | 439 | fr->setCaption( "Please wait..." ); |
389 | QVBoxLayout* box = new QVBoxLayout( fr, 2, 2 ); | 440 | QVBoxLayout* box = new QVBoxLayout( fr, 2, 2 ); |
390 | QProgressBar* bar = new QProgressBar( fr ); | 441 | QProgressBar* bar = new QProgressBar( fr ); |
391 | bar->setCenterIndicator( true ); | 442 | bar->setCenterIndicator( true ); |
392 | bar->setTotalSteps( _videopics-1 ); | 443 | bar->setTotalSteps( _videopics-1 ); |
393 | QLabel* label = new QLabel( "Post processing frame bla/bla", fr ); | 444 | QLabel* label = new QLabel( "Post processing frame bla/bla", fr ); |
394 | box->addWidget( bar ); | 445 | box->addWidget( bar ); |
395 | box->addWidget( label ); | 446 | box->addWidget( label ); |
396 | fr->show(); | 447 | fr->show(); |
448 | label->show(); | ||
449 | bar->show(); | ||
450 | fr->repaint(); | ||
397 | qApp->processEvents(); | 451 | qApp->processEvents(); |
398 | 452 | ||
399 | for ( int i = 0; i < _videopics; ++i ) | 453 | // open files |
400 | { | ||
401 | label->setText( QString().sprintf( "Post processing frame %d / %d", i+1, _videopics ) ); | ||
402 | bar->setProgress( i ); | ||
403 | qApp->processEvents(); | ||
404 | } | ||
405 | |||
406 | */ | ||
407 | 454 | ||
408 | int infd = ::open( CAPTUREFILE, O_RDONLY ); | 455 | int infd = ::open( (const char*) infile, O_RDONLY ); |
409 | if ( infd == -1 ) | 456 | if ( infd == -1 ) |
410 | { | 457 | { |
411 | owarn << "couldn't open capture file: " << strerror(errno) << oendl; | 458 | owarn << "couldn't open capture file: " << strerror(errno) << oendl; |
412 | return; | 459 | return; |
413 | } | 460 | } |
414 | 461 | ||
415 | int outfd = ::open( "/tmp/output.avi", O_WRONLY ); | 462 | int outfd = ::open( (const char*) outfile, O_CREAT | O_WRONLY | O_TRUNC, 0644 ); |
416 | if ( outfd == -1 ) | 463 | if ( outfd == -1 ) |
417 | { | 464 | { |
418 | owarn << "couldn't open output file: " << strerror(errno) << oendl; | 465 | owarn << "couldn't open output file: " << strerror(errno) << oendl; |
419 | return; | 466 | return; |
420 | } | 467 | } |
421 | 468 | ||
469 | int framesize = captureX*captureY*2; | ||
470 | |||
471 | unsigned char* inbuffer = new unsigned char[ framesize ]; | ||
472 | QImage image; | ||
422 | 473 | ||
474 | avi_start( outfd, _videopics ); // write preambel | ||
423 | 475 | ||
476 | // post process | ||
477 | |||
478 | for ( int i = 0; i < _videopics; ++i ) | ||
479 | { | ||
480 | odebug << "processing frame " << i << oendl; | ||
481 | |||
482 | // <gui> | ||
483 | label->setText( QString().sprintf( "Post processing frame %d / %d", i+1, _videopics ) ); | ||
484 | bar->setProgress( i ); | ||
485 | bar->repaint(); | ||
486 | qApp->processEvents(); | ||
487 | // </gui> | ||
488 | |||
489 | int read = ::read( infd, inbuffer, framesize ); | ||
490 | odebug << "read " << read << " bytes" << oendl; | ||
491 | bufferToImage( captureX, captureY, inbuffer, &image ); | ||
492 | |||
493 | QPixmap p; | ||
494 | p.convertFromImage( image ); | ||
495 | preview->setPixmap( p ); | ||
496 | preview->repaint(); | ||
497 | qApp->processEvents(); | ||
498 | |||
499 | QString tmpfilename( "/tmp/tempfile" ); | ||
500 | //tmpfilename.sprintf( "/tmp/test/%d.jpg", i ); | ||
501 | |||
502 | imageToFile( &image, tmpfilename, "JPEG", quality ); | ||
503 | |||
504 | QFile framefile( tmpfilename ); | ||
505 | if ( !framefile.open( IO_ReadOnly ) ) | ||
506 | { | ||
507 | oerr << "can't process file: %s" << strerror(errno) << oendl; | ||
508 | return; // TODO: clean up temp ressources | ||
509 | } | ||
510 | |||
511 | int filesize = framefile.size(); | ||
512 | odebug << "filesize for frame " << i << " = " << filesize << oendl; | ||
513 | |||
514 | unsigned char* tempbuffer = new unsigned char[ filesize ]; | ||
515 | framefile.readBlock( (char*) tempbuffer, filesize ); | ||
516 | avi_add( outfd, tempbuffer, filesize ); | ||
517 | delete tempbuffer; | ||
518 | framefile.close(); | ||
519 | } | ||
520 | |||
521 | avi_end( outfd, captureX, captureY, _framerate ); | ||
522 | |||
523 | fr->hide(); | ||
524 | delete fr; | ||
525 | |||
526 | updateCaption(); | ||
424 | 527 | ||
425 | } | 528 | } |
426 | 529 | ||
530 | |||
427 | void CameraMainWindow::updateCaption() | 531 | void CameraMainWindow::updateCaption() |
428 | { | 532 | { |
429 | if ( !_capturing ) | 533 | if ( !_capturing ) |
430 | setCaption( QString().sprintf( "Opie-Camera: %dx%d %s q%d z%d (%s)", captureX, captureY, (const char*) captureFormat.lower(), quality, zoom, (const char*) flip ) ); | 534 | setCaption( QString().sprintf( "Opie-Camera: %dx%d %s q%d z%d (%s)", captureX, captureY, (const char*) captureFormat.lower(), quality, zoom, (const char*) flip ) ); |
431 | else | 535 | else |
432 | setCaption( "Opie-Camera: => CAPTURING <=" ); | 536 | setCaption( "Opie-Camera: => CAPTURING <=" ); |
433 | } | 537 | } |
434 | 538 | ||
435 | 539 | ||
540 | #ifndef QT_NO_DEBUG | ||
541 | void CameraMainWindow::doSomething() | ||
542 | { | ||
543 | captureX = 240; | ||
544 | captureY = 320; | ||
545 | _videopics = 176; | ||
546 | _framerate = 5; | ||
547 | postProcessVideo( "/var/compile/opie/noncore/multimedia/camera/capture-320x240.dat", | ||
548 | "/tmp/output.avi" ); | ||
549 | } | ||
550 | #endif | ||
551 | |||