Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

mainwindow.cpp

Go to the documentation of this file.
00001 /**********************************************************************
00002 ** Copyright (C) 2002 Michael 'Mickey' Lauer.  All rights reserved.
00003 **
00004 ** This file is part of Opie Environment.
00005 **
00006 ** This file may be distributed and/or modified under the terms of the
00007 ** GNU General Public License version 2 as published by the Free Software
00008 ** Foundation and appearing in the file LICENSE.GPL included in the
00009 ** packaging of this file.
00010 **
00011 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 **
00014 **********************************************************************/
00015 
00016 #include "mainwindow.h"
00017 #include "previewwidget.h"
00018 #include "zcameraio.h"
00019 #include "imageio.h"
00020 #include "avi.h"
00021 
00022 /* OPIE */
00023 #include <opie2/ofiledialog.h>
00024 #include <opie2/odevice.h>
00025 #include <opie2/oapplication.h>
00026 #include <opie2/oconfig.h>
00027 #include <opie2/odebug.h>
00028 #include <opie2/oresource.h>
00029 #include <qpe/global.h>
00030 #include <qpe/qcopenvelope_qws.h>
00031 
00032 /* QT */
00033 #include <qapplication.h>
00034 #include <qaction.h>
00035 #include <qvbox.h>
00036 #include <qcombobox.h>
00037 #include <qcursor.h>
00038 #include <qdatastream.h>
00039 #include <qdir.h>
00040 #include <qfile.h>
00041 #include <qimage.h>
00042 #include <qlabel.h>
00043 #include <qlineedit.h>
00044 #include <qpopupmenu.h>
00045 #include <qprogressbar.h>
00046 #include <qpushbutton.h>
00047 #include <qmessagebox.h>
00048 #include <qlayout.h>
00049 #include <qdirectpainter_qws.h>
00050 
00051 /* STD */
00052 #include <assert.h>
00053 #include <sys/types.h>
00054 #include <sys/stat.h>
00055 #include <fcntl.h>
00056 #include <string.h>
00057 #include <errno.h>
00058 #include <unistd.h>
00059 
00060 #define CAPTUREFILE "/tmp/capture.dat"
00061 #define OUTPUTFILE  "/tmp/output.avi"
00062 
00063 #define OUTPUT_TO_CUSTOM 250
00064 #define OUTPUT_TO_DOCFOLDER 251
00065 
00066 
00067 using namespace Opie::Ui;
00068 using namespace Opie::Core;
00069 
00070 CameraMainWindow::CameraMainWindow( QWidget * parent, const char * name, WFlags f )
00071            :QMainWindow( parent, name, f ),
00072            _rotation( 270 ), // FIXME: get this from current settings (ODevice?)
00073            _capturing( false ),
00074            _pics( 1 ), _videos( 1 )
00075 {
00076     #ifdef QT_NO_DEBUG
00077     if ( !ZCameraIO::instance()->isOpen() )
00078     {
00079         QVBox* v = new QVBox( this );
00080         v->setMargin( 10 );
00081         QLabel* l1 = new QLabel( v );
00082         l1->setPixmap( Opie::Core::OResource::loadPixmap( "camera/error", Opie::Core::OResource::SmallIcon ) );
00083         QLabel* l2 = new QLabel( v );
00084         l2->setText( "<b>Sorry. could not detect your camera :-(</b><p>"
00085         "* Is the sharpzdc_cs module loaded ?<br>"
00086         "* Is /dev/sharpzdc read/writable ?<p>" );
00087         connect( new QPushButton( "Exit", v ), SIGNAL( clicked() ), this, SLOT( close() ) );
00088         setCentralWidget( v );
00089         return;
00090     }
00091     #endif
00092 
00093     init();
00094 
00095     _rotation = 270; //TODO: grab these from the actual settings
00096 
00097     preview = new PreviewWidget( this, "camera preview widget" );
00098     //setCentralWidget( preview ); <--- don't do this!
00099     preview->resize( QSize( 240, 288 ) );
00100     preview->show();
00101 
00102     // construct a System Channel to receive setRotation messages
00103     _sysChannel = new QCopChannel( "QPE/System", this );
00104     connect( _sysChannel, SIGNAL( received(const QCString&,const QByteArray&) ),
00105              this, SLOT( systemMessage(const QCString&,const QByteArray&) ) );
00106 
00107     connect( preview, SIGNAL( contextMenuRequested() ), this, SLOT( showContextMenu() ) );
00108 
00109     connect( ZCameraIO::instance(), SIGNAL( shutterClicked() ), this, SLOT( shutterClicked() ) );
00110 
00111     updateCaption();
00112 
00113 };
00114 
00115 
00116 CameraMainWindow::~CameraMainWindow()
00117 {
00118     // write back configuration
00119     OConfigGroupSaver cgs( oApp->config(), "General" );
00120     cgs.config()->writeEntry( "flip", flip );
00121     cgs.config()->writeEntry( "quality", quality );
00122     cgs.config()->writeEntry( "zoom", zoom );
00123     cgs.config()->writeEntry( "captureX", captureX );
00124     cgs.config()->writeEntry( "captureY", captureY );
00125     cgs.config()->writeEntry( "captureFormat", captureFormat );
00126     cgs.config()->writeEntry( "outputTo", outputTo );
00127     cgs.config()->writeEntry( "prefix", prefix );
00128     cgs.config()->writeEntry( "appendSettings", appendSettings );
00129 }
00130 
00131 
00132 void CameraMainWindow::init()
00133 {
00134     // get values from configuration
00135     OConfigGroupSaver cgs( oApp->config(), "General" );
00136     flip = cgs.config()->readEntry( "flip", "A" );
00137     quality = cgs.config()->readNumEntry( "quality", 50 );
00138     zoom = cgs.config()->readNumEntry( "zoom", 1 );
00139     captureX = cgs.config()->readNumEntry( "captureX", 480 );
00140     captureY = cgs.config()->readNumEntry( "captureY", 640 );
00141     captureFormat = cgs.config()->readEntry( "captureFormat", "JPEG" );
00142     outputTo = cgs.config()->readEntry( "outputTo", "Documents Folder" );
00143     prefix = cgs.config()->readEntry( "prefix", "Untitled" );
00144     appendSettings = cgs.config()->readBoolEntry( "appendSettings", true );
00145 
00146     // create action groups
00147     QAction* a;
00148     resog = new QActionGroup( 0, "reso", true );
00149     resog->setToggleAction( true );
00150     new QAction( " 64 x  48", 0, 0, resog, "64x48", true );
00151     new QAction( "128 x  96", 0, 0, resog, "128x96", true );
00152     new QAction( "192 x 144", 0, 0, resog, "192x144", true );
00153     new QAction( "256 x 192", 0, 0, resog, "256x192", true );
00154     new QAction( "320 x 240", 0, 0, resog, "320x240", true );
00155     new QAction( "384 x 288", 0, 0, resog, "384x288", true );
00156     new QAction( "448 x 336", 0, 0, resog, "448x336", true );
00157     new QAction( "512 x 384", 0, 0, resog, "512x384", true );
00158     new QAction( "576 x 432", 0, 0, resog, "576x432", true );
00159     new QAction( "640 x 480", 0, 0, resog, "640x480", true );
00160     a = (QAction*) resog->child( QString().sprintf( "%dx%d", captureX>captureY ? captureX:captureY, captureX>captureY ? captureY:captureX ) );
00161     if ( a ) a->setOn( true );
00162     else owarn << "can't set resolution" << oendl;
00163 
00164     qualityg = new QActionGroup( 0, "quality", true );
00165     qualityg->setToggleAction( true );
00166     new QAction( "  0 (&minimal)", 0, 0, qualityg, "0", true );
00167     new QAction( " 25 (&low)", 0, 0, qualityg, "25", true );
00168     new QAction( " 50 (&good)", 0, 0, qualityg, "50", true );
00169     new QAction( " 75 (&better)", 0, 0, qualityg, "75", true );
00170     new QAction( "100 (bes&t)", 0, 0, qualityg, "100", true );
00171     a = (QAction*) qualityg->child( QString().sprintf( "%d", quality ) );
00172     if ( a ) a->setOn( true );
00173     else owarn << "can't set quality" << oendl;
00174 
00175     zoomg = new QActionGroup( 0, "zoom", true );
00176     zoomg->setToggleAction( true );
00177     new QAction( "x 1", 0, 0, zoomg, "1", true );
00178     new QAction( "x 2", 0, 0, zoomg, "2", true );
00179     a = (QAction*) zoomg->child( QString().sprintf( "%d", zoom ) );
00180     if ( a ) a->setOn( true );
00181     else owarn << "can't set zoom" << oendl;
00182 
00183     flipg = new QActionGroup( 0, "flip", true );
00184     flipg->setToggleAction( true );
00185     new QAction( "Auto (recommended)", 0, 0, flipg, "A", true );
00186     new QAction( "0 (always off)", 0, 0, flipg, "0", true );
00187     new QAction( "X (always horizontal)", 0, 0, flipg, "X", true );
00188     new QAction( "Y (always vertical)", 0, 0, flipg, "Y", true );
00189     new QAction( "* (always both)", 0, 0, flipg, "*", true );
00190     a = (QAction*) flipg->child( QString().sprintf( "%s", (const char*) flip ) );
00191     if ( a ) a->setOn( true );
00192     else owarn << "can't set flip" << oendl;
00193 
00194     outputTog = new QActionGroup( 0, "output", true );
00195     outputTog->setToggleAction( true );
00196     new QAction( "/tmp/", 0, 0, outputTog, "/tmp/", true );
00197     new QAction( "/mnt/card/", 0, 0, outputTog, "/mnt/card/", true );
00198     new QAction( "/mnt/cf/", 0, 0, outputTog, "/mnt/cf/", true );
00199     docfolder = new QAction( "Documents Folder", 0, 0, outputTog, "Documents Folder", true );
00200     custom = new QAction( "&Custom...", 0, 0, outputTog, "custom", true ); //TODO: How to save custom!?
00201     a = (QAction*) outputTog->child( QString().sprintf( "%s", (const char*) outputTo ) );
00202     if ( a ) a->setOn( true );
00203     else owarn << "can't set outputTo" << oendl;
00204 
00205     outputg = new QActionGroup( 0, "output", true );
00206     outputg->setToggleAction( true );
00207     new QAction( "JPEG", 0, 0, outputg, "JPEG", true );
00208     new QAction( "PNG", 0, 0, outputg, "PNG", true );
00209     new QAction( "BMP", 0, 0, outputg, "BMP", true );
00210     new QAction( "AVI", 0, 0, outputg, "AVI", true );
00211     a = (QAction*) outputg->child( QString().sprintf( "%s", (const char*) captureFormat ) );
00212     if ( a ) a->setOn( true );
00213     else owarn << "can't set output format" << oendl;
00214 
00215     connect( resog, SIGNAL( selected(QAction*) ), this, SLOT( resoMenuItemClicked(QAction*) ) );
00216     connect( qualityg, SIGNAL( selected(QAction*) ), this, SLOT( qualityMenuItemClicked(QAction*) ) );
00217     connect( zoomg, SIGNAL( selected(QAction*) ), this, SLOT( zoomMenuItemClicked(QAction*) ) );
00218     connect( flipg, SIGNAL( selected(QAction*) ), this, SLOT( flipMenuItemClicked(QAction*) ) );
00219     connect( outputTog, SIGNAL( selected(QAction*) ), this, SLOT( outputToMenuItemClicked(QAction*) ) );
00220     connect( outputg, SIGNAL( selected(QAction*) ), this, SLOT( outputMenuItemClicked(QAction*) ) );
00221 }
00222 
00223 
00224 void CameraMainWindow::systemMessage( const QCString& msg, const QByteArray& data )
00225 {
00226     int _newrotation;
00227 
00228     QDataStream stream( data, IO_ReadOnly );
00229     odebug << "received system message: " << msg << oendl;
00230     if ( msg == "setCurrentRotation(int)" )
00231     {
00232         stream >> _newrotation;
00233         odebug << "received setCurrentRotation(" << _newrotation << ")" << oendl;
00234 
00235         switch ( _newrotation )
00236         {
00237             case 270: preview->resize( QSize( 240, 288 ) ); break;
00238             case 180: preview->resize( QSize( 320, 208 ) ); break;
00239             default: QMessageBox::warning( this, "opie-camera",
00240                 "This rotation is not supported.\n"
00241                 "Supported are 180 and 270" );
00242         }
00243 
00244         if ( _newrotation != _rotation )
00245         {
00246             int tmp = captureX;
00247             captureX = captureY;
00248             captureY = tmp;
00249             _rotation = _newrotation;
00250         }
00251 
00252         updateCaption();
00253 
00254     }
00255 }
00256 
00257 
00258 void CameraMainWindow::changeZoom( int zoom )
00259 {
00260     int z;
00261     switch ( zoom )
00262     {
00263         case 0: z = 128; break;
00264         case 1: z = 256; break;
00265         case 2: z = 512; break;
00266         default: assert( 0 ); break;
00267     }
00268 
00269     ZCameraIO::instance()->setCaptureFrame( 240, 160, z );
00270 }
00271 
00272 
00273 void CameraMainWindow::showContextMenu()
00274 {
00275     QPopupMenu reso;
00276     reso.setCheckable( true );
00277     resog->addTo( &reso );
00278 
00279     QPopupMenu quality;
00280     quality.setCheckable( true );
00281     qualityg->addTo( &quality );
00282 
00283     QPopupMenu flip;
00284     flip.setCheckable( true );
00285     flipg->addTo( &flip );
00286 
00287     QPopupMenu zoom;
00288     zoom.setCheckable( true );
00289     zoomg->addTo( &zoom );
00290 
00291     QPopupMenu prefix;
00292     prefix.insertItem( "&Choose...", this, SLOT( prefixItemChoosen() ) );
00293     int id = prefix.insertItem( "&Append Settings", this, SLOT( appendSettingsChoosen() ) );
00294     prefix.setItemChecked( id, appendSettings );
00295 
00296     QPopupMenu outputTo;
00297     outputTo.setCheckable( true );
00298     outputTog->addTo( &outputTo );
00299 
00300     QPopupMenu output;
00301     output.setCheckable( true );
00302     outputg->addTo( &output );
00303 
00304     QPopupMenu m( this );
00305     m.insertItem( "&Resolution", &reso );
00306     m.insertItem( "&Zoom", &zoom );
00307     m.insertItem( "&Flip", &flip );
00308     m.insertItem( "&Quality", &quality );
00309     m.insertSeparator();
00310     m.insertItem( "&Prefix", &prefix );
00311     m.insertItem( "Output &To", &outputTo );
00312     m.insertItem( "&Output As", &output );
00313 
00314     #ifndef QT_NO_DEBUG
00315     m.insertItem( "&Debug!", this, SLOT( doSomething() ) );
00316     #endif
00317 
00318     m.exec( QCursor::pos() );
00319 }
00320 
00321 
00322 void CameraMainWindow::resoMenuItemClicked( QAction* a )
00323 {
00324     switch ( _rotation )
00325     {
00326         case 270:
00327             captureY = a->text().left(3).toInt();
00328             captureX = a->text().right(3).toInt();
00329             break;
00330         case 180:
00331             captureX = a->text().left(3).toInt();
00332             captureY = a->text().right(3).toInt();
00333             break;
00334         default: QMessageBox::warning( this, "opie-camera",
00335             "This rotation is not supported.\n"
00336             "Supported are 180 and 270" );
00337     }
00338     odebug << "Capture Resolution now: " << captureX << ", " << captureY << oendl;
00339     updateCaption();
00340 }
00341 
00342 
00343 void CameraMainWindow::qualityMenuItemClicked( QAction* a )
00344 {
00345     quality = a->text().left(3).toInt();
00346     odebug << "Quality now: " << quality << oendl;
00347     updateCaption();
00348 }
00349 
00350 
00351 void CameraMainWindow::zoomMenuItemClicked( QAction* a )
00352 {
00353     zoom = QString( a->text().at(2) ).toInt();
00354     odebug << "Zoom now: " << zoom << oendl;
00355     ZCameraIO::instance()->setZoom( zoom );
00356     updateCaption();
00357 }
00358 
00359 
00360 void CameraMainWindow::flipMenuItemClicked( QAction* a )
00361 {
00362     flip = QString( a->text().at(0) );
00363     odebug << "Flip now: " << flip << oendl;
00364     if ( flip == "A" )
00365         ZCameraIO::instance()->setFlip( ZCameraIO::AUTOMATICFLIP );
00366     else if ( flip == "0" )
00367         ZCameraIO::instance()->setFlip( ZCameraIO::XNOFLIP | ZCameraIO::YNOFLIP );
00368     else if ( flip == "X" )
00369         ZCameraIO::instance()->setFlip( ZCameraIO::XFLIP );
00370     else if ( flip == "Y" )
00371         ZCameraIO::instance()->setFlip( ZCameraIO::YFLIP );
00372     else if ( flip == "*" )
00373         ZCameraIO::instance()->setFlip( ZCameraIO::XFLIP | ZCameraIO::YFLIP );
00374 
00375     updateCaption();
00376 }
00377 
00378 
00379 void CameraMainWindow::outputToMenuItemClicked( QAction* a )
00380 {
00381     if ( a->text() == "&Custom..." )
00382     {
00383         QMap<QString, QStringList> map;
00384         map.insert( tr("All"), QStringList() );
00385         QStringList text;
00386         text << "text/*";
00387         map.insert(tr("Text"), text );
00388         text << "*";
00389         map.insert(tr("All"), text );
00390 
00391         QString str;
00392         str = OFileDialog::getSaveFileName( 2, "/", QString::null, map );
00393         if ( str.isEmpty() || !QFileInfo(str).isDir() )
00394         {
00395             docfolder->setOn( true );
00396             outputTo = "Documents Folder";
00397         }
00398         else
00399         {
00400             outputTo = str;
00401         }
00402     }
00403     else
00404     {
00405         outputTo = a->text();
00406     }
00407     odebug << "Output to now: " << outputTo << oendl;
00408 }
00409 
00410 
00411 void CameraMainWindow::outputMenuItemClicked( QAction* a )
00412 {
00413     captureFormat = a->text();
00414     odebug << "Output format now: " << captureFormat << oendl;
00415     updateCaption();
00416 }
00417 
00418 
00419 void CameraMainWindow::prefixItemChoosen()
00420 {
00421     QDialog* d = new QDialog( this, "dialog", true );
00422     d->setCaption( "Enter Prefix..." );
00423     QVBoxLayout* v = new QVBoxLayout( d );
00424     QLineEdit* le = new QLineEdit( prefix, d );
00425     v->addWidget( le );
00426     le->setFixedWidth( 150 ); //FIXME: 'tis a bit dirty
00427     if ( d->exec() == QDialog::Accepted )
00428         prefix = le->text();
00429     odebug << "Prefix now: " << prefix << oendl;
00430 }
00431 
00432 
00433 void CameraMainWindow::appendSettingsChoosen()
00434 {
00435     appendSettings = !appendSettings;
00436     odebug << "appendSettings now: " << appendSettings << oendl;
00437 }
00438 
00439 
00440 void CameraMainWindow::shutterClicked()
00441 {
00442     if ( captureFormat != "AVI" ) // capture one photo per shutterClick
00443     {
00444         Global::statusMessage( "CAPTURING..." );
00445         qApp->processEvents();
00446 
00447         odebug << "Shutter has been pressed" << oendl;
00448         ODevice::inst()->playTouchSound();
00449 
00450         performCapture( captureFormat );
00451     }
00452     else // capture video! start with one shutter click and stop with the next
00453     {
00454         !_capturing ? startVideoCapture() : stopVideoCapture();
00455     }
00456 }
00457 
00458 
00459 void CameraMainWindow::performCapture( const QString& format )
00460 {
00461     QString name;
00462 
00463     if ( outputTo == "Documents Folder" )
00464     {
00465         name.sprintf(  "%s/Documents/image/%s/", (const char*) QDir::homeDirPath(), (const char*) captureFormat.lower() );
00466         if ( !QDir( name ).exists() )
00467         {
00468             odebug << "creating directory " << name << oendl;
00469             QString msg = "mkdir -p " + name;
00470             system( msg.latin1() );
00471         }
00472     }
00473     else
00474         name = outputTo;
00475 
00476     name.append( prefix );
00477     if ( appendSettings )
00478     {
00479         name.append( QString().sprintf( "_%d_%d_q%d", captureX, captureY, quality ) );
00480     }
00481     name.append( QString().sprintf( "-%d.%s", _pics++, (const char*) captureFormat.lower() ) );
00482 
00483     QImage i;
00484     ZCameraIO::instance()->captureFrame( captureX, captureY, zoom, &i );
00485     QImage im = i.convertDepth( 32 );
00486     bool result = im.save( name, format, quality );
00487     if ( !result )
00488     {
00489         oerr << "imageio-Problem while writing." << oendl;
00490         Global::statusMessage( "Error!" );
00491     }
00492     else
00493     {
00494         odebug << captureFormat << "-image has been successfully captured" << oendl;
00495         Global::statusMessage( "Ok." );
00496     }
00497 }
00498 
00499 
00500 void CameraMainWindow::startVideoCapture()
00501 {
00502     ODevice::inst()->playTouchSound();
00503     ODevice::inst()->setLedState( Led_Mail, Led_BlinkSlow );
00504 
00505     _capturefd = ::open( CAPTUREFILE, O_WRONLY | O_CREAT | O_TRUNC );
00506     if ( _capturefd == -1 )
00507     {
00508         owarn << "can't open capture file: " << strerror(errno) << oendl;
00509         return;
00510     }
00511 
00512     _capturebuf = new unsigned char[captureX*captureY*2];
00513     _capturing = true;
00514     _videopics = 0;
00515     _framerate = 0;
00516     updateCaption();
00517     _time.start();
00518     preview->setRefreshingRate( 1000 );
00519     startTimer( 100 ); // too fast but that is ok
00520 }
00521 
00522 
00523 void CameraMainWindow::timerEvent( QTimerEvent* )
00524 {
00525     if ( !_capturing )
00526     {
00527         odebug << "timer event in CameraMainWindow without capturing video ?" << oendl;
00528         return;
00529     }
00530 
00531     odebug << "timer event during video - now capturing frame #" << _videopics+1 << oendl;
00532 
00533     ZCameraIO::instance()->captureFrame( captureX, captureY, zoom, _capturebuf );
00534     _videopics++;
00535     ::write( _capturefd, _capturebuf, captureX*captureY*2 );
00536     setCaption( QString().sprintf( "Capturing %dx%d @ %.2f fps %d",
00537         captureX, captureY, 1000.0 / (_time.elapsed()/_videopics), _videopics ) );
00538 }
00539 
00540 
00541 void CameraMainWindow::stopVideoCapture()
00542 {
00543     killTimers();
00544     ODevice::inst()->playTouchSound();
00545     ODevice::inst()->setLedState( Led_Mail, Led_Off );
00546     _capturing = false;
00547     updateCaption();
00548     ::close( _capturefd );
00549     _framerate = static_cast<int>( 1000.0 / (_time.elapsed()/_videopics) );
00550 
00551     QString name;
00552     if ( outputTo == "Documents Folder" )
00553     {
00554         name.sprintf(  "%s/Documents/video/%s/", (const char*) QDir::homeDirPath(), (const char*) captureFormat.lower() );
00555         if ( !QDir( name ).exists() )
00556         {
00557             odebug << "creating directory " << name << oendl;
00558             QString msg = "mkdir -p " + name;
00559             system( msg.latin1() );
00560         }
00561     }
00562     else
00563         name = outputTo;
00564 
00565     name.append( "/" ); // sure is sure and safe is safe ;-)
00566     name.append( prefix );
00567     if ( appendSettings )
00568         name.append( QString().sprintf( "_%d_%d_q%d_%dfps", captureX, captureY, quality, _framerate ) );
00569     name.append( QString().sprintf( "-%d.%s", _videos++, (const char*) captureFormat.lower() ) );
00570     postProcessVideo( CAPTUREFILE, name );
00571 
00572     #ifndef QT_NO_DEBUG
00573     preview->setRefreshingRate( 1500 );
00574     #else
00575     preview->setRefreshingRate( 200 );
00576     #endif
00577 
00578     //delete[] _capturebuf; //FIXME: close memory leak
00579 }
00580 
00581 void CameraMainWindow::postProcessVideo( const QString& infile, const QString& outfile )
00582 {
00583     odebug << "post processing " << infile << " --> " << outfile << oendl;
00584 
00585     preview->setRefreshingRate( 0 );
00586 
00587     /*
00588     unsigned char buf[153600];
00589 
00590     int fd = ::open( "/var/compile/opie/noncore/multimedia/camera/capture-320x240.dat", O_RDONLY );
00591     ::read( fd, &buf, 153600 );
00592     QImage i;
00593     bufferToImage( 240, 320, (unsigned char*) &buf, &i );
00594     QPixmap p;
00595     p.convertFromImage( i );
00596     preview->setPixmap( p );
00597     imageToFile( &i, "/tmp/tmpfile", "JPEG", 100 );
00598     return;
00599     */
00600 
00601     QDialog* fr = new QDialog( this, "splash", false, QWidget::WStyle_StaysOnTop ); //, false, QWidget::WStyle_NoBorder | QWidget::WStyle_Customize );
00602     fr->setCaption( "Please wait..." );
00603     QVBoxLayout* box = new QVBoxLayout( fr, 2, 2 );
00604     QProgressBar* bar = new QProgressBar( fr );
00605     bar->setCenterIndicator( true );
00606     bar->setTotalSteps( _videopics-1 );
00607     QLabel* label = new QLabel( "Post processing frame bla/bla", fr );
00608     box->addWidget( bar );
00609     box->addWidget( label );
00610     fr->show();
00611     label->show();
00612     bar->show();
00613     fr->repaint();
00614     qApp->processEvents();
00615 
00616     // open files
00617 
00618     int infd = ::open( (const char*) infile, O_RDONLY );
00619     if ( infd == -1 )
00620     {
00621         owarn << "couldn't open capture file: " << strerror(errno) << oendl;
00622         return;
00623     }
00624 
00625     int outfd = ::open( (const char*) outfile, O_CREAT | O_WRONLY | O_TRUNC, 0644 );
00626     if ( outfd == -1 )
00627     {
00628         owarn << "couldn't open output file: " << strerror(errno) << oendl;
00629         return;
00630     }
00631 
00632     int framesize = captureX*captureY*2;
00633 
00634     unsigned char* inbuffer = new unsigned char[ framesize ];
00635     QImage image;
00636 
00637     avi_start( outfd, _videopics ); // write preambel
00638 
00639     // post process
00640 
00641     for ( int i = 0; i < _videopics; ++i )
00642     {
00643         odebug << "processing frame " << i << oendl;
00644 
00645         // <gui>
00646         label->setText( QString().sprintf( "Post processing frame %d / %d", i+1, _videopics ) );
00647         bar->setProgress( i );
00648         bar->repaint();
00649         qApp->processEvents();
00650         // </gui>
00651 
00652         int read = ::read( infd, inbuffer, framesize );
00653         odebug << "read " << read << " bytes" << oendl;
00654         bufferToImage( captureX, captureY, inbuffer, &image );
00655 
00656         QPixmap p;
00657         p.convertFromImage( image );
00658         preview->setPixmap( p );
00659         preview->repaint();
00660         qApp->processEvents();
00661 
00662         #ifdef CAMERA_EXTRA_DEBUG
00663         QString tmpfilename;
00664         tmpfilename.sprintf( "/tmp/test/%04d.jpg", i );
00665         #else
00666         QString tmpfilename( "/tmp/tempfile" );
00667         #endif
00668 
00669         imageToFile( &image, tmpfilename, "JPEG", quality );
00670 
00671         QFile framefile( tmpfilename );
00672         if ( !framefile.open( IO_ReadOnly ) )
00673         {
00674             oerr << "can't process file: %s" << strerror(errno) << oendl;
00675             return; // TODO: clean up temp ressources
00676         }
00677 
00678         int filesize = framefile.size();
00679         odebug << "filesize for frame " << i << " = " << filesize << oendl;
00680 
00681         unsigned char* tempbuffer = new unsigned char[ filesize ];
00682         framefile.readBlock( (char*) tempbuffer, filesize );
00683         avi_add( outfd, tempbuffer, filesize );
00684         delete tempbuffer;
00685         framefile.close();
00686 
00687     }
00688 
00689     avi_end( outfd, captureX, captureY, _framerate );
00690     ::close( outfd );
00691     ::close( infd );
00692 
00693     label->setText( "deleting temp files..." );
00694     qApp->processEvents();
00695     odebug << "deleting temporary capturefile " << infile << oendl;
00696     QFile::remove( infile );
00697 
00698     fr->hide();
00699     delete fr;
00700 
00701     updateCaption();
00702 
00703 }
00704 
00705 
00706 void CameraMainWindow::updateCaption()
00707 {
00708     if ( !_capturing )
00709         setCaption( QString().sprintf( "Opie-Camera: %dx%d %s q%d z%d (%s)", captureX, captureY, (const char*) captureFormat.lower(), quality, zoom, (const char*) flip ) );
00710     else
00711         setCaption( "Opie-Camera: => CAPTURING <=" );
00712     qApp->processEvents();
00713 }
00714 
00715 
00716 #ifndef QT_NO_DEBUG
00717 void CameraMainWindow::doSomething()
00718 {
00719     captureX = 240;
00720     captureY = 320;
00721     _videopics = 50;
00722     _framerate = 5;
00723     postProcessVideo( "/var/compile/opie/noncore/multimedia/camera/capture.dat",
00724     "/tmp/output.avi" );
00725 }
00726 #else
00727 void CameraMainWindow::doSomething()
00728 {
00729 }
00730 #endif
00731 

Generated on Sat Nov 5 16:15:52 2005 for OPIE by  doxygen 1.4.2