blob: 035fa9fa67b583704c1dc88133def4df84244a8c [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";
Ray Chen055c9862010-02-23 10:45:42 +080047const char CameraParameters::KEY_GPS_PROCESSING_METHOD[] = "gps-processing-method";
Wu-cheng Li8de57d82009-09-23 14:37:52 -070048const char CameraParameters::KEY_WHITE_BALANCE[] = "whitebalance";
49const char CameraParameters::KEY_SUPPORTED_WHITE_BALANCE[] = "whitebalance-values";
50const char CameraParameters::KEY_EFFECT[] = "effect";
51const char CameraParameters::KEY_SUPPORTED_EFFECTS[] = "effect-values";
52const char CameraParameters::KEY_ANTIBANDING[] = "antibanding";
53const char CameraParameters::KEY_SUPPORTED_ANTIBANDING[] = "antibanding-values";
54const char CameraParameters::KEY_SCENE_MODE[] = "scene-mode";
55const char CameraParameters::KEY_SUPPORTED_SCENE_MODES[] = "scene-mode-values";
56const char CameraParameters::KEY_FLASH_MODE[] = "flash-mode";
57const char CameraParameters::KEY_SUPPORTED_FLASH_MODES[] = "flash-mode-values";
58const char CameraParameters::KEY_FOCUS_MODE[] = "focus-mode";
59const char CameraParameters::KEY_SUPPORTED_FOCUS_MODES[] = "focus-mode-values";
Wu-cheng Li6c8d2762010-01-27 22:55:14 +080060const char CameraParameters::KEY_FOCAL_LENGTH[] = "focal-length";
61const char CameraParameters::KEY_HORIZONTAL_VIEW_ANGLE[] = "horizontal-view-angle";
62const char CameraParameters::KEY_VERTICAL_VIEW_ANGLE[] = "vertical-view-angle";
Wu-cheng Liff723b62010-02-09 13:38:19 +080063const char CameraParameters::KEY_EXPOSURE_COMPENSATION[] = "exposure-compensation";
Wu-cheng Li24b326a2010-02-20 17:47:04 +080064const char CameraParameters::KEY_MAX_EXPOSURE_COMPENSATION[] = "max-exposure-compensation";
65const char CameraParameters::KEY_MIN_EXPOSURE_COMPENSATION[] = "min-exposure-compensation";
66const char CameraParameters::KEY_EXPOSURE_COMPENSATION_STEP[] = "exposure-compensation-step";
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -080067const char CameraParameters::KEY_ZOOM[] = "zoom";
68const char CameraParameters::KEY_MAX_ZOOM[] = "max-zoom";
69const char CameraParameters::KEY_ZOOM_RATIOS[] = "zoom-ratios";
70const char CameraParameters::KEY_ZOOM_SUPPORTED[] = "zoom-supported";
71const char CameraParameters::KEY_SMOOTH_ZOOM_SUPPORTED[] = "smooth-zoom-supported";
Wu-cheng Lie339c5e2010-05-13 19:31:02 +080072const char CameraParameters::KEY_FOCUS_DISTANCES[] = "focus-distances";
Wu-cheng Li8de57d82009-09-23 14:37:52 -070073
Wu-cheng Lid9ef5d72010-03-08 15:28:48 -080074const char CameraParameters::TRUE[] = "true";
Wu-cheng Lie339c5e2010-05-13 19:31:02 +080075const char CameraParameters::INFINITY[] = "Infinity";
Wu-cheng Lid9ef5d72010-03-08 15:28:48 -080076
Wu-cheng Li8de57d82009-09-23 14:37:52 -070077// Values for white balance settings.
78const char CameraParameters::WHITE_BALANCE_AUTO[] = "auto";
79const char CameraParameters::WHITE_BALANCE_INCANDESCENT[] = "incandescent";
80const char CameraParameters::WHITE_BALANCE_FLUORESCENT[] = "fluorescent";
81const char CameraParameters::WHITE_BALANCE_WARM_FLUORESCENT[] = "warm-fluorescent";
82const char CameraParameters::WHITE_BALANCE_DAYLIGHT[] = "daylight";
83const char CameraParameters::WHITE_BALANCE_CLOUDY_DAYLIGHT[] = "cloudy-daylight";
84const char CameraParameters::WHITE_BALANCE_TWILIGHT[] = "twilight";
85const char CameraParameters::WHITE_BALANCE_SHADE[] = "shade";
86
87// Values for effect settings.
88const char CameraParameters::EFFECT_NONE[] = "none";
89const char CameraParameters::EFFECT_MONO[] = "mono";
90const char CameraParameters::EFFECT_NEGATIVE[] = "negative";
91const char CameraParameters::EFFECT_SOLARIZE[] = "solarize";
92const char CameraParameters::EFFECT_SEPIA[] = "sepia";
93const char CameraParameters::EFFECT_POSTERIZE[] = "posterize";
94const char CameraParameters::EFFECT_WHITEBOARD[] = "whiteboard";
95const char CameraParameters::EFFECT_BLACKBOARD[] = "blackboard";
96const char CameraParameters::EFFECT_AQUA[] = "aqua";
97
98// Values for antibanding settings.
99const char CameraParameters::ANTIBANDING_AUTO[] = "auto";
100const char CameraParameters::ANTIBANDING_50HZ[] = "50hz";
101const char CameraParameters::ANTIBANDING_60HZ[] = "60hz";
102const char CameraParameters::ANTIBANDING_OFF[] = "off";
103
104// Values for flash mode settings.
105const char CameraParameters::FLASH_MODE_OFF[] = "off";
106const char CameraParameters::FLASH_MODE_AUTO[] = "auto";
107const char CameraParameters::FLASH_MODE_ON[] = "on";
108const char CameraParameters::FLASH_MODE_RED_EYE[] = "red-eye";
Wu-cheng Life1a86d2009-09-28 13:51:12 -0700109const char CameraParameters::FLASH_MODE_TORCH[] = "torch";
Wu-cheng Li8de57d82009-09-23 14:37:52 -0700110
111// Values for scene mode settings.
112const char CameraParameters::SCENE_MODE_AUTO[] = "auto";
113const char CameraParameters::SCENE_MODE_ACTION[] = "action";
114const char CameraParameters::SCENE_MODE_PORTRAIT[] = "portrait";
115const char CameraParameters::SCENE_MODE_LANDSCAPE[] = "landscape";
116const char CameraParameters::SCENE_MODE_NIGHT[] = "night";
117const char CameraParameters::SCENE_MODE_NIGHT_PORTRAIT[] = "night-portrait";
118const char CameraParameters::SCENE_MODE_THEATRE[] = "theatre";
119const char CameraParameters::SCENE_MODE_BEACH[] = "beach";
120const char CameraParameters::SCENE_MODE_SNOW[] = "snow";
121const char CameraParameters::SCENE_MODE_SUNSET[] = "sunset";
122const char CameraParameters::SCENE_MODE_STEADYPHOTO[] = "steadyphoto";
123const char CameraParameters::SCENE_MODE_FIREWORKS[] = "fireworks";
124const char CameraParameters::SCENE_MODE_SPORTS[] = "sports";
125const char CameraParameters::SCENE_MODE_PARTY[] = "party";
126const char CameraParameters::SCENE_MODE_CANDLELIGHT[] = "candlelight";
Wu-cheng Lic58b4232010-03-29 17:21:28 +0800127const char CameraParameters::SCENE_MODE_BARCODE[] = "barcode";
Wu-cheng Li8de57d82009-09-23 14:37:52 -0700128
129// Formats for setPreviewFormat and setPictureFormat.
130const char CameraParameters::PIXEL_FORMAT_YUV422SP[] = "yuv422sp";
131const char CameraParameters::PIXEL_FORMAT_YUV420SP[] = "yuv420sp";
132const char CameraParameters::PIXEL_FORMAT_YUV422I[] = "yuv422i-yuyv";
133const char CameraParameters::PIXEL_FORMAT_RGB565[] = "rgb565";
134const char CameraParameters::PIXEL_FORMAT_JPEG[] = "jpeg";
135
136// Values for focus mode settings.
137const char CameraParameters::FOCUS_MODE_AUTO[] = "auto";
138const char CameraParameters::FOCUS_MODE_INFINITY[] = "infinity";
139const char CameraParameters::FOCUS_MODE_MACRO[] = "macro";
140const char CameraParameters::FOCUS_MODE_FIXED[] = "fixed";
Wu-cheng Lic58b4232010-03-29 17:21:28 +0800141const char CameraParameters::FOCUS_MODE_EDOF[] = "edof";
Wu-cheng Lica099612010-05-06 16:47:30 +0800142const char CameraParameters::FOCUS_MODE_CONTINUOUS[] = "continuous";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144CameraParameters::CameraParameters()
145 : mMap()
146{
147}
148
149CameraParameters::~CameraParameters()
150{
151}
152
153String8 CameraParameters::flatten() const
154{
155 String8 flattened("");
156 size_t size = mMap.size();
157
158 for (size_t i = 0; i < size; i++) {
159 String8 k, v;
160 k = mMap.keyAt(i);
161 v = mMap.valueAt(i);
162
163 flattened += k;
164 flattened += "=";
165 flattened += v;
166 if (i != size-1)
167 flattened += ";";
168 }
169
170 return flattened;
171}
172
173void CameraParameters::unflatten(const String8 &params)
174{
175 const char *a = params.string();
176 const char *b;
177
178 mMap.clear();
179
180 for (;;) {
181 // Find the bounds of the key name.
182 b = strchr(a, '=');
183 if (b == 0)
184 break;
185
186 // Create the key string.
187 String8 k(a, (size_t)(b-a));
188
189 // Find the value.
190 a = b+1;
191 b = strchr(a, ';');
192 if (b == 0) {
193 // If there's no semicolon, this is the last item.
194 String8 v(a);
195 mMap.add(k, v);
196 break;
197 }
198
199 String8 v(a, (size_t)(b-a));
200 mMap.add(k, v);
201 a = b+1;
202 }
203}
204
205
206void CameraParameters::set(const char *key, const char *value)
207{
Wu-cheng Li8de57d82009-09-23 14:37:52 -0700208 // XXX i think i can do this with strspn()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 if (strchr(key, '=') || strchr(key, ';')) {
210 //XXX LOGE("Key \"%s\"contains invalid character (= or ;)", key);
211 return;
212 }
213
214 if (strchr(value, '=') || strchr(key, ';')) {
215 //XXX LOGE("Value \"%s\"contains invalid character (= or ;)", value);
216 return;
217 }
218
219 mMap.replaceValueFor(String8(key), String8(value));
220}
221
222void CameraParameters::set(const char *key, int value)
223{
224 char str[16];
225 sprintf(str, "%d", value);
226 set(key, str);
227}
228
Wu-cheng Li7eae31c2010-01-31 14:19:38 +0800229void CameraParameters::setFloat(const char *key, float value)
230{
231 char str[16]; // 14 should be enough. We overestimate to be safe.
232 snprintf(str, sizeof(str), "%g", value);
233 set(key, str);
234}
235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236const char *CameraParameters::get(const char *key) const
237{
238 String8 v = mMap.valueFor(String8(key));
239 if (v.length() == 0)
240 return 0;
241 return v.string();
242}
243
244int CameraParameters::getInt(const char *key) const
245{
246 const char *v = get(key);
247 if (v == 0)
248 return -1;
249 return strtol(v, 0, 0);
250}
251
Wu-cheng Li7eae31c2010-01-31 14:19:38 +0800252float CameraParameters::getFloat(const char *key) const
253{
254 const char *v = get(key);
255 if (v == 0) return -1;
256 return strtof(v, 0);
257}
258
Wu-cheng Li78624e42010-05-11 12:11:56 +0800259void CameraParameters::remove(const char *key)
260{
261 mMap.removeItem(String8(key));
262}
263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264static int parse_size(const char *str, int &width, int &height)
265{
266 // Find the width.
267 char *end;
268 int w = (int)strtol(str, &end, 10);
269 // If an 'x' does not immediately follow, give up.
270 if (*end != 'x')
271 return -1;
272
273 // Find the height, immediately after the 'x'.
274 int h = (int)strtol(end+1, 0, 10);
275
276 width = w;
277 height = h;
278
279 return 0;
280}
281
282void CameraParameters::setPreviewSize(int width, int height)
283{
284 char str[32];
285 sprintf(str, "%dx%d", width, height);
Wu-cheng Li8de57d82009-09-23 14:37:52 -0700286 set(KEY_PREVIEW_SIZE, str);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287}
288
289void CameraParameters::getPreviewSize(int *width, int *height) const
290{
291 *width = -1;
292 *height = -1;
293
294 // Get the current string, if it doesn't exist, leave the -1x-1
Wu-cheng Li8de57d82009-09-23 14:37:52 -0700295 const char *p = get(KEY_PREVIEW_SIZE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 if (p == 0)
297 return;
298
299 int w, h;
300 if (parse_size(p, w, h) == 0) {
301 *width = w;
302 *height = h;
303 }
304}
305
306void CameraParameters::setPreviewFrameRate(int fps)
307{
Wu-cheng Li8de57d82009-09-23 14:37:52 -0700308 set(KEY_PREVIEW_FRAME_RATE, fps);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309}
310
311int CameraParameters::getPreviewFrameRate() const
312{
Wu-cheng Li8de57d82009-09-23 14:37:52 -0700313 return getInt(KEY_PREVIEW_FRAME_RATE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314}
315
316void CameraParameters::setPreviewFormat(const char *format)
317{
Wu-cheng Li8de57d82009-09-23 14:37:52 -0700318 set(KEY_PREVIEW_FORMAT, format);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319}
320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321const char *CameraParameters::getPreviewFormat() const
322{
Wu-cheng Li8de57d82009-09-23 14:37:52 -0700323 return get(KEY_PREVIEW_FORMAT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324}
325
326void CameraParameters::setPictureSize(int width, int height)
327{
328 char str[32];
329 sprintf(str, "%dx%d", width, height);
Wu-cheng Li8de57d82009-09-23 14:37:52 -0700330 set(KEY_PICTURE_SIZE, str);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331}
332
333void CameraParameters::getPictureSize(int *width, int *height) const
334{
335 *width = -1;
336 *height = -1;
337
338 // Get the current string, if it doesn't exist, leave the -1x-1
Wu-cheng Li8de57d82009-09-23 14:37:52 -0700339 const char *p = get(KEY_PICTURE_SIZE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 if (p == 0)
341 return;
342
343 int w, h;
344 if (parse_size(p, w, h) == 0) {
345 *width = w;
346 *height = h;
347 }
348}
349
350void CameraParameters::setPictureFormat(const char *format)
351{
Wu-cheng Li8de57d82009-09-23 14:37:52 -0700352 set(KEY_PICTURE_FORMAT, format);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353}
354
355const char *CameraParameters::getPictureFormat() const
356{
Wu-cheng Li8de57d82009-09-23 14:37:52 -0700357 return get(KEY_PICTURE_FORMAT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358}
359
360void CameraParameters::dump() const
361{
362 LOGD("dump: mMap.size = %d", mMap.size());
363 for (size_t i = 0; i < mMap.size(); i++) {
364 String8 k, v;
365 k = mMap.keyAt(i);
366 v = mMap.valueAt(i);
367 LOGD("%s: %s\n", k.string(), v.string());
368 }
369}
370
371status_t CameraParameters::dump(int fd, const Vector<String16>& args) const
372{
373 const size_t SIZE = 256;
374 char buffer[SIZE];
375 String8 result;
376 snprintf(buffer, 255, "CameraParameters::dump: mMap.size = %d\n", mMap.size());
377 result.append(buffer);
378 for (size_t i = 0; i < mMap.size(); i++) {
379 String8 k, v;
380 k = mMap.keyAt(i);
381 v = mMap.valueAt(i);
382 snprintf(buffer, 255, "\t%s: %s\n", k.string(), v.string());
383 result.append(buffer);
384 }
385 write(fd, result.string(), result.size());
386 return NO_ERROR;
387}
388
389}; // namespace android