summaryrefslogtreecommitdiff
path: root/noncore/multimedia/camera/mainwindow.cpp
Unidiff
Diffstat (limited to 'noncore/multimedia/camera/mainwindow.cpp') (more/less context) (show whitespace changes)
-rw-r--r--noncore/multimedia/camera/mainwindow.cpp158
1 files changed, 137 insertions, 21 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
@@ -17,6 +17,7 @@
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>
@@ -50,9 +51,13 @@ using namespace Opie;
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
54CameraMainWindow::CameraMainWindow( QWidget * parent, const char * name, WFlags f ) 56CameraMainWindow::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() )
@@ -105,8 +110,8 @@ void CameraMainWindow::init()
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 );
@@ -161,14 +166,16 @@ void CameraMainWindow::init()
161 166
162void CameraMainWindow::systemMessage( const QCString& msg, const QByteArray& data ) 167void 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;
@@ -176,6 +183,17 @@ void CameraMainWindow::systemMessage( const QCString& msg, const QByteArray& dat
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
@@ -223,14 +241,31 @@ void CameraMainWindow::showContextMenu()
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
230void CameraMainWindow::resoMenuItemClicked( QAction* a ) 253void CameraMainWindow::resoMenuItemClicked( QAction* a )
231{ 254{
255 switch ( _rotation )
256 {
257 case 270:
258 captureY = a->text().left(3).toInt();
259 captureX = a->text().right(3).toInt();
260 break;
261 case 180:
232 captureX = a->text().left(3).toInt(); 262 captureX = a->text().left(3).toInt();
233 captureY = a->text().right(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}
@@ -325,7 +360,7 @@ void CameraMainWindow::startVideoCapture()
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;
@@ -335,6 +370,7 @@ void CameraMainWindow::startVideoCapture()
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 );
@@ -346,10 +382,12 @@ 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 );
@@ -366,8 +404,9 @@ void CameraMainWindow::stopVideoCapture()
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 );
@@ -378,13 +417,25 @@ void CameraMainWindow::stopVideoCapture()
378 //delete[] _capturebuf; //FIXME: close memory leak 417 //delete[] _capturebuf; //FIXME: close memory leak
379} 418}
380 419
381void CameraMainWindow::postProcessVideo() 420void 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];
426
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 */
386 437
387 QDialog* fr = new QDialog( this, "splash" ); //, false, QWidget::WStyle_NoBorder | QWidget::WStyle_Customize ); 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 );
@@ -394,36 +445,89 @@ void CameraMainWindow::postProcessVideo()
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;
473
474 avi_start( outfd, _videopics ); // write preambel
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 }
422 520
521 avi_end( outfd, captureX, captureY, _framerate );
423 522
523 fr->hide();
524 delete fr;
525
526 updateCaption();
424 527
425} 528}
426 529
530
427void CameraMainWindow::updateCaption() 531void CameraMainWindow::updateCaption()
428{ 532{
429 if ( !_capturing ) 533 if ( !_capturing )
@@ -433,3 +537,15 @@ void CameraMainWindow::updateCaption()
433} 537}
434 538
435 539
540#ifndef QT_NO_DEBUG
541void 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