blob: cf4cbfac44202c89114263297ca1fb9f0bec22fb [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2**
3** Copyright 2008, The Android Open Source Project
4**
Wu-cheng Li8de57d82009-09-23 14:37:52 -07005** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008**
Wu-cheng Li8de57d82009-09-23 14:37:52 -07009** http://www.apache.org/licenses/LICENSE-2.0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010**
Wu-cheng Li8de57d82009-09-23 14:37:52 -070011** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015** limitations under the License.
16*/
17
18#define LOG_TAG "CameraParams"
19#include <utils/Log.h>
20
21#include <string.h>
22#include <stdlib.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080023#include <camera/CameraParameters.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024
25namespace android {
Wu-cheng Li8de57d82009-09-23 14:37:52 -070026// Parameter keys to communicate between camera application and driver.
27const char CameraParameters::KEY_PREVIEW_SIZE[] = "preview-size";
28const char CameraParameters::KEY_SUPPORTED_PREVIEW_SIZES[] = "preview-size-values";
29const char CameraParameters::KEY_PREVIEW_FORMAT[] = "preview-format";
30const char CameraParameters::KEY_SUPPORTED_PREVIEW_FORMATS[] = "preview-format-values";
31const char CameraParameters::KEY_PREVIEW_FRAME_RATE[] = "preview-frame-rate";
32const char CameraParameters::KEY_SUPPORTED_PREVIEW_FRAME_RATES[] = "preview-frame-rate-values";
33const char CameraParameters::KEY_PICTURE_SIZE[] = "picture-size";
34const char CameraParameters::KEY_SUPPORTED_PICTURE_SIZES[] = "picture-size-values";
35const char CameraParameters::KEY_PICTURE_FORMAT[] = "picture-format";
36const char CameraParameters::KEY_SUPPORTED_PICTURE_FORMATS[] = "picture-format-values";
37const char CameraParameters::KEY_JPEG_THUMBNAIL_WIDTH[] = "jpeg-thumbnail-width";
38const char CameraParameters::KEY_JPEG_THUMBNAIL_HEIGHT[] = "jpeg-thumbnail-height";
Wu-cheng Liacf77032010-01-25 15:18:18 +080039const char CameraParameters::KEY_SUPPORTED_JPEG_THUMBNAIL_SIZES[] = "jpeg-thumbnail-size-values";
Wu-cheng Li8de57d82009-09-23 14:37:52 -070040const char CameraParameters::KEY_JPEG_THUMBNAIL_QUALITY[] = "jpeg-thumbnail-quality";
41const char CameraParameters::KEY_JPEG_QUALITY[] = "jpeg-quality";
42const char CameraParameters::KEY_ROTATION[] = "rotation";
43const char CameraParameters::KEY_GPS_LATITUDE[] = "gps-latitude";
44const char CameraParameters::KEY_GPS_LONGITUDE[] = "gps-longitude";
45const char CameraParameters::KEY_GPS_ALTITUDE[] = "gps-altitude";
46const char CameraParameters::KEY_GPS_TIMESTAMP[] = "gps-timestamp";
47const char CameraParameters::KEY_WHITE_BALANCE[] = "whitebalance";
48const char CameraParameters::KEY_SUPPORTED_WHITE_BALANCE[] = "whitebalance-values";
49const char CameraParameters::KEY_EFFECT[] = "effect";
50const char CameraParameters::KEY_SUPPORTED_EFFECTS[] = "effect-values";
51const char CameraParameters::KEY_ANTIBANDING[] = "antibanding";
52const char CameraParameters::KEY_SUPPORTED_ANTIBANDING[] = "antibanding-values";
53const char CameraParameters::KEY_SCENE_MODE[] = "scene-mode";
54const char CameraParameters::KEY_SUPPORTED_SCENE_MODES[] = "scene-mode-values";
55const char CameraParameters::KEY_FLASH_MODE[] = "flash-mode";
56const char CameraParameters::KEY_SUPPORTED_FLASH_MODES[] = "flash-mode-values";
57const char CameraParameters::KEY_FOCUS_MODE[] = "focus-mode";
58const char CameraParameters::KEY_SUPPORTED_FOCUS_MODES[] = "focus-mode-values";
Wu-cheng Li6c8d2762010-01-27 22:55:14 +080059const char CameraParameters::KEY_FOCAL_LENGTH[] = "focal-length";
60const char CameraParameters::KEY_HORIZONTAL_VIEW_ANGLE[] = "horizontal-view-angle";
61const char CameraParameters::KEY_VERTICAL_VIEW_ANGLE[] = "vertical-view-angle";
Wu-cheng Liff723b62010-02-09 13:38:19 +080062const char CameraParameters::KEY_EXPOSURE_COMPENSATION[] = "exposure-compensation";
Wu-cheng Li24b326a2010-02-20 17:47:04 +080063const char CameraParameters::KEY_MAX_EXPOSURE_COMPENSATION[] = "max-exposure-compensation";
64const char CameraParameters::KEY_MIN_EXPOSURE_COMPENSATION[] = "min-exposure-compensation";
65const char CameraParameters::KEY_EXPOSURE_COMPENSATION_STEP[] = "exposure-compensation-step";
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -080066const char CameraParameters::KEY_ZOOM[] = "zoom";
67const char CameraParameters::KEY_MAX_ZOOM[] = "max-zoom";
68const char CameraParameters::KEY_ZOOM_RATIOS[] = "zoom-ratios";
69const char CameraParameters::KEY_ZOOM_SUPPORTED[] = "zoom-supported";
70const char CameraParameters::KEY_SMOOTH_ZOOM_SUPPORTED[] = "smooth-zoom-supported";
Wu-cheng Li8de57d82009-09-23 14:37:52 -070071
72// Values for white balance settings.
73const char CameraParameters::WHITE_BALANCE_AUTO[] = "auto";
74const char CameraParameters::WHITE_BALANCE_INCANDESCENT[] = "incandescent";
75const char CameraParameters::WHITE_BALANCE_FLUORESCENT[] = "fluorescent";
76const char CameraParameters::WHITE_BALANCE_WARM_FLUORESCENT[] = "warm-fluorescent";
77const char CameraParameters::WHITE_BALANCE_DAYLIGHT[] = "daylight";
78const char CameraParameters::WHITE_BALANCE_CLOUDY_DAYLIGHT[] = "cloudy-daylight";
79const char CameraParameters::WHITE_BALANCE_TWILIGHT[] = "twilight";
80const char CameraParameters::WHITE_BALANCE_SHADE[] = "shade";
81
82// Values for effect settings.
83const char CameraParameters::EFFECT_NONE[] = "none";
84const char CameraParameters::EFFECT_MONO[] = "mono";
85const char CameraParameters::EFFECT_NEGATIVE[] = "negative";
86const char CameraParameters::EFFECT_SOLARIZE[] = "solarize";
87const char CameraParameters::EFFECT_SEPIA[] = "sepia";
88const char CameraParameters::EFFECT_POSTERIZE[] = "posterize";
89const char CameraParameters::EFFECT_WHITEBOARD[] = "whiteboard";
90const char CameraParameters::EFFECT_BLACKBOARD[] = "blackboard";
91const char CameraParameters::EFFECT_AQUA[] = "aqua";
92
93// Values for antibanding settings.
94const char CameraParameters::ANTIBANDING_AUTO[] = "auto";
95const char CameraParameters::ANTIBANDING_50HZ[] = "50hz";
96const char CameraParameters::ANTIBANDING_60HZ[] = "60hz";
97const char CameraParameters::ANTIBANDING_OFF[] = "off";
98
99// Values for flash mode settings.
100const char CameraParameters::FLASH_MODE_OFF[] = "off";
101const char CameraParameters::FLASH_MODE_AUTO[] = "auto";
102const char CameraParameters::FLASH_MODE_ON[] = "on";
103const char CameraParameters::FLASH_MODE_RED_EYE[] = "red-eye";
Wu-cheng Life1a86d2009-09-28 13:51:12 -0700104const char CameraParameters::FLASH_MODE_TORCH[] = "torch";
Wu-cheng Li8de57d82009-09-23 14:37:52 -0700105
106// Values for scene mode settings.
107const char CameraParameters::SCENE_MODE_AUTO[] = "auto";
108const char CameraParameters::SCENE_MODE_ACTION[] = "action";
109const char CameraParameters::SCENE_MODE_PORTRAIT[] = "portrait";
110const char CameraParameters::SCENE_MODE_LANDSCAPE[] = "landscape";
111const char CameraParameters::SCENE_MODE_NIGHT[] = "night";
112const char CameraParameters::SCENE_MODE_NIGHT_PORTRAIT[] = "night-portrait";
113const char CameraParameters::SCENE_MODE_THEATRE[] = "theatre";
114const char CameraParameters::SCENE_MODE_BEACH[] = "beach";
115const char CameraParameters::SCENE_MODE_SNOW[] = "snow";
116const char CameraParameters::SCENE_MODE_SUNSET[] = "sunset";
117const char CameraParameters::SCENE_MODE_STEADYPHOTO[] = "steadyphoto";
118const char CameraParameters::SCENE_MODE_FIREWORKS[] = "fireworks";
119const char CameraParameters::SCENE_MODE_SPORTS[] = "sports";
120const char CameraParameters::SCENE_MODE_PARTY[] = "party";
121const char CameraParameters::SCENE_MODE_CANDLELIGHT[] = "candlelight";
122
123// Formats for setPreviewFormat and setPictureFormat.
124const char CameraParameters::PIXEL_FORMAT_YUV422SP[] = "yuv422sp";
125const char CameraParameters::PIXEL_FORMAT_YUV420SP[] = "yuv420sp";
126const char CameraParameters::PIXEL_FORMAT_YUV422I[] = "yuv422i-yuyv";
127const char CameraParameters::PIXEL_FORMAT_RGB565[] = "rgb565";
128const char CameraParameters::PIXEL_FORMAT_JPEG[] = "jpeg";
129
130// Values for focus mode settings.
131const char CameraParameters::FOCUS_MODE_AUTO[] = "auto";
132const char CameraParameters::FOCUS_MODE_INFINITY[] = "infinity";
133const char CameraParameters::FOCUS_MODE_MACRO[] = "macro";
134const char CameraParameters::FOCUS_MODE_FIXED[] = "fixed";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136CameraParameters::CameraParameters()
137 : mMap()
138{
139}
140
141CameraParameters::~CameraParameters()
142{
143}
144
145String8 CameraParameters::flatten() const
146{
147 String8 flattened("");
148 size_t size = mMap.size();
149
150 for (size_t i = 0; i < size; i++) {
151 String8 k, v;
152 k = mMap.keyAt(i);
153 v = mMap.valueAt(i);
154
155 flattened += k;
156 flattened += "=";
157 flattened += v;
158 if (i != size-1)
159 flattened += ";";
160 }
161
162 return flattened;
163}
164
165void CameraParameters::unflatten(const String8 &params)
166{
167 const char *a = params.string();
168 const char *b;
169
170 mMap.clear();
171
172 for (;;) {
173 // Find the bounds of the key name.
174 b = strchr(a, '=');
175 if (b == 0)
176 break;
177
178 // Create the key string.
179 String8 k(a, (size_t)(b-a));
180
181 // Find the value.
182 a = b+1;
183 b = strchr(a, ';');
184 if (b == 0) {
185 // If there's no semicolon, this is the last item.
186 String8 v(a);
187 mMap.add(k, v);
188 break;
189 }
190
191 String8 v(a, (size_t)(b-a));
192 mMap.add(k, v);
193 a = b+1;
194 }
195}
196
197
198void CameraParameters::set(const char *key, const char *value)
199{
Wu-cheng Li8de57d82009-09-23 14:37:52 -0700200 // XXX i think i can do this with strspn()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 if (strchr(key, '=') || strchr(key, ';')) {
202 //XXX LOGE("Key \"%s\"contains invalid character (= or ;)", key);
203 return;
204 }
205
206 if (strchr(value, '=') || strchr(key, ';')) {
207 //XXX LOGE("Value \"%s\"contains invalid character (= or ;)", value);
208 return;
209 }
210
211 mMap.replaceValueFor(String8(key), String8(value));
212}
213
214void CameraParameters::set(const char *key, int value)
215{
216 char str[16];
217 sprintf(str, "%d", value);
218 set(key, str);
219}
220
Wu-cheng Li7eae31c2010-01-31 14:19:38 +0800221void CameraParameters::setFloat(const char *key, float value)
222{
223 char str[16]; // 14 should be enough. We overestimate to be safe.
224 snprintf(str, sizeof(str), "%g", value);
225 set(key, str);
226}
227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228const char *CameraParameters::get(const char *key) const
229{
230 String8 v = mMap.valueFor(String8(key));
231 if (v.length() == 0)
232 return 0;
233 return v.string();
234}
235
236int CameraParameters::getInt(const char *key) const
237{
238 const char *v = get(key);
239 if (v == 0)
240 return -1;
241 return strtol(v, 0, 0);
242}
243
Wu-cheng Li7eae31c2010-01-31 14:19:38 +0800244float CameraParameters::getFloat(const char *key) const
245{
246 const char *v = get(key);
247 if (v == 0) return -1;
248 return strtof(v, 0);
249}
250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251static int parse_size(const char *str, int &width, int &height)
252{
253 // Find the width.
254 char *end;
255 int w = (int)strtol(str, &end, 10);
256 // If an 'x' does not immediately follow, give up.
257 if (*end != 'x')
258 return -1;
259
260 // Find the height, immediately after the 'x'.
261 int h = (int)strtol(end+1, 0, 10);
262
263 width = w;
264 height = h;
265
266 return 0;
267}
268
269void CameraParameters::setPreviewSize(int width, int height)
270{
271 char str[32];
272 sprintf(str, "%dx%d", width, height);
Wu-cheng Li8de57d82009-09-23 14:37:52 -0700273 set(KEY_PREVIEW_SIZE, str);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274}
275
276void CameraParameters::getPreviewSize(int *width, int *height) const
277{
278 *width = -1;
279 *height = -1;
280
281 // Get the current string, if it doesn't exist, leave the -1x-1
Wu-cheng Li8de57d82009-09-23 14:37:52 -0700282 const char *p = get(KEY_PREVIEW_SIZE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 if (p == 0)
284 return;
285
286 int w, h;
287 if (parse_size(p, w, h) == 0) {
288 *width = w;
289 *height = h;
290 }
291}
292
293void CameraParameters::setPreviewFrameRate(int fps)
294{
Wu-cheng Li8de57d82009-09-23 14:37:52 -0700295 set(KEY_PREVIEW_FRAME_RATE, fps);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296}
297
298int CameraParameters::getPreviewFrameRate() const
299{
Wu-cheng Li8de57d82009-09-23 14:37:52 -0700300 return getInt(KEY_PREVIEW_FRAME_RATE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301}
302
303void CameraParameters::setPreviewFormat(const char *format)
304{
Wu-cheng Li8de57d82009-09-23 14:37:52 -0700305 set(KEY_PREVIEW_FORMAT, format);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306}
307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308const char *CameraParameters::getPreviewFormat() const
309{
Wu-cheng Li8de57d82009-09-23 14:37:52 -0700310 return get(KEY_PREVIEW_FORMAT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311}
312
313void CameraParameters::setPictureSize(int width, int height)
314{
315 char str[32];
316 sprintf(str, "%dx%d", width, height);
Wu-cheng Li8de57d82009-09-23 14:37:52 -0700317 set(KEY_PICTURE_SIZE, str);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318}
319
320void CameraParameters::getPictureSize(int *width, int *height) const
321{
322 *width = -1;
323 *height = -1;
324
325 // Get the current string, if it doesn't exist, leave the -1x-1
Wu-cheng Li8de57d82009-09-23 14:37:52 -0700326 const char *p = get(KEY_PICTURE_SIZE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 if (p == 0)
328 return;
329
330 int w, h;
331 if (parse_size(p, w, h) == 0) {
332 *width = w;
333 *height = h;
334 }
335}
336
337void CameraParameters::setPictureFormat(const char *format)
338{
Wu-cheng Li8de57d82009-09-23 14:37:52 -0700339 set(KEY_PICTURE_FORMAT, format);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340}
341
342const char *CameraParameters::getPictureFormat() const
343{
Wu-cheng Li8de57d82009-09-23 14:37:52 -0700344 return get(KEY_PICTURE_FORMAT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345}
346
347void CameraParameters::dump() const
348{
349 LOGD("dump: mMap.size = %d", mMap.size());
350 for (size_t i = 0; i < mMap.size(); i++) {
351 String8 k, v;
352 k = mMap.keyAt(i);
353 v = mMap.valueAt(i);
354 LOGD("%s: %s\n", k.string(), v.string());
355 }
356}
357
358status_t CameraParameters::dump(int fd, const Vector<String16>& args) const
359{
360 const size_t SIZE = 256;
361 char buffer[SIZE];
362 String8 result;
363 snprintf(buffer, 255, "CameraParameters::dump: mMap.size = %d\n", mMap.size());
364 result.append(buffer);
365 for (size_t i = 0; i < mMap.size(); i++) {
366 String8 k, v;
367 k = mMap.keyAt(i);
368 v = mMap.valueAt(i);
369 snprintf(buffer, 255, "\t%s: %s\n", k.string(), v.string());
370 result.append(buffer);
371 }
372 write(fd, result.string(), result.size());
373 return NO_ERROR;
374}
375
376}; // namespace android