author | mickeyl <mickeyl> | 2003-04-06 14:38:18 (UTC) |
---|---|---|
committer | mickeyl <mickeyl> | 2003-04-06 14:38:18 (UTC) |
commit | 0b985ce87d6ce6d4110a50d5be48831d34794cd2 (patch) (unidiff) | |
tree | da19d8dc2cd2b37d15220783940652e8e91f3843 /noncore/multimedia/camera/zcameraio.cpp | |
parent | 91d65a97c956963a24f418fadd7cd69f6a52f5d5 (diff) | |
download | opie-0b985ce87d6ce6d4110a50d5be48831d34794cd2.zip opie-0b985ce87d6ce6d4110a50d5be48831d34794cd2.tar.gz opie-0b985ce87d6ce6d4110a50d5be48831d34794cd2.tar.bz2 |
- add realtime preview widget with zoom control
The camera driver is damn slow. For a 240x160x16 image, it takes ~100ms. That gives approx. 10fps which is too slow for doing video. :(
Diffstat (limited to 'noncore/multimedia/camera/zcameraio.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/multimedia/camera/zcameraio.cpp | 58 |
1 files changed, 34 insertions, 24 deletions
diff --git a/noncore/multimedia/camera/zcameraio.cpp b/noncore/multimedia/camera/zcameraio.cpp index 7343dca..56b2e13 100644 --- a/noncore/multimedia/camera/zcameraio.cpp +++ b/noncore/multimedia/camera/zcameraio.cpp | |||
@@ -3,204 +3,214 @@ | |||
3 | ** | 3 | ** |
4 | ** This file is part of Opie Environment. | 4 | ** This file is part of Opie Environment. |
5 | ** | 5 | ** |
6 | ** This file may be distributed and/or modified under the terms of the | 6 | ** This file may be distributed and/or modified under the terms of the |
7 | ** GNU General Public License version 2 as published by the Free Software | 7 | ** GNU General Public License version 2 as published by the Free Software |
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | 8 | ** Foundation and appearing in the file LICENSE.GPL included in the |
9 | ** packaging of this file. | 9 | ** packaging of this file. |
10 | ** | 10 | ** |
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | 11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | 12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
13 | ** | 13 | ** |
14 | **********************************************************************/ | 14 | **********************************************************************/ |
15 | 15 | ||
16 | #include "zcameraio.h" | 16 | #include "zcameraio.h" |
17 | 17 | ||
18 | #include <sys/types.h> | 18 | #include <sys/types.h> |
19 | #include <sys/stat.h> | 19 | #include <sys/stat.h> |
20 | #include <errno.h> | 20 | #include <errno.h> |
21 | #include <string.h> | 21 | #include <string.h> |
22 | #include <fcntl.h> | 22 | #include <fcntl.h> |
23 | #include <unistd.h> | 23 | #include <unistd.h> |
24 | #include <stdio.h> | 24 | #include <stdio.h> |
25 | 25 | ||
26 | #include <qimage.h> | 26 | #include <qimage.h> |
27 | #include <qdatetime.h> | ||
27 | 28 | ||
28 | #include <opie2/odebug.h> | 29 | #include <opie2/odebug.h> |
29 | 30 | ||
31 | #define SHARPZDC "/dev/sharp_zdc" | ||
32 | |||
30 | ZCameraIO* ZCameraIO::_instance = 0; | 33 | ZCameraIO* ZCameraIO::_instance = 0; |
31 | 34 | ||
32 | ZCameraIO* ZCameraIO::instance() | 35 | ZCameraIO* ZCameraIO::instance() |
33 | { | 36 | { |
34 | if ( !ZCameraIO::_instance ) | 37 | if ( !ZCameraIO::_instance ) |
35 | { | 38 | { |
36 | odebug << "Creating ZCameraIO::_instance" << oendl; | 39 | odebug << "Creating ZCameraIO::_instance" << oendl; |
37 | ZCameraIO::_instance = new ZCameraIO(); | 40 | ZCameraIO::_instance = new ZCameraIO(); |
38 | } | 41 | } |
39 | return ZCameraIO::_instance; | 42 | return ZCameraIO::_instance; |
40 | } | 43 | } |
41 | 44 | ||
42 | 45 | ||
43 | ZCameraIO::ZCameraIO() | 46 | ZCameraIO::ZCameraIO() |
44 | :_height( 0 ), _width( 0 ), _zoom( 0 ), _rot( 0 ), _readlen( 0 ) | 47 | :_height( 0 ), _width( 0 ), _zoom( 0 ), _rot( 0 ), _readlen( 0 ) |
45 | { | 48 | { |
46 | _driver = ::open( "/dev/sharp_zdc", O_RDWR ); | 49 | _driver = ::open( SHARPZDC, O_RDWR ); |
47 | if ( _driver == -1 ) | 50 | if ( _driver == -1 ) |
48 | oerr << "Can't open camera driver: " << strerror(errno) << oendl; | 51 | oerr << "Can't open camera driver: " << strerror(errno) << oendl; |
49 | else | 52 | else |
50 | init(); | 53 | init(); |
51 | } | 54 | } |
52 | 55 | ||
53 | 56 | ||
54 | void ZCameraIO::init() | 57 | void ZCameraIO::init() |
55 | { | 58 | { |
56 | if ( ZCameraIO::_instance ) | 59 | if ( ZCameraIO::_instance ) |
57 | ofatal << "Don't create more than one ZCameraIO instances." << oendl; | 60 | ofatal << "Don't create more than one ZCameraIO instances." << oendl; |
58 | else | 61 | else |
59 | { | 62 | { |
60 | setReadMode( STATUS ); | 63 | setReadMode( STATUS ); |
61 | } | 64 | } |
62 | } | 65 | } |
63 | 66 | ||
64 | 67 | ||
65 | ZCameraIO::~ZCameraIO() | 68 | ZCameraIO::~ZCameraIO() |
66 | { | 69 | { |
67 | if ( _driver != -1 ) | 70 | if ( _driver != -1 ) |
71 | { | ||
72 | setReadMode( 0 ); | ||
68 | ::close( _driver ); | 73 | ::close( _driver ); |
74 | } | ||
69 | } | 75 | } |
70 | 76 | ||
71 | 77 | ||
72 | inline bool ZCameraIO::isOpen() const | 78 | inline bool ZCameraIO::isOpen() const |
73 | { | 79 | { |
74 | return _driver != -1; | 80 | return _driver != -1; |
75 | } | 81 | } |
76 | 82 | ||
77 | 83 | ||
78 | inline bool ZCameraIO::isShutterPressed() | 84 | inline bool ZCameraIO::isShutterPressed() |
79 | { | 85 | { |
80 | return _status[0] == 'S'; | 86 | return _status[0] == 'S'; |
81 | clearShutterLatch(); | 87 | clearShutterLatch(); |
82 | } | 88 | } |
83 | 89 | ||
84 | 90 | ||
85 | inline bool ZCameraIO::isFinderReversed() const | 91 | inline bool ZCameraIO::isFinderReversed() const |
86 | { | 92 | { |
87 | return _status[1] == 'M'; | 93 | return _status[1] == 'M'; |
88 | } | 94 | } |
89 | 95 | ||
90 | 96 | ||
91 | inline bool ZCameraIO::isCapturing() const | 97 | inline bool ZCameraIO::isCapturing() const |
92 | { | 98 | { |
93 | return _status[2] == 'C'; | 99 | return _status[2] == 'C'; |
94 | } | 100 | } |
95 | 101 | ||
96 | 102 | ||
97 | inline bool ZCameraIO::isAvailable() const | 103 | inline bool ZCameraIO::isAvailable() const |
98 | { | 104 | { |
99 | return _status[3] == 'A'; | 105 | return _status[3] == 'A'; |
100 | } | 106 | } |
101 | 107 | ||
102 | 108 | ||
103 | bool ZCameraIO::setCaptureFrame( int width, int height, int zoom, bool rot ) | 109 | bool ZCameraIO::setCaptureFrame( int width, int height, int zoom, bool rot ) |
104 | { | 110 | { |
105 | char b[100]; | 111 | char b[100]; |
106 | sprintf( b, "%c=%d,%d,%d,%d", rot ? 'R':'S', width, height, zoom, width*2 ); | 112 | sprintf( b, "%c=%d,%d,%d,%d", rot ? 'R':'S', width, height, zoom, width*2 ); |
107 | if ( write( b ) ) | 113 | if ( write( b ) ) |
108 | { | 114 | { |
109 | _width = width; | 115 | _width = width; |
110 | _height = _height; | 116 | _height = height; |
111 | _zoom = zoom; | 117 | _zoom = zoom; |
112 | _rot = rot; | 118 | _rot = rot; |
113 | _readlen = 2 * _width * _height; // camera is fixed @ 16 bits per pixel | 119 | _readlen = 2 * _width * _height; // camera is fixed @ 16 bits per pixel |
114 | return true; | 120 | return true; |
115 | } | 121 | } |
116 | return false; | 122 | return false; |
117 | } | 123 | } |
118 | 124 | ||
119 | 125 | ||
120 | void ZCameraIO::setReadMode( int mode ) | 126 | void ZCameraIO::setReadMode( int mode ) |
121 | { | 127 | { |
122 | char b[4]; | 128 | char b[10]; |
123 | sprintf( b, "M=%d", mode ); | 129 | sprintf( b, "M=%d", mode ); |
124 | write( b, mode <= 9 ? 3 : 4 ); | 130 | write( b, mode <= 9 ? 3 : 4 ); |
125 | if ( mode & 1 ) // STATUS bit is set | 131 | if ( mode & 1 ) // STATUS bit is set |
126 | read( _status, 4 ); | 132 | read( _status, 4 ); |
127 | } | 133 | } |
128 | 134 | ||
129 | 135 | ||
130 | void ZCameraIO::clearShutterLatch() | 136 | void ZCameraIO::clearShutterLatch() |
131 | { | 137 | { |
132 | char b = 'B'; | 138 | char b = 'B'; |
133 | write( &b, 1 ); | 139 | write( &b, 1 ); |
134 | } | 140 | } |
135 | 141 | ||
136 | 142 | ||
137 | bool ZCameraIO::read( char* b, int len ) | 143 | bool ZCameraIO::read( char* b, int len ) |
138 | { | 144 | { |
145 | #ifndef NO_TIMING | ||
146 | QTime t; | ||
147 | t.start(); | ||
148 | #endif | ||
139 | int rlen = ::read( _driver, b, len ); | 149 | int rlen = ::read( _driver, b, len ); |
140 | odebug << "read " << rlen << " from driver." << oendl; | 150 | #ifndef NO_TIMING |
151 | int time = t.elapsed(); | ||
152 | #else | ||
153 | int time = -1; | ||
154 | #endif | ||
155 | if ( rlen ) | ||
156 | odebug << "read " << rlen << " ('" << b[0] << b[1] << b[2] << b[3] << "') [" << time << " ms] from driver." << oendl; | ||
157 | else | ||
158 | odebug << "read nothing from driver." << oendl; | ||
141 | return rlen == len; | 159 | return rlen == len; |
142 | } | 160 | } |
143 | 161 | ||
144 | 162 | ||
145 | bool ZCameraIO::write( char* buf, int len ) | 163 | bool ZCameraIO::write( char* buf, int len ) |
146 | { | 164 | { |
147 | if ( !len ) | 165 | if ( !len ) |
148 | len = strlen( buf ); | 166 | len = strlen( buf ); |
149 | 167 | ||
150 | odebug << "writing '" << buf << "' to driver." << oendl; | 168 | odebug << "writing '" << buf << "' to driver." << oendl; |
151 | 169 | ||
152 | return ::write( _driver, buf, len ) == len; | 170 | return ::write( _driver, buf, len ) == len; |
153 | } | 171 | } |
154 | 172 | ||
155 | 173 | ||
156 | bool ZCameraIO::snapshot( QImage* image ) | 174 | bool ZCameraIO::snapshot( QImage* image ) |
157 | { | 175 | { |
158 | /* | 176 | setReadMode( IMAGE | XFLIP | YFLIP ); |
159 | |||
160 | char buf[76800]; | 177 | char buf[76800]; |
161 | 178 | char* bp = buf; | |
162 | write( _driver, "M=13", 4 ); | ||
163 | write( _driver, "R=240,160,256,480", 17 ); | ||
164 | write( _driver, "M=12", 4 ); | ||
165 | |||
166 | int result = read( _driver, &buf, sizeof buf ); | ||
167 | |||
168 | return result == sizeof buf; | ||
169 | |||
170 | */ | ||
171 | |||
172 | unsigned char buf[76800]; | ||
173 | unsigned char* bp = buf; | ||
174 | unsigned char* p; | 179 | unsigned char* p; |
175 | 180 | ||
176 | int fd = open( "/tmp/cam", O_RDONLY ); | 181 | read( bp, _readlen ); |
177 | if ( ::read( fd, buf, sizeof buf ) != sizeof buf ) | ||
178 | owarn << "Couldn't read image from /dev/sharp_zdc" << oendl; | ||
179 | 182 | ||
180 | image->create( 240, 160, 16 ); | 183 | image->create( 240, 160, 16 ); |
181 | for ( int i = 0; i < 160; ++i ) | 184 | for ( int i = 0; i < 160; ++i ) |
182 | { | 185 | { |
183 | p = image->scanLine( i ); | 186 | p = image->scanLine( i ); |
184 | for ( int j = 0; j < 240; j++ ) | 187 | for ( int j = 0; j < 240; j++ ) |
185 | { | 188 | { |
186 | *p = *bp; | 189 | *p = *bp; |
187 | p++; | 190 | p++; |
188 | bp++; | 191 | bp++; |
189 | *p = *bp; | 192 | *p = *bp; |
190 | p++; | 193 | p++; |
191 | bp++; | 194 | bp++; |
192 | } | 195 | } |
193 | } | 196 | } |
194 | 197 | ||
195 | return true; | 198 | return true; |
196 | } | 199 | } |
197 | 200 | ||
198 | bool ZCameraIO::snapshot( uchar* buf ) | 201 | bool ZCameraIO::snapshot( unsigned char* buf ) |
199 | { | 202 | { |
203 | setReadMode( IMAGE | XFLIP | YFLIP ); | ||
204 | |||
205 | read( (char*) buf, _readlen ); | ||
206 | |||
207 | /* //TESTCODE | ||
200 | int fd = open( "/tmp/cam", O_RDONLY ); | 208 | int fd = open( "/tmp/cam", O_RDONLY ); |
201 | if ( ::read( fd, buf, 76800 ) != 76800 ) | 209 | if ( ::read( fd, (char*) buf, 76800 ) != 76800 ) |
202 | owarn << "Couldn't read image from /dev/sharp_zdc" << oendl; | 210 | owarn << "Couldn't read image from /dev/sharp_zdc" << oendl; |
211 | // TESTCODE */ | ||
212 | |||
203 | 213 | ||
204 | return true; | 214 | return true; |
205 | } | 215 | } |
206 | 216 | ||