00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "capture.h"
00017
00018 #include "zcameraio.h"
00019 #include "imageio.h"
00020 #include "avi.h"
00021
00022 #include <opie2/oapplication.h>
00023 #include <opie2/odebug.h>
00024
00025 #include <qimage.h>
00026
00027 using namespace Opie::Core;
00028 using namespace Opie::Core;
00029 Capturer::Capturer()
00030 :QFrame( 0 ), height( 320 ), width( 240 ), zoom( 1 ), quality( 90 ),
00031 flip( "A" ), format( "JPEG" ), name( "Untitled" )
00032 {
00033 }
00034
00035
00036 Capturer::~Capturer()
00037 {
00038 }
00039
00040
00041 void Capturer::checkSettings()
00042 {
00043 if ( width > height )
00044 {
00045 if ( 0 != width % 16 || width < 16 || width > 640 )
00046 {
00047 printf( "Warning: Corrected X resolution to 320 px\n" );
00048 width = 320;
00049 }
00050 if ( 0 != height % 16 || height < 16 || height > 480 )
00051 {
00052 printf( "Warning: Corrected Y resolution to 240 px\n" );
00053 height = 240;
00054 }
00055 }
00056 else
00057 {
00058 if ( 0 != width % 16 || width < 16 || width > 480 )
00059 {
00060 printf( "Warning: Corrected X resolution to 240 px\n" );
00061 width = 240;
00062 }
00063 if ( 0 != height % 16 || height < 16 || height > 640 )
00064 {
00065 printf( "Warning: Corrected Y resolution to 320 px\n" );
00066 height = 320;
00067 }
00068 }
00069
00070 if ( quality > 100 || quality < 10 )
00071 {
00072 printf( "Warning: Corrected quality to 75%%\n" );
00073 quality = 75;
00074 }
00075
00076 if ( zoom > 2 || zoom < 1 )
00077 {
00078 printf( "Warning: Corrected zoom to x1\n" );
00079 zoom = 1;
00080 }
00081
00082 if ( format != "JPEG" && format != "PNG" && format != "BMP" )
00083 {
00084 printf( "Warning: Corrected format to 'JPEG'\n" );
00085 format = "JPEG";
00086 }
00087 }
00088
00089
00090 void Capturer::capture()
00091 {
00092 if ( flip == "A" )
00093 ZCameraIO::instance()->setFlip( ZCameraIO::AUTOMATICFLIP );
00094 else if ( flip == "0" )
00095 ZCameraIO::instance()->setFlip( ZCameraIO::XNOFLIP | ZCameraIO::YNOFLIP );
00096 else if ( flip == "X" )
00097 ZCameraIO::instance()->setFlip( ZCameraIO::XFLIP );
00098 else if ( flip == "Y" )
00099 ZCameraIO::instance()->setFlip( ZCameraIO::YFLIP );
00100 else if ( flip == "*" )
00101 ZCameraIO::instance()->setFlip( ZCameraIO::XFLIP | ZCameraIO::YFLIP );
00102
00103 ZCameraIO::instance()->captureFrame( width, height, zoom, &image );
00104 QImage im = image.convertDepth( 32 );
00105 bool result = im.save( name, format, quality );
00106 if ( !result )
00107 {
00108 printf( "QImageio-Problem while writing.\n" );
00109 }
00110 else
00111 {
00112 printf( "Ok.\n" );
00113 }
00114 }
00115
00116
00117 void usage()
00118 {
00119 printf( "Usage: ./capture [options] filename\n\n" );
00120 printf( " -x xresolution (dividable by 16) [default=240]\n" );
00121 printf( " -y xresolution (dividable by 16) [default=320]\n" );
00122 printf( " -q quality (10-100) [default=75]\n" );
00123 printf( " -f flip (A=auto, 0, X, Y, *=both) [default=Auto]\n" );
00124 printf( " -o output format (JPEG,BMP,PNG) [default=JPEG]\n" );
00125 printf( " -z zoom (1-2) [default=1]\n" );
00126 }
00127
00128 int main( int argc, char** argv )
00129 {
00130 OApplication* a = new OApplication( argc, argv, "Capture" );
00131 Capturer* c = new Capturer();
00132
00133 if ( argc < 2 )
00134 {
00135 usage();
00136 return -1;
00137 }
00138
00139 #define I_HATE_WRITING_HARDCODED_PARSES
00140
00141 int i = 1;
00142 while ( i < argc )
00143 {
00144
00145 if ( argv[i][0] != '-' )
00146 {
00147 if ( argc != i+1 )
00148 {
00149 usage();
00150 return -1;
00151 }
00152 else
00153 {
00154 c->name = argv[i];
00155 break;
00156 }
00157 }
00158 else
00159 {
00160 i++;
00161 if ( argc == i )
00162 {
00163 usage();
00164 return -1;
00165 }
00166 switch ( argv[i-1][1] )
00167 {
00168 case 'x': c->width = QString( argv[i] ).toInt(); break;
00169 case 'y': c->height = QString( argv[i] ).toInt(); break;
00170 case 'z': c->zoom = QString( argv[i] ).toInt(); break;
00171 case 'o': c->format = QString( argv[i] ); break;
00172 case 'q': c->quality = QString( argv[i] ).toInt(); break;
00173 case 'f': c->flip = QString( argv[i] )[0]; break;
00174 default: usage(); return -1;
00175 }
00176 i++;
00177 }
00178
00179 #undef I_HATE_WRITING_HARDCODED_PARSES
00180 }
00181
00182 if ( !ZCameraIO::instance()->isOpen() )
00183 {
00184 printf( "Error: Can't detect your camera. Exiting.\n" );
00185 return -1;
00186 }
00187
00188 c->checkSettings();
00189 c->capture();
00190
00191 delete c;
00192 delete a;
00193 return 0;
00194 }
00195