1 /* 2 ** 3 ** Copyright 2008, The Android Open Source Project 4 ** 5 ** 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 8 ** 9 ** http://www.apache.org/licenses/LICENSE-2.0 10 ** 11 ** 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 15 ** 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> 23 #include <camera/CameraParameters.h> 24 25 namespace android { 26 // Parameter keys to communicate between camera application and driver. 27 const char CameraParameters::KEY_PREVIEW_SIZE[] = "preview-size"; 28 const char CameraParameters::KEY_SUPPORTED_PREVIEW_SIZES[] = "preview-size-values"; 29 const char CameraParameters::KEY_PREVIEW_FORMAT[] = "preview-format"; 30 const char CameraParameters::KEY_SUPPORTED_PREVIEW_FORMATS[] = "preview-format-values"; 31 const char CameraParameters::KEY_PREVIEW_FRAME_RATE[] = "preview-frame-rate"; 32 const char CameraParameters::KEY_SUPPORTED_PREVIEW_FRAME_RATES[] = "preview-frame-rate-values"; 33 const char CameraParameters::KEY_PREVIEW_FPS_RANGE[] = "preview-fps-range"; 34 const char CameraParameters::KEY_SUPPORTED_PREVIEW_FPS_RANGE[] = "preview-fps-range-values"; 35 const char CameraParameters::KEY_PICTURE_SIZE[] = "picture-size"; 36 const char CameraParameters::KEY_SUPPORTED_PICTURE_SIZES[] = "picture-size-values"; 37 const char CameraParameters::KEY_PICTURE_FORMAT[] = "picture-format"; 38 const char CameraParameters::KEY_SUPPORTED_PICTURE_FORMATS[] = "picture-format-values"; 39 const char CameraParameters::KEY_JPEG_THUMBNAIL_WIDTH[] = "jpeg-thumbnail-width"; 40 const char CameraParameters::KEY_JPEG_THUMBNAIL_HEIGHT[] = "jpeg-thumbnail-height"; 41 const char CameraParameters::KEY_SUPPORTED_JPEG_THUMBNAIL_SIZES[] = "jpeg-thumbnail-size-values"; 42 const char CameraParameters::KEY_JPEG_THUMBNAIL_QUALITY[] = "jpeg-thumbnail-quality"; 43 const char CameraParameters::KEY_JPEG_QUALITY[] = "jpeg-quality"; 44 const char CameraParameters::KEY_ROTATION[] = "rotation"; 45 const char CameraParameters::KEY_GPS_LATITUDE[] = "gps-latitude"; 46 const char CameraParameters::KEY_GPS_LONGITUDE[] = "gps-longitude"; 47 const char CameraParameters::KEY_GPS_ALTITUDE[] = "gps-altitude"; 48 const char CameraParameters::KEY_GPS_TIMESTAMP[] = "gps-timestamp"; 49 const char CameraParameters::KEY_GPS_PROCESSING_METHOD[] = "gps-processing-method"; 50 const char CameraParameters::KEY_WHITE_BALANCE[] = "whitebalance"; 51 const char CameraParameters::KEY_SUPPORTED_WHITE_BALANCE[] = "whitebalance-values"; 52 const char CameraParameters::KEY_EFFECT[] = "effect"; 53 const char CameraParameters::KEY_SUPPORTED_EFFECTS[] = "effect-values"; 54 const char CameraParameters::KEY_ANTIBANDING[] = "antibanding"; 55 const char CameraParameters::KEY_SUPPORTED_ANTIBANDING[] = "antibanding-values"; 56 const char CameraParameters::KEY_SCENE_MODE[] = "scene-mode"; 57 const char CameraParameters::KEY_SUPPORTED_SCENE_MODES[] = "scene-mode-values"; 58 const char CameraParameters::KEY_FLASH_MODE[] = "flash-mode"; 59 const char CameraParameters::KEY_SUPPORTED_FLASH_MODES[] = "flash-mode-values"; 60 const char CameraParameters::KEY_FOCUS_MODE[] = "focus-mode"; 61 const char CameraParameters::KEY_SUPPORTED_FOCUS_MODES[] = "focus-mode-values"; 62 const char CameraParameters::KEY_MAX_NUM_FOCUS_AREAS[] = "max-num-focus-areas"; 63 const char CameraParameters::KEY_FOCUS_AREAS[] = "focus-areas"; 64 const char CameraParameters::KEY_FOCAL_LENGTH[] = "focal-length"; 65 const char CameraParameters::KEY_HORIZONTAL_VIEW_ANGLE[] = "horizontal-view-angle"; 66 const char CameraParameters::KEY_VERTICAL_VIEW_ANGLE[] = "vertical-view-angle"; 67 const char CameraParameters::KEY_EXPOSURE_COMPENSATION[] = "exposure-compensation"; 68 const char CameraParameters::KEY_MAX_EXPOSURE_COMPENSATION[] = "max-exposure-compensation"; 69 const char CameraParameters::KEY_MIN_EXPOSURE_COMPENSATION[] = "min-exposure-compensation"; 70 const char CameraParameters::KEY_EXPOSURE_COMPENSATION_STEP[] = "exposure-compensation-step"; 71 const char CameraParameters::KEY_AUTO_EXPOSURE_LOCK[] = "auto-exposure-lock"; 72 const char CameraParameters::KEY_AUTO_EXPOSURE_LOCK_SUPPORTED[] = "auto-exposure-lock-supported"; 73 const char CameraParameters::KEY_AUTO_WHITEBALANCE_LOCK[] = "auto-whitebalance-lock"; 74 const char CameraParameters::KEY_AUTO_WHITEBALANCE_LOCK_SUPPORTED[] = "auto-whitebalance-lock-supported"; 75 const char CameraParameters::KEY_MAX_NUM_METERING_AREAS[] = "max-num-metering-areas"; 76 const char CameraParameters::KEY_METERING_AREAS[] = "metering-areas"; 77 const char CameraParameters::KEY_ZOOM[] = "zoom"; 78 const char CameraParameters::KEY_MAX_ZOOM[] = "max-zoom"; 79 const char CameraParameters::KEY_ZOOM_RATIOS[] = "zoom-ratios"; 80 const char CameraParameters::KEY_ZOOM_SUPPORTED[] = "zoom-supported"; 81 const char CameraParameters::KEY_SMOOTH_ZOOM_SUPPORTED[] = "smooth-zoom-supported"; 82 const char CameraParameters::KEY_FOCUS_DISTANCES[] = "focus-distances"; 83 const char CameraParameters::KEY_VIDEO_FRAME_FORMAT[] = "video-frame-format"; 84 const char CameraParameters::KEY_VIDEO_SIZE[] = "video-size"; 85 const char CameraParameters::KEY_SUPPORTED_VIDEO_SIZES[] = "video-size-values"; 86 const char CameraParameters::KEY_PREFERRED_PREVIEW_SIZE_FOR_VIDEO[] = "preferred-preview-size-for-video"; 87 const char CameraParameters::KEY_MAX_NUM_DETECTED_FACES_HW[] = "max-num-detected-faces-hw"; 88 const char CameraParameters::KEY_MAX_NUM_DETECTED_FACES_SW[] = "max-num-detected-faces-sw"; 89 const char CameraParameters::KEY_RECORDING_HINT[] = "recording-hint"; 90 const char CameraParameters::KEY_VIDEO_SNAPSHOT_SUPPORTED[] = "video-snapshot-supported"; 91 const char CameraParameters::KEY_VIDEO_STABILIZATION[] = "video-stabilization"; 92 const char CameraParameters::KEY_VIDEO_STABILIZATION_SUPPORTED[] = "video-stabilization-supported"; 93 94 const char CameraParameters::TRUE[] = "true"; 95 const char CameraParameters::FALSE[] = "false"; 96 const char CameraParameters::FOCUS_DISTANCE_INFINITY[] = "Infinity"; 97 98 // Values for white balance settings. 99 const char CameraParameters::WHITE_BALANCE_AUTO[] = "auto"; 100 const char CameraParameters::WHITE_BALANCE_INCANDESCENT[] = "incandescent"; 101 const char CameraParameters::WHITE_BALANCE_FLUORESCENT[] = "fluorescent"; 102 const char CameraParameters::WHITE_BALANCE_WARM_FLUORESCENT[] = "warm-fluorescent"; 103 const char CameraParameters::WHITE_BALANCE_DAYLIGHT[] = "daylight"; 104 const char CameraParameters::WHITE_BALANCE_CLOUDY_DAYLIGHT[] = "cloudy-daylight"; 105 const char CameraParameters::WHITE_BALANCE_TWILIGHT[] = "twilight"; 106 const char CameraParameters::WHITE_BALANCE_SHADE[] = "shade"; 107 108 // Values for effect settings. 109 const char CameraParameters::EFFECT_NONE[] = "none"; 110 const char CameraParameters::EFFECT_MONO[] = "mono"; 111 const char CameraParameters::EFFECT_NEGATIVE[] = "negative"; 112 const char CameraParameters::EFFECT_SOLARIZE[] = "solarize"; 113 const char CameraParameters::EFFECT_SEPIA[] = "sepia"; 114 const char CameraParameters::EFFECT_POSTERIZE[] = "posterize"; 115 const char CameraParameters::EFFECT_WHITEBOARD[] = "whiteboard"; 116 const char CameraParameters::EFFECT_BLACKBOARD[] = "blackboard"; 117 const char CameraParameters::EFFECT_AQUA[] = "aqua"; 118 119 // Values for antibanding settings. 120 const char CameraParameters::ANTIBANDING_AUTO[] = "auto"; 121 const char CameraParameters::ANTIBANDING_50HZ[] = "50hz"; 122 const char CameraParameters::ANTIBANDING_60HZ[] = "60hz"; 123 const char CameraParameters::ANTIBANDING_OFF[] = "off"; 124 125 // Values for flash mode settings. 126 const char CameraParameters::FLASH_MODE_OFF[] = "off"; 127 const char CameraParameters::FLASH_MODE_AUTO[] = "auto"; 128 const char CameraParameters::FLASH_MODE_ON[] = "on"; 129 const char CameraParameters::FLASH_MODE_RED_EYE[] = "red-eye"; 130 const char CameraParameters::FLASH_MODE_TORCH[] = "torch"; 131 132 // Values for scene mode settings. 133 const char CameraParameters::SCENE_MODE_AUTO[] = "auto"; 134 const char CameraParameters::SCENE_MODE_ACTION[] = "action"; 135 const char CameraParameters::SCENE_MODE_PORTRAIT[] = "portrait"; 136 const char CameraParameters::SCENE_MODE_LANDSCAPE[] = "landscape"; 137 const char CameraParameters::SCENE_MODE_NIGHT[] = "night"; 138 const char CameraParameters::SCENE_MODE_NIGHT_PORTRAIT[] = "night-portrait"; 139 const char CameraParameters::SCENE_MODE_THEATRE[] = "theatre"; 140 const char CameraParameters::SCENE_MODE_BEACH[] = "beach"; 141 const char CameraParameters::SCENE_MODE_SNOW[] = "snow"; 142 const char CameraParameters::SCENE_MODE_SUNSET[] = "sunset"; 143 const char CameraParameters::SCENE_MODE_STEADYPHOTO[] = "steadyphoto"; 144 const char CameraParameters::SCENE_MODE_FIREWORKS[] = "fireworks"; 145 const char CameraParameters::SCENE_MODE_SPORTS[] = "sports"; 146 const char CameraParameters::SCENE_MODE_PARTY[] = "party"; 147 const char CameraParameters::SCENE_MODE_CANDLELIGHT[] = "candlelight"; 148 const char CameraParameters::SCENE_MODE_BARCODE[] = "barcode"; 149 150 const char CameraParameters::PIXEL_FORMAT_YUV422SP[] = "yuv422sp"; 151 const char CameraParameters::PIXEL_FORMAT_YUV420SP[] = "yuv420sp"; 152 const char CameraParameters::PIXEL_FORMAT_YUV422I[] = "yuv422i-yuyv"; 153 const char CameraParameters::PIXEL_FORMAT_YUV420P[] = "yuv420p"; 154 const char CameraParameters::PIXEL_FORMAT_RGB565[] = "rgb565"; 155 const char CameraParameters::PIXEL_FORMAT_RGBA8888[] = "rgba8888"; 156 const char CameraParameters::PIXEL_FORMAT_JPEG[] = "jpeg"; 157 const char CameraParameters::PIXEL_FORMAT_BAYER_RGGB[] = "bayer-rggb"; 158 159 // Values for focus mode settings. 160 const char CameraParameters::FOCUS_MODE_AUTO[] = "auto"; 161 const char CameraParameters::FOCUS_MODE_INFINITY[] = "infinity"; 162 const char CameraParameters::FOCUS_MODE_MACRO[] = "macro"; 163 const char CameraParameters::FOCUS_MODE_FIXED[] = "fixed"; 164 const char CameraParameters::FOCUS_MODE_EDOF[] = "edof"; 165 const char CameraParameters::FOCUS_MODE_CONTINUOUS_VIDEO[] = "continuous-video"; 166 const char CameraParameters::FOCUS_MODE_CONTINUOUS_PICTURE[] = "continuous-picture"; 167 168 CameraParameters::CameraParameters() 169 : mMap() 170 { 171 } 172 173 CameraParameters::~CameraParameters() 174 { 175 } 176 177 String8 CameraParameters::flatten() const 178 { 179 String8 flattened(""); 180 size_t size = mMap.size(); 181 182 for (size_t i = 0; i < size; i++) { 183 String8 k, v; 184 k = mMap.keyAt(i); 185 v = mMap.valueAt(i); 186 187 flattened += k; 188 flattened += "="; 189 flattened += v; 190 if (i != size-1) 191 flattened += ";"; 192 } 193 194 return flattened; 195 } 196 197 void CameraParameters::unflatten(const String8 ¶ms) 198 { 199 const char *a = params.string(); 200 const char *b; 201 202 mMap.clear(); 203 204 for (;;) { 205 // Find the bounds of the key name. 206 b = strchr(a, '='); 207 if (b == 0) 208 break; 209 210 // Create the key string. 211 String8 k(a, (size_t)(b-a)); 212 213 // Find the value. 214 a = b+1; 215 b = strchr(a, ';'); 216 if (b == 0) { 217 // If there's no semicolon, this is the last item. 218 String8 v(a); 219 mMap.add(k, v); 220 break; 221 } 222 223 String8 v(a, (size_t)(b-a)); 224 mMap.add(k, v); 225 a = b+1; 226 } 227 } 228 229 230 void CameraParameters::set(const char *key, const char *value) 231 { 232 // XXX i think i can do this with strspn() 233 if (strchr(key, '=') || strchr(key, ';')) { 234 //XXX LOGE("Key \"%s\"contains invalid character (= or ;)", key); 235 return; 236 } 237 238 if (strchr(value, '=') || strchr(key, ';')) { 239 //XXX LOGE("Value \"%s\"contains invalid character (= or ;)", value); 240 return; 241 } 242 243 mMap.replaceValueFor(String8(key), String8(value)); 244 } 245 246 void CameraParameters::set(const char *key, int value) 247 { 248 char str[16]; 249 sprintf(str, "%d", value); 250 set(key, str); 251 } 252 253 void CameraParameters::setFloat(const char *key, float value) 254 { 255 char str[16]; // 14 should be enough. We overestimate to be safe. 256 snprintf(str, sizeof(str), "%g", value); 257 set(key, str); 258 } 259 260 const char *CameraParameters::get(const char *key) const 261 { 262 String8 v = mMap.valueFor(String8(key)); 263 if (v.length() == 0) 264 return 0; 265 return v.string(); 266 } 267 268 int CameraParameters::getInt(const char *key) const 269 { 270 const char *v = get(key); 271 if (v == 0) 272 return -1; 273 return strtol(v, 0, 0); 274 } 275 276 float CameraParameters::getFloat(const char *key) const 277 { 278 const char *v = get(key); 279 if (v == 0) return -1; 280 return strtof(v, 0); 281 } 282 283 void CameraParameters::remove(const char *key) 284 { 285 mMap.removeItem(String8(key)); 286 } 287 288 // Parse string like "640x480" or "10000,20000" 289 static int parse_pair(const char *str, int *first, int *second, char delim, 290 char **endptr = NULL) 291 { 292 // Find the first integer. 293 char *end; 294 int w = (int)strtol(str, &end, 10); 295 // If a delimeter does not immediately follow, give up. 296 if (*end != delim) { 297 LOGE("Cannot find delimeter (%c) in str=%s", delim, str); 298 return -1; 299 } 300 301 // Find the second integer, immediately after the delimeter. 302 int h = (int)strtol(end+1, &end, 10); 303 304 *first = w; 305 *second = h; 306 307 if (endptr) { 308 *endptr = end; 309 } 310 311 return 0; 312 } 313 314 static void parseSizesList(const char *sizesStr, Vector<Size> &sizes) 315 { 316 if (sizesStr == 0) { 317 return; 318 } 319 320 char *sizeStartPtr = (char *)sizesStr; 321 322 while (true) { 323 int width, height; 324 int success = parse_pair(sizeStartPtr, &width, &height, 'x', 325 &sizeStartPtr); 326 if (success == -1 || (*sizeStartPtr != ',' && *sizeStartPtr != '\0')) { 327 LOGE("Picture sizes string \"%s\" contains invalid character.", sizesStr); 328 return; 329 } 330 sizes.push(Size(width, height)); 331 332 if (*sizeStartPtr == '\0') { 333 return; 334 } 335 sizeStartPtr++; 336 } 337 } 338 339 void CameraParameters::setPreviewSize(int width, int height) 340 { 341 char str[32]; 342 sprintf(str, "%dx%d", width, height); 343 set(KEY_PREVIEW_SIZE, str); 344 } 345 346 void CameraParameters::getPreviewSize(int *width, int *height) const 347 { 348 *width = *height = -1; 349 // Get the current string, if it doesn't exist, leave the -1x-1 350 const char *p = get(KEY_PREVIEW_SIZE); 351 if (p == 0) return; 352 parse_pair(p, width, height, 'x'); 353 } 354 355 void CameraParameters::getPreferredPreviewSizeForVideo(int *width, int *height) const 356 { 357 *width = *height = -1; 358 const char *p = get(KEY_PREFERRED_PREVIEW_SIZE_FOR_VIDEO); 359 if (p == 0) return; 360 parse_pair(p, width, height, 'x'); 361 } 362 363 void CameraParameters::getSupportedPreviewSizes(Vector<Size> &sizes) const 364 { 365 const char *previewSizesStr = get(KEY_SUPPORTED_PREVIEW_SIZES); 366 parseSizesList(previewSizesStr, sizes); 367 } 368 369 void CameraParameters::setVideoSize(int width, int height) 370 { 371 char str[32]; 372 sprintf(str, "%dx%d", width, height); 373 set(KEY_VIDEO_SIZE, str); 374 } 375 376 void CameraParameters::getVideoSize(int *width, int *height) const 377 { 378 *width = *height = -1; 379 const char *p = get(KEY_VIDEO_SIZE); 380 if (p == 0) return; 381 parse_pair(p, width, height, 'x'); 382 } 383 384 void CameraParameters::getSupportedVideoSizes(Vector<Size> &sizes) const 385 { 386 const char *videoSizesStr = get(KEY_SUPPORTED_VIDEO_SIZES); 387 parseSizesList(videoSizesStr, sizes); 388 } 389 390 void CameraParameters::setPreviewFrameRate(int fps) 391 { 392 set(KEY_PREVIEW_FRAME_RATE, fps); 393 } 394 395 int CameraParameters::getPreviewFrameRate() const 396 { 397 return getInt(KEY_PREVIEW_FRAME_RATE); 398 } 399 400 void CameraParameters::getPreviewFpsRange(int *min_fps, int *max_fps) const 401 { 402 *min_fps = *max_fps = -1; 403 const char *p = get(KEY_PREVIEW_FPS_RANGE); 404 if (p == 0) return; 405 parse_pair(p, min_fps, max_fps, ','); 406 } 407 408 void CameraParameters::setPreviewFormat(const char *format) 409 { 410 set(KEY_PREVIEW_FORMAT, format); 411 } 412 413 const char *CameraParameters::getPreviewFormat() const 414 { 415 return get(KEY_PREVIEW_FORMAT); 416 } 417 418 void CameraParameters::setPictureSize(int width, int height) 419 { 420 char str[32]; 421 sprintf(str, "%dx%d", width, height); 422 set(KEY_PICTURE_SIZE, str); 423 } 424 425 void CameraParameters::getPictureSize(int *width, int *height) const 426 { 427 *width = *height = -1; 428 // Get the current string, if it doesn't exist, leave the -1x-1 429 const char *p = get(KEY_PICTURE_SIZE); 430 if (p == 0) return; 431 parse_pair(p, width, height, 'x'); 432 } 433 434 void CameraParameters::getSupportedPictureSizes(Vector<Size> &sizes) const 435 { 436 const char *pictureSizesStr = get(KEY_SUPPORTED_PICTURE_SIZES); 437 parseSizesList(pictureSizesStr, sizes); 438 } 439 440 void CameraParameters::setPictureFormat(const char *format) 441 { 442 set(KEY_PICTURE_FORMAT, format); 443 } 444 445 const char *CameraParameters::getPictureFormat() const 446 { 447 return get(KEY_PICTURE_FORMAT); 448 } 449 450 void CameraParameters::dump() const 451 { 452 LOGD("dump: mMap.size = %d", mMap.size()); 453 for (size_t i = 0; i < mMap.size(); i++) { 454 String8 k, v; 455 k = mMap.keyAt(i); 456 v = mMap.valueAt(i); 457 LOGD("%s: %s\n", k.string(), v.string()); 458 } 459 } 460 461 status_t CameraParameters::dump(int fd, const Vector<String16>& args) const 462 { 463 const size_t SIZE = 256; 464 char buffer[SIZE]; 465 String8 result; 466 snprintf(buffer, 255, "CameraParameters::dump: mMap.size = %d\n", mMap.size()); 467 result.append(buffer); 468 for (size_t i = 0; i < mMap.size(); i++) { 469 String8 k, v; 470 k = mMap.keyAt(i); 471 v = mMap.valueAt(i); 472 snprintf(buffer, 255, "\t%s: %s\n", k.string(), v.string()); 473 result.append(buffer); 474 } 475 write(fd, result.string(), result.size()); 476 return NO_ERROR; 477 } 478 479 }; // namespace android 480