• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

examples/example7/CameraThread.cpp

Go to the documentation of this file.
00001 
00003 #include <qmetatype.h>
00004 #include "CameraThread.h"
00005  
00006 #include <FCam/N900.h>
00007 
00008 #include <vector>
00009 #include <iostream>
00010 
00011 #include "OverlayWidget.h"
00012 
00013 using namespace std;
00014 
00015 namespace Plat = FCam::N900;
00016 
00017 void CameraThread::run() {
00018 
00019     // Make an asynchronous file writer to save images in the background    
00020     FCam::AsyncFileWriter writer;   
00021     Plat::Sensor sensor;
00022     Plat::Lens lens;
00023     Plat::Flash flash;
00024     
00025     // tell the sensor that the flash and the lens will be tagging
00026     // frames that come back from it
00027     sensor.attach(&flash);
00028     sensor.attach(&lens);
00029     
00030     // Make a helper autofocus object
00031     FCam::AutoFocus autoFocus(&lens);
00032     
00033     // The viewfinder shot
00034     FCam::Shot viewfinder;
00035     viewfinder.exposure = 40000;
00036     viewfinder.gain = 1.0f;
00037     // run at 25 fps
00038     viewfinder.frameTime = 40000;
00039     // dump image data directly into the frame buffer
00040     viewfinder.image = overlay->framebuffer(); 
00041     // enable histograms and sharpness maps
00042     viewfinder.histogram.enabled = true;
00043     viewfinder.histogram.region = FCam::Rect(0, 0, 640, 480);
00044     viewfinder.sharpness.enabled = true;
00045     viewfinder.sharpness.size = FCam::Size(16, 12);
00046     
00047     // A full 5MP photograph. We'll set the exposure, frameTime, and
00048     // gain later, after we meter. Default parameters apply (no
00049     // histograms or sharpness), image memory auto allocated for each
00050     // new photograph, so that we can have multiple unique photographs
00051     // saving at once.
00052     FCam::Shot photo;
00053     photo.image = FCam::Image(2592, 1968, FCam::RAW, FCam::Image::AutoAllocate);
00054     
00055     bool takeSnapshot = false;
00056     bool halfDepress = false;
00057     bool fullDepress = false;
00058     
00059     // stream the viewfinder
00060     sensor.stream(viewfinder);
00061     
00062     while (keepGoing) {
00063         // deal with FCam events
00064         FCam::Event e;
00065         while (FCam::getNextEvent(&e)) {
00066             cout << e.description << endl;
00067             switch(e.type) {
00068             case FCam::Event::FocusPressed:
00069                 if (autoFocus.idle()) autoFocus.startSweep();       
00070                 halfDepress = true;
00071                 break;
00072             case FCam::Event::FocusReleased:
00073                 halfDepress = false;
00074                 break;
00075             case FCam::Event::ShutterPressed:
00076                 takeSnapshot = true;        
00077                 fullDepress = true;
00078                 break;
00079             case FCam::Event::ShutterReleased:
00080                 fullDepress = false;
00081             };
00082         }
00083     
00084         // Take a picture once autofocus completes and we have space to store the frame       
00085         if (takeSnapshot && autoFocus.idle() && writer.savesPending() < 8) {
00086             // use the metering the viewfinder has been doing
00087             photo.exposure  = viewfinder.exposure;
00088             photo.gain      = viewfinder.gain;
00089             photo.whiteBalance = viewfinder.whiteBalance;
00090             sensor.capture(photo);
00091             takeSnapshot = false;
00092         }
00093     
00094         // Drain the queue
00095         FCam::Frame f;
00096         do {
00097             f = sensor.getFrame();
00098         
00099             if (f.shot().id == photo.id) {
00100                 // Our photo came back, asynchronously save it to disk
00101                 // with a unique filename. We use the exposure start
00102                 // time for now just so we don't have to keep a
00103                 // globally unique numbering.
00104                 if (!f.image().valid()) {
00105                     printf("ERROR: Photo dropped!\n");
00106                     continue;
00107                 } else {
00108                     printf("Got a 5MP frame\n");
00109                 }
00110 
00111                 // Save it as a DNG
00112                 char fname[256];
00113                 snprintf(fname, 255, "/home/user/MyDocs/photo_%s.dng", 
00114                          f.exposureStartTime().toString().c_str());
00115                 writer.saveDNG(f, fname);
00116 
00117                 // Save it as a JPEG too
00118                 snprintf(fname, 255, "/home/user/MyDocs/photo_%s.jpg", 
00119                          f.exposureStartTime().toString().c_str());
00120                 writer.saveJPEG(f, fname, 90);
00121             } else {
00122 
00123                 // update the autofocus and metering algorithms     
00124                 autoFocus.update(f);
00125                 autoExpose(&viewfinder, f);
00126                 autoWhiteBalance(&viewfinder, f);
00127                 sensor.stream(viewfinder);      
00128             }
00129         } while (sensor.framesPending());
00130     }   
00131 }

Generated on Fri Sep 24 2010 15:52:59 for FCam by  doxygen 1.7.1