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_PICTURE_SIZE[] = "picture-size"; 34 const char CameraParameters::KEY_SUPPORTED_PICTURE_SIZES[] = "picture-size-values"; 35 const char CameraParameters::KEY_PICTURE_FORMAT[] = "picture-format"; 36 const char CameraParameters::KEY_SUPPORTED_PICTURE_FORMATS[] = "picture-format-values"; 37 const char CameraParameters::KEY_JPEG_THUMBNAIL_WIDTH[] = "jpeg-thumbnail-width"; 38 const char CameraParameters::KEY_JPEG_THUMBNAIL_HEIGHT[] = "jpeg-thumbnail-height"; 39 const char CameraParameters::KEY_SUPPORTED_JPEG_THUMBNAIL_SIZES[] = "jpeg-thumbnail-size-values"; 40 const char CameraParameters::KEY_JPEG_THUMBNAIL_QUALITY[] = "jpeg-thumbnail-quality"; 41 const char CameraParameters::KEY_JPEG_QUALITY[] = "jpeg-quality"; 42 const char CameraParameters::KEY_ROTATION[] = "rotation"; 43 const char CameraParameters::KEY_GPS_LATITUDE[] = "gps-latitude"; 44 const char CameraParameters::KEY_GPS_LONGITUDE[] = "gps-longitude"; 45 const char CameraParameters::KEY_GPS_ALTITUDE[] = "gps-altitude"; 46 const char CameraParameters::KEY_GPS_TIMESTAMP[] = "gps-timestamp"; 47 const char CameraParameters::KEY_GPS_PROCESSING_METHOD[] = "gps-processing-method"; 48 const char CameraParameters::KEY_WHITE_BALANCE[] = "whitebalance"; 49 const char CameraParameters::KEY_SUPPORTED_WHITE_BALANCE[] = "whitebalance-values"; 50 const char CameraParameters::KEY_EFFECT[] = "effect"; 51 const char CameraParameters::KEY_SUPPORTED_EFFECTS[] = "effect-values"; 52 const char CameraParameters::KEY_ANTIBANDING[] = "antibanding"; 53 const char CameraParameters::KEY_SUPPORTED_ANTIBANDING[] = "antibanding-values"; 54 const char CameraParameters::KEY_SCENE_MODE[] = "scene-mode"; 55 const char CameraParameters::KEY_SUPPORTED_SCENE_MODES[] = "scene-mode-values"; 56 const char CameraParameters::KEY_FLASH_MODE[] = "flash-mode"; 57 const char CameraParameters::KEY_SUPPORTED_FLASH_MODES[] = "flash-mode-values"; 58 const char CameraParameters::KEY_FOCUS_MODE[] = "focus-mode"; 59 const char CameraParameters::KEY_SUPPORTED_FOCUS_MODES[] = "focus-mode-values"; 60 const char CameraParameters::KEY_FOCAL_LENGTH[] = "focal-length"; 61 const char CameraParameters::KEY_HORIZONTAL_VIEW_ANGLE[] = "horizontal-view-angle"; 62 const char CameraParameters::KEY_VERTICAL_VIEW_ANGLE[] = "vertical-view-angle"; 63 const char CameraParameters::KEY_EXPOSURE_COMPENSATION[] = "exposure-compensation"; 64 const char CameraParameters::KEY_MAX_EXPOSURE_COMPENSATION[] = "max-exposure-compensation"; 65 const char CameraParameters::KEY_MIN_EXPOSURE_COMPENSATION[] = "min-exposure-compensation"; 66 const char CameraParameters::KEY_EXPOSURE_COMPENSATION_STEP[] = "exposure-compensation-step"; 67 const char CameraParameters::KEY_ZOOM[] = "zoom"; 68 const char CameraParameters::KEY_MAX_ZOOM[] = "max-zoom"; 69 const char CameraParameters::KEY_ZOOM_RATIOS[] = "zoom-ratios"; 70 const char CameraParameters::KEY_ZOOM_SUPPORTED[] = "zoom-supported"; 71 const char CameraParameters::KEY_SMOOTH_ZOOM_SUPPORTED[] = "smooth-zoom-supported"; 72 73 const char CameraParameters::TRUE[] = "true"; 74 75 // Values for white balance settings. 76 const char CameraParameters::WHITE_BALANCE_AUTO[] = "auto"; 77 const char CameraParameters::WHITE_BALANCE_INCANDESCENT[] = "incandescent"; 78 const char CameraParameters::WHITE_BALANCE_FLUORESCENT[] = "fluorescent"; 79 const char CameraParameters::WHITE_BALANCE_WARM_FLUORESCENT[] = "warm-fluorescent"; 80 const char CameraParameters::WHITE_BALANCE_DAYLIGHT[] = "daylight"; 81 const char CameraParameters::WHITE_BALANCE_CLOUDY_DAYLIGHT[] = "cloudy-daylight"; 82 const char CameraParameters::WHITE_BALANCE_TWILIGHT[] = "twilight"; 83 const char CameraParameters::WHITE_BALANCE_SHADE[] = "shade"; 84 85 // Values for effect settings. 86 const char CameraParameters::EFFECT_NONE[] = "none"; 87 const char CameraParameters::EFFECT_MONO[] = "mono"; 88 const char CameraParameters::EFFECT_NEGATIVE[] = "negative"; 89 const char CameraParameters::EFFECT_SOLARIZE[] = "solarize"; 90 const char CameraParameters::EFFECT_SEPIA[] = "sepia"; 91 const char CameraParameters::EFFECT_POSTERIZE[] = "posterize"; 92 const char CameraParameters::EFFECT_WHITEBOARD[] = "whiteboard"; 93 const char CameraParameters::EFFECT_BLACKBOARD[] = "blackboard"; 94 const char CameraParameters::EFFECT_AQUA[] = "aqua"; 95 96 // Values for antibanding settings. 97 const char CameraParameters::ANTIBANDING_AUTO[] = "auto"; 98 const char CameraParameters::ANTIBANDING_50HZ[] = "50hz"; 99 const char CameraParameters::ANTIBANDING_60HZ[] = "60hz"; 100 const char CameraParameters::ANTIBANDING_OFF[] = "off"; 101 102 // Values for flash mode settings. 103 const char CameraParameters::FLASH_MODE_OFF[] = "off"; 104 const char CameraParameters::FLASH_MODE_AUTO[] = "auto"; 105 const char CameraParameters::FLASH_MODE_ON[] = "on"; 106 const char CameraParameters::FLASH_MODE_RED_EYE[] = "red-eye"; 107 const char CameraParameters::FLASH_MODE_TORCH[] = "torch"; 108 109 // Values for scene mode settings. 110 const char CameraParameters::SCENE_MODE_AUTO[] = "auto"; 111 const char CameraParameters::SCENE_MODE_ACTION[] = "action"; 112 const char CameraParameters::SCENE_MODE_PORTRAIT[] = "portrait"; 113 const char CameraParameters::SCENE_MODE_LANDSCAPE[] = "landscape"; 114 const char CameraParameters::SCENE_MODE_NIGHT[] = "night"; 115 const char CameraParameters::SCENE_MODE_NIGHT_PORTRAIT[] = "night-portrait"; 116 const char CameraParameters::SCENE_MODE_THEATRE[] = "theatre"; 117 const char CameraParameters::SCENE_MODE_BEACH[] = "beach"; 118 const char CameraParameters::SCENE_MODE_SNOW[] = "snow"; 119 const char CameraParameters::SCENE_MODE_SUNSET[] = "sunset"; 120 const char CameraParameters::SCENE_MODE_STEADYPHOTO[] = "steadyphoto"; 121 const char CameraParameters::SCENE_MODE_FIREWORKS[] = "fireworks"; 122 const char CameraParameters::SCENE_MODE_SPORTS[] = "sports"; 123 const char CameraParameters::SCENE_MODE_PARTY[] = "party"; 124 const char CameraParameters::SCENE_MODE_CANDLELIGHT[] = "candlelight"; 125 const char CameraParameters::SCENE_MODE_BARCODE[] = "barcode"; 126 127 // Formats for setPreviewFormat and setPictureFormat. 128 const char CameraParameters::PIXEL_FORMAT_YUV422SP[] = "yuv422sp"; 129 const char CameraParameters::PIXEL_FORMAT_YUV420SP[] = "yuv420sp"; 130 const char CameraParameters::PIXEL_FORMAT_YUV422I[] = "yuv422i-yuyv"; 131 const char CameraParameters::PIXEL_FORMAT_RGB565[] = "rgb565"; 132 const char CameraParameters::PIXEL_FORMAT_JPEG[] = "jpeg"; 133 134 // Values for focus mode settings. 135 const char CameraParameters::FOCUS_MODE_AUTO[] = "auto"; 136 const char CameraParameters::FOCUS_MODE_INFINITY[] = "infinity"; 137 const char CameraParameters::FOCUS_MODE_MACRO[] = "macro"; 138 const char CameraParameters::FOCUS_MODE_FIXED[] = "fixed"; 139 const char CameraParameters::FOCUS_MODE_EDOF[] = "edof"; 140 141 CameraParameters::CameraParameters() 142 : mMap() 143 { 144 } 145 146 CameraParameters::~CameraParameters() 147 { 148 } 149 150 String8 CameraParameters::flatten() const 151 { 152 String8 flattened(""); 153 size_t size = mMap.size(); 154 155 for (size_t i = 0; i < size; i++) { 156 String8 k, v; 157 k = mMap.keyAt(i); 158 v = mMap.valueAt(i); 159 160 flattened += k; 161 flattened += "="; 162 flattened += v; 163 if (i != size-1) 164 flattened += ";"; 165 } 166 167 return flattened; 168 } 169 170 void CameraParameters::unflatten(const String8 ¶ms) 171 { 172 const char *a = params.string(); 173 const char *b; 174 175 mMap.clear(); 176 177 for (;;) { 178 // Find the bounds of the key name. 179 b = strchr(a, '='); 180 if (b == 0) 181 break; 182 183 // Create the key string. 184 String8 k(a, (size_t)(b-a)); 185 186 // Find the value. 187 a = b+1; 188 b = strchr(a, ';'); 189 if (b == 0) { 190 // If there's no semicolon, this is the last item. 191 String8 v(a); 192 mMap.add(k, v); 193 break; 194 } 195 196 String8 v(a, (size_t)(b-a)); 197 mMap.add(k, v); 198 a = b+1; 199 } 200 } 201 202 203 void CameraParameters::set(const char *key, const char *value) 204 { 205 // XXX i think i can do this with strspn() 206 if (strchr(key, '=') || strchr(key, ';')) { 207 //XXX LOGE("Key \"%s\"contains invalid character (= or ;)", key); 208 return; 209 } 210 211 if (strchr(value, '=') || strchr(key, ';')) { 212 //XXX LOGE("Value \"%s\"contains invalid character (= or ;)", value); 213 return; 214 } 215 216 mMap.replaceValueFor(String8(key), String8(value)); 217 } 218 219 void CameraParameters::set(const char *key, int value) 220 { 221 char str[16]; 222 sprintf(str, "%d", value); 223 set(key, str); 224 } 225 226 void CameraParameters::setFloat(const char *key, float value) 227 { 228 char str[16]; // 14 should be enough. We overestimate to be safe. 229 snprintf(str, sizeof(str), "%g", value); 230 set(key, str); 231 } 232 233 const char *CameraParameters::get(const char *key) const 234 { 235 String8 v = mMap.valueFor(String8(key)); 236 if (v.length() == 0) 237 return 0; 238 return v.string(); 239 } 240 241 int CameraParameters::getInt(const char *key) const 242 { 243 const char *v = get(key); 244 if (v == 0) 245 return -1; 246 return strtol(v, 0, 0); 247 } 248 249 float CameraParameters::getFloat(const char *key) const 250 { 251 const char *v = get(key); 252 if (v == 0) return -1; 253 return strtof(v, 0); 254 } 255 256 void CameraParameters::remove(const char *key) 257 { 258 mMap.removeItem(String8(key)); 259 } 260 261 static int parse_size(const char *str, int &width, int &height) 262 { 263 // Find the width. 264 char *end; 265 int w = (int)strtol(str, &end, 10); 266 // If an 'x' does not immediately follow, give up. 267 if (*end != 'x') 268 return -1; 269 270 // Find the height, immediately after the 'x'. 271 int h = (int)strtol(end+1, 0, 10); 272 273 width = w; 274 height = h; 275 276 return 0; 277 } 278 279 void CameraParameters::setPreviewSize(int width, int height) 280 { 281 char str[32]; 282 sprintf(str, "%dx%d", width, height); 283 set(KEY_PREVIEW_SIZE, str); 284 } 285 286 void CameraParameters::getPreviewSize(int *width, int *height) const 287 { 288 *width = -1; 289 *height = -1; 290 291 // Get the current string, if it doesn't exist, leave the -1x-1 292 const char *p = get(KEY_PREVIEW_SIZE); 293 if (p == 0) 294 return; 295 296 int w, h; 297 if (parse_size(p, w, h) == 0) { 298 *width = w; 299 *height = h; 300 } 301 } 302 303 void CameraParameters::setPreviewFrameRate(int fps) 304 { 305 set(KEY_PREVIEW_FRAME_RATE, fps); 306 } 307 308 int CameraParameters::getPreviewFrameRate() const 309 { 310 return getInt(KEY_PREVIEW_FRAME_RATE); 311 } 312 313 void CameraParameters::setPreviewFormat(const char *format) 314 { 315 set(KEY_PREVIEW_FORMAT, format); 316 } 317 318 const char *CameraParameters::getPreviewFormat() const 319 { 320 return get(KEY_PREVIEW_FORMAT); 321 } 322 323 void CameraParameters::setPictureSize(int width, int height) 324 { 325 char str[32]; 326 sprintf(str, "%dx%d", width, height); 327 set(KEY_PICTURE_SIZE, str); 328 } 329 330 void CameraParameters::getPictureSize(int *width, int *height) const 331 { 332 *width = -1; 333 *height = -1; 334 335 // Get the current string, if it doesn't exist, leave the -1x-1 336 const char *p = get(KEY_PICTURE_SIZE); 337 if (p == 0) 338 return; 339 340 int w, h; 341 if (parse_size(p, w, h) == 0) { 342 *width = w; 343 *height = h; 344 } 345 } 346 347 void CameraParameters::setPictureFormat(const char *format) 348 { 349 set(KEY_PICTURE_FORMAT, format); 350 } 351 352 const char *CameraParameters::getPictureFormat() const 353 { 354 return get(KEY_PICTURE_FORMAT); 355 } 356 357 void CameraParameters::dump() const 358 { 359 LOGD("dump: mMap.size = %d", mMap.size()); 360 for (size_t i = 0; i < mMap.size(); i++) { 361 String8 k, v; 362 k = mMap.keyAt(i); 363 v = mMap.valueAt(i); 364 LOGD("%s: %s\n", k.string(), v.string()); 365 } 366 } 367 368 status_t CameraParameters::dump(int fd, const Vector<String16>& args) const 369 { 370 const size_t SIZE = 256; 371 char buffer[SIZE]; 372 String8 result; 373 snprintf(buffer, 255, "CameraParameters::dump: mMap.size = %d\n", mMap.size()); 374 result.append(buffer); 375 for (size_t i = 0; i < mMap.size(); i++) { 376 String8 k, v; 377 k = mMap.keyAt(i); 378 v = mMap.valueAt(i); 379 snprintf(buffer, 255, "\t%s: %s\n", k.string(), v.string()); 380 result.append(buffer); 381 } 382 write(fd, result.string(), result.size()); 383 return NO_ERROR; 384 } 385 386 }; // namespace android 387