-rw-r--r-- | noncore/multimedia/camera/cmd/capture.cpp | 162 | ||||
-rw-r--r-- | noncore/multimedia/camera/cmd/capture.h | 45 | ||||
-rw-r--r-- | noncore/multimedia/camera/cmd/cmd.pro | 4 | ||||
-rw-r--r-- | noncore/multimedia/camera/lib/zcameraio.cpp | 8 |
4 files changed, 156 insertions, 63 deletions
diff --git a/noncore/multimedia/camera/cmd/capture.cpp b/noncore/multimedia/camera/cmd/capture.cpp index 7960bbb..64c223c 100644 --- a/noncore/multimedia/camera/cmd/capture.cpp +++ b/noncore/multimedia/camera/cmd/capture.cpp @@ -1,146 +1,190 @@ /********************************************************************** ** Copyright (C) 2003 Michael 'Mickey' Lauer. All rights reserved. ** ** This file is part of Opie Environment. ** ** This file may be distributed and/or modified under the terms of the ** GNU General Public License version 2 as published by the Free Software ** Foundation and appearing in the file LICENSE.GPL included in the ** packaging of this file. ** ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ** **********************************************************************/ +#include "capture.h" + #include "zcameraio.h" #include "imageio.h" #include "avi.h" -#include <qfile.h> -#include <qimage.h> #include <opie2/oapplication.h> +#include <opie2/odebug.h> + +#include <qimage.h> -#include <assert.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <fcntl.h> -#include <string.h> -#include <errno.h> -#include <unistd.h> -#include <stdio.h> +Capturer::Capturer() + :QFrame( 0 ), height( 320 ), width( 240 ), zoom( 1 ), quality( 90 ), + flip( "A" ), format( "JPEG" ), name( "Untitled" ) +{ +} -#define CAPTUREFILE "/tmp/capture.dat" -#define OUTPUTFILE "/tmp/output.avi" -int captureX = 240; -int captureY = 320; -int quality = 75; -QString flip = "A"; -int zoom = 1; -QString format = "JPG"; -QString name; +Capturer::~Capturer() +{ +} + + +void Capturer::checkSettings() +{ + if ( width > height ) + { + if ( 0 != width % 16 || width < 16 || width > 640 ) + { + printf( "Warning: Corrected X resolution to 320 px\n" ); + width = 320; + } + if ( 0 != height % 16 || height < 16 || height > 480 ) + { + printf( "Warning: Corrected Y resolution to 240 px\n" ); + height = 240; + } + } + else + { + if ( 0 != width % 16 || width < 16 || width > 480 ) + { + printf( "Warning: Corrected X resolution to 240 px\n" ); + width = 240; + } + if ( 0 != height % 16 || height < 16 || height > 640 ) + { + printf( "Warning: Corrected Y resolution to 320 px\n" ); + height = 320; + } + } + + if ( quality > 100 || quality < 10 ) + { + printf( "Warning: Corrected quality to 75%%\n" ); + quality = 75; + } + + if ( zoom > 2 || zoom < 1 ) + { + printf( "Warning: Corrected zoom to x1\n" ); + zoom = 1; + } + + if ( format != "JPEG" && format != "PNG" && format != "BMP" ) + { + printf( "Warning: Corrected format to 'JPEG'\n" ); + format = "JPEG"; + } +} + + +void Capturer::capture() +{ + if ( flip == "A" ) + ZCameraIO::instance()->setFlip( ZCameraIO::AUTOMATICFLIP ); + else if ( flip == "0" ) + ZCameraIO::instance()->setFlip( ZCameraIO::XNOFLIP | ZCameraIO::YNOFLIP ); + else if ( flip == "X" ) + ZCameraIO::instance()->setFlip( ZCameraIO::XFLIP ); + else if ( flip == "Y" ) + ZCameraIO::instance()->setFlip( ZCameraIO::YFLIP ); + else if ( flip == "*" ) + ZCameraIO::instance()->setFlip( ZCameraIO::XFLIP | ZCameraIO::YFLIP ); + + ZCameraIO::instance()->captureFrame( width, height, zoom, &image ); + QImage im = image.convertDepth( 32 ); + bool result = im.save( name, format, quality ); + if ( !result ) + { + printf( "QImageio-Problem while writing.\n" ); + } + else + { + printf( "Ok.\n" ); + } +} -int performCapture(); void usage() { printf( "Usage: ./capture [options] filename\n\n" ); printf( " -x xresolution (dividable by 16) [default=240]\n" ); printf( " -y xresolution (dividable by 16) [default=320]\n" ); printf( " -q quality (10-100) [default=75]\n" ); - printf( " -f flip (A=auto, X, Y, *=both) [default=Auto]\n" ); - printf( " -o output format (JPG,BMP,PNG) [default=JPG]\n" ); + printf( " -f flip (A=auto, 0, X, Y, *=both) [default=Auto]\n" ); + printf( " -o output format (JPEG,BMP,PNG) [default=JPEG]\n" ); printf( " -z zoom (1-2) [default=1]\n" ); } int main( int argc, char** argv ) { - OApplication* a = new OApplication( argc, argv, "opie-camera" ); + OApplication* a = new OApplication( argc, argv, "Capture" ); + Capturer* c = new Capturer(); if ( argc < 2 ) { usage(); return -1; } - int captureX = 240; - int captureY = 320; - QString flip = "A"; - #define I_HATE_WRITING_HARDCODED_PARSES int i = 1; while ( i < argc ) { // check for filename if ( argv[i][0] != '-' ) { if ( argc != i+1 ) { usage(); return -1; } else { - name = argv[i]; + c->name = argv[i]; break; } } else { i++; if ( argc == i ) { usage(); return -1; } switch ( argv[i-1][1] ) { - case 'x': captureX = QString( argv[i] ).toInt(); break; - case 'y': captureY = QString( argv[i] ).toInt(); break; - case 'z': zoom = QString( argv[i] ).toInt(); break; - case 'o': format = QString( argv[i] ); break; - case 'q': quality = QString( argv[i] ).toInt(); break; - case 'f': flip = QString( argv[i] )[0]; break; + case 'x': c->width = QString( argv[i] ).toInt(); break; + case 'y': c->height = QString( argv[i] ).toInt(); break; + case 'z': c->zoom = QString( argv[i] ).toInt(); break; + case 'o': c->format = QString( argv[i] ); break; + case 'q': c->quality = QString( argv[i] ).toInt(); break; + case 'f': c->flip = QString( argv[i] )[0]; break; default: usage(); return -1; } i++; } #undef I_HATE_WRITING_HARDCODED_PARSES } if ( !ZCameraIO::instance()->isOpen() ) { printf( "Error: Can't detect your camera. Exiting.\n" ); return -1; } - return performCapture(); -} - -int performCapture() -{ - printf( "Capturing %dx%d Image [%s] q%d z%d --> %s\n", captureX, - captureY, - (const char*) format, - quality, - zoom, - (const char*) name ); - - QImage i; - ZCameraIO::instance()->captureFrame( captureX, captureY, zoom, &i ); - QImage im = i.convertDepth( 32 ); - bool result = im.save( name, format, quality ); - if ( !result ) - { - printf( "QImageio-Problem while writing.\n" ); - return -1; - } - else - { - printf( "Ok.\n" ); - } + c->checkSettings(); + c->capture(); + return 0; } diff --git a/noncore/multimedia/camera/cmd/capture.h b/noncore/multimedia/camera/cmd/capture.h new file mode 100644 index 0000000..f23aca0 --- a/dev/null +++ b/noncore/multimedia/camera/cmd/capture.h @@ -0,0 +1,45 @@ +/********************************************************************** +** Copyright (C) 2003 Michael 'Mickey' Lauer. All rights reserved. +** +** This file is part of Opie Environment. +** +** This file may be distributed and/or modified under the terms of the +** GNU General Public License version 2 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +**********************************************************************/ + +#include "zcameraio.h" +#include "imageio.h" +#include "avi.h" + +#include <qfile.h> +#include <qframe.h> +#include <qimage.h> + +class Capturer: public QFrame +{ + Q_OBJECT + + public: + Capturer(); + virtual ~Capturer(); + + int height; + int width; + int zoom; + int quality; + QString flip; + QString format; + QString name; + QImage image; + + public slots: + void capture(); + void checkSettings(); +}; + diff --git a/noncore/multimedia/camera/cmd/cmd.pro b/noncore/multimedia/camera/cmd/cmd.pro index 3b1aaa6..a5791f4 100644 --- a/noncore/multimedia/camera/cmd/cmd.pro +++ b/noncore/multimedia/camera/cmd/cmd.pro @@ -1,18 +1,18 @@ MOC_DIR = ./moc OBJECTS_DIR = ./obj DESTDIR = $(OPIEDIR)/bin TEMPLATE = app CONFIG = qt warn_on debug -HEADERS = +HEADERS = capture.h SOURCES = capture.cpp INCLUDEPATH += $(OPIEDIR)/include ../lib DEPENDPATH += $(OPIEDIR)/include ../lib -LIBS += -lqpe -lopiecore2 -lopiecam +LIBS += -lopiecore2 -lopiecam INTERFACES = TARGET = capture include ( $(OPIEDIR)/include.pro ) diff --git a/noncore/multimedia/camera/lib/zcameraio.cpp b/noncore/multimedia/camera/lib/zcameraio.cpp index c940b45..d59cbbb 100644 --- a/noncore/multimedia/camera/lib/zcameraio.cpp +++ b/noncore/multimedia/camera/lib/zcameraio.cpp @@ -1,311 +1,315 @@ /********************************************************************** ** Copyright (C) 2002 Michael 'Mickey' Lauer. All rights reserved. ** ** This file is part of Opie Environment. ** ** This file may be distributed and/or modified under the terms of the ** GNU General Public License version 2 as published by the Free Software ** Foundation and appearing in the file LICENSE.GPL included in the ** packaging of this file. ** ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ** **********************************************************************/ #include "zcameraio.h" #include <sys/types.h> #include <sys/stat.h> #include <errno.h> #include <string.h> #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <qimage.h> #include <qdatetime.h> #include <opie2/odebug.h> #define SHARPZDC "/dev/sharp_zdc" ZCameraIO* ZCameraIO::_instance = 0; ZCameraIO* ZCameraIO::instance() { if ( !ZCameraIO::_instance ) { odebug << "Creating ZCameraIO::_instance" << oendl; ZCameraIO::_instance = new ZCameraIO(); } return ZCameraIO::_instance; } ZCameraIO::ZCameraIO() : _pressed( false ), _height( 0 ), _width( 0 ), _zoom( 0 ), _flip( -1 ), _rot( 0 ), _readlen( 0 ) { _driver = ::open( SHARPZDC, O_RDWR ); if ( _driver == -1 ) oerr << "Can't open camera driver: " << strerror(errno) << oendl; else init(); } void ZCameraIO::init() { if ( ZCameraIO::_instance ) ofatal << "Don't create more than one ZCameraIO instances." << oendl; else { _timer = new QTime(); setReadMode( STATUS ); } } ZCameraIO::~ZCameraIO() { if ( _driver != -1 ) { setReadMode( 0 ); ::close( _driver ); } } bool ZCameraIO::isOpen() const { return _driver != -1; } bool ZCameraIO::isShutterPressed() { if ( _status[0] == 'S' ) { if ( !_pressed ) // wasn't pressed before, but is now! { _pressed = true; _timer->start(); return true; } if ( _timer->elapsed() > 2000 ) // the press is pretty old now { clearShutterLatch(); _status[0] = 's'; _pressed = false; } } return false; } bool ZCameraIO::isFinderReversed() const { return _status[1] == 'M'; } bool ZCameraIO::isCapturing() const { return _status[2] == 'C'; } bool ZCameraIO::isAvailable() const { return _status[3] == 'A'; } bool ZCameraIO::setCaptureFrame( int width, int height, int zoom, bool rot ) { odebug << "setCaptureFrame( " << width << ", " << height << ", " << zoom << ", " << rot << " )" << oendl; char b[100]; sprintf( b, "%c=%d,%d,%d,%d", rot ? 'R':'S', width, height, zoom, width*2 ); if ( write( b ) ) { _width = width; _height = height; _zoom = zoom; _rot = rot; _readlen = 2 * _width * _height; // camera is fixed @ 16 bits per pixel return true; } + owarn << "couldn't write to driver" << oendl; return false; } bool ZCameraIO::setZoom( int zoom ) { return setCaptureFrame( _width, _height, zoom*256, _rot ); } void ZCameraIO::setReadMode( int mode ) { char b[10]; sprintf( b, "M=%d", mode ); write( b, mode <= 9 ? 3 : 4 ); if ( mode & STATUS ) // STATUS bit is set { read( _status, 4 ); if ( isShutterPressed() ) { emit shutterClicked(); } } } void ZCameraIO::setFlip( int flip ) { _flip = flip; } void ZCameraIO::clearShutterLatch() { write( "B", 1 ); } bool ZCameraIO::read( char* b, int len ) { #ifndef NO_TIMING QTime t; t.start(); #endif int rlen = ::read( _driver, b, len ); #ifndef NO_TIMING int time = t.elapsed(); #else int time = -1; #endif if ( rlen ) odebug << "read " << rlen << " ('" << b[0] << b[1] << b[2] << b[3] << "') [" << time << " ms] from driver." << oendl; else odebug << "read nothing from driver." << oendl; return rlen == len; } bool ZCameraIO::write( char* buf, int len ) { if ( !len ) len = strlen( buf ); odebug << "writing '" << buf << "' to driver." << oendl; return ::write( _driver, buf, len ) == len; } bool ZCameraIO::snapshot( QImage* image ) { setReadMode( STATUS ); odebug << "finder reversed = " << isFinderReversed() << oendl; odebug << "rotation = " << _rot << oendl; + odebug << "w=" << _width << " h= " << _height << " readlen= " << _readlen << oendl; + int readmode; if ( _flip == -1 ) // AUTO { if ( _rot ) // Portrait { readmode = IMAGE | isFinderReversed() ? XFLIP | YFLIP : 0; } else // Landscape { readmode = IMAGE | XFLIP | YFLIP; } } else // OVERRIDE { readmode = IMAGE | _flip; } setReadMode( readmode ); char buf[_readlen]; char* bp = buf; unsigned char* p; read( bp, _readlen ); image->create( _width, _height, 16 ); for ( int i = 0; i < _height; ++i ) { p = image->scanLine( i ); for ( int j = 0; j < _width; j++ ) { *p = *bp; p++; bp++; *p = *bp; p++; bp++; } } return true; } bool ZCameraIO::snapshot( unsigned char* buf ) { setReadMode( STATUS ); odebug << "finder reversed = " << isFinderReversed() << oendl; odebug << "rotation = " << _rot << oendl; int readmode; if ( _flip == -1 ) // AUTO { if ( _rot ) // Portrait { readmode = IMAGE | isFinderReversed() ? XFLIP | YFLIP : 0; } else // Landscape { readmode = IMAGE | XFLIP | YFLIP; } } else // OVERRIDE { readmode = IMAGE | _flip; } setReadMode( readmode ); read( (char*) buf, _readlen ); } void ZCameraIO::captureFrame( int w, int h, int zoom, QImage* image ) { + int prot = _rot; int pw = _width; int ph = _height; - setCaptureFrame( w, h, zoom*256, _rot ); + setCaptureFrame( w, h, zoom*256, w<h ); snapshot( image ); - setCaptureFrame( pw, ph, _zoom, _rot ); + setCaptureFrame( pw, ph, _zoom, prot ); } void ZCameraIO::captureFrame( int w, int h, int zoom, unsigned char* buf ) { //FIXME: this is too slow int pw = _width; int ph = _height; setCaptureFrame( w, h, zoom*256, _rot ); snapshot( buf ); setCaptureFrame( pw, ph, _zoom, _rot ); } |