The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2008, The Android Open Source Project |
| 4 | ** |
Wu-cheng Li | 8de57d8 | 2009-09-23 14:37:52 -0700 | [diff] [blame] | 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 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8 | ** |
Wu-cheng Li | 8de57d8 | 2009-09-23 14:37:52 -0700 | [diff] [blame] | 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10 | ** |
Wu-cheng Li | 8de57d8 | 2009-09-23 14:37:52 -0700 | [diff] [blame] | 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 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 <ui/CameraParameters.h> |
| 24 | |
| 25 | namespace android { |
Wu-cheng Li | 8de57d8 | 2009-09-23 14:37:52 -0700 | [diff] [blame] | 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_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_WHITE_BALANCE[] = "whitebalance"; |
| 48 | const char CameraParameters::KEY_SUPPORTED_WHITE_BALANCE[] = "whitebalance-values"; |
| 49 | const char CameraParameters::KEY_EFFECT[] = "effect"; |
| 50 | const char CameraParameters::KEY_SUPPORTED_EFFECTS[] = "effect-values"; |
| 51 | const char CameraParameters::KEY_ANTIBANDING[] = "antibanding"; |
| 52 | const char CameraParameters::KEY_SUPPORTED_ANTIBANDING[] = "antibanding-values"; |
| 53 | const char CameraParameters::KEY_SCENE_MODE[] = "scene-mode"; |
| 54 | const char CameraParameters::KEY_SUPPORTED_SCENE_MODES[] = "scene-mode-values"; |
| 55 | const char CameraParameters::KEY_FLASH_MODE[] = "flash-mode"; |
| 56 | const char CameraParameters::KEY_SUPPORTED_FLASH_MODES[] = "flash-mode-values"; |
| 57 | const char CameraParameters::KEY_FOCUS_MODE[] = "focus-mode"; |
| 58 | const char CameraParameters::KEY_SUPPORTED_FOCUS_MODES[] = "focus-mode-values"; |
| 59 | |
| 60 | // Values for white balance settings. |
| 61 | const char CameraParameters::WHITE_BALANCE_AUTO[] = "auto"; |
| 62 | const char CameraParameters::WHITE_BALANCE_INCANDESCENT[] = "incandescent"; |
| 63 | const char CameraParameters::WHITE_BALANCE_FLUORESCENT[] = "fluorescent"; |
| 64 | const char CameraParameters::WHITE_BALANCE_WARM_FLUORESCENT[] = "warm-fluorescent"; |
| 65 | const char CameraParameters::WHITE_BALANCE_DAYLIGHT[] = "daylight"; |
| 66 | const char CameraParameters::WHITE_BALANCE_CLOUDY_DAYLIGHT[] = "cloudy-daylight"; |
| 67 | const char CameraParameters::WHITE_BALANCE_TWILIGHT[] = "twilight"; |
| 68 | const char CameraParameters::WHITE_BALANCE_SHADE[] = "shade"; |
| 69 | |
| 70 | // Values for effect settings. |
| 71 | const char CameraParameters::EFFECT_NONE[] = "none"; |
| 72 | const char CameraParameters::EFFECT_MONO[] = "mono"; |
| 73 | const char CameraParameters::EFFECT_NEGATIVE[] = "negative"; |
| 74 | const char CameraParameters::EFFECT_SOLARIZE[] = "solarize"; |
| 75 | const char CameraParameters::EFFECT_SEPIA[] = "sepia"; |
| 76 | const char CameraParameters::EFFECT_POSTERIZE[] = "posterize"; |
| 77 | const char CameraParameters::EFFECT_WHITEBOARD[] = "whiteboard"; |
| 78 | const char CameraParameters::EFFECT_BLACKBOARD[] = "blackboard"; |
| 79 | const char CameraParameters::EFFECT_AQUA[] = "aqua"; |
| 80 | |
| 81 | // Values for antibanding settings. |
| 82 | const char CameraParameters::ANTIBANDING_AUTO[] = "auto"; |
| 83 | const char CameraParameters::ANTIBANDING_50HZ[] = "50hz"; |
| 84 | const char CameraParameters::ANTIBANDING_60HZ[] = "60hz"; |
| 85 | const char CameraParameters::ANTIBANDING_OFF[] = "off"; |
| 86 | |
| 87 | // Values for flash mode settings. |
| 88 | const char CameraParameters::FLASH_MODE_OFF[] = "off"; |
| 89 | const char CameraParameters::FLASH_MODE_AUTO[] = "auto"; |
| 90 | const char CameraParameters::FLASH_MODE_ON[] = "on"; |
| 91 | const char CameraParameters::FLASH_MODE_RED_EYE[] = "red-eye"; |
Wu-cheng Li | fe1a86d | 2009-09-28 13:51:12 -0700 | [diff] [blame] | 92 | const char CameraParameters::FLASH_MODE_TORCH[] = "torch"; |
Wu-cheng Li | 8de57d8 | 2009-09-23 14:37:52 -0700 | [diff] [blame] | 93 | |
| 94 | // Values for scene mode settings. |
| 95 | const char CameraParameters::SCENE_MODE_AUTO[] = "auto"; |
| 96 | const char CameraParameters::SCENE_MODE_ACTION[] = "action"; |
| 97 | const char CameraParameters::SCENE_MODE_PORTRAIT[] = "portrait"; |
| 98 | const char CameraParameters::SCENE_MODE_LANDSCAPE[] = "landscape"; |
| 99 | const char CameraParameters::SCENE_MODE_NIGHT[] = "night"; |
| 100 | const char CameraParameters::SCENE_MODE_NIGHT_PORTRAIT[] = "night-portrait"; |
| 101 | const char CameraParameters::SCENE_MODE_THEATRE[] = "theatre"; |
| 102 | const char CameraParameters::SCENE_MODE_BEACH[] = "beach"; |
| 103 | const char CameraParameters::SCENE_MODE_SNOW[] = "snow"; |
| 104 | const char CameraParameters::SCENE_MODE_SUNSET[] = "sunset"; |
| 105 | const char CameraParameters::SCENE_MODE_STEADYPHOTO[] = "steadyphoto"; |
| 106 | const char CameraParameters::SCENE_MODE_FIREWORKS[] = "fireworks"; |
| 107 | const char CameraParameters::SCENE_MODE_SPORTS[] = "sports"; |
| 108 | const char CameraParameters::SCENE_MODE_PARTY[] = "party"; |
| 109 | const char CameraParameters::SCENE_MODE_CANDLELIGHT[] = "candlelight"; |
| 110 | |
| 111 | // Formats for setPreviewFormat and setPictureFormat. |
| 112 | const char CameraParameters::PIXEL_FORMAT_YUV422SP[] = "yuv422sp"; |
| 113 | const char CameraParameters::PIXEL_FORMAT_YUV420SP[] = "yuv420sp"; |
| 114 | const char CameraParameters::PIXEL_FORMAT_YUV422I[] = "yuv422i-yuyv"; |
| 115 | const char CameraParameters::PIXEL_FORMAT_RGB565[] = "rgb565"; |
| 116 | const char CameraParameters::PIXEL_FORMAT_JPEG[] = "jpeg"; |
| 117 | |
| 118 | // Values for focus mode settings. |
| 119 | const char CameraParameters::FOCUS_MODE_AUTO[] = "auto"; |
| 120 | const char CameraParameters::FOCUS_MODE_INFINITY[] = "infinity"; |
| 121 | const char CameraParameters::FOCUS_MODE_MACRO[] = "macro"; |
| 122 | const char CameraParameters::FOCUS_MODE_FIXED[] = "fixed"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | |
| 124 | static const char* portrait = "portrait"; |
| 125 | static const char* landscape = "landscape"; |
| 126 | |
| 127 | CameraParameters::CameraParameters() |
| 128 | : mMap() |
| 129 | { |
| 130 | } |
| 131 | |
| 132 | CameraParameters::~CameraParameters() |
| 133 | { |
| 134 | } |
| 135 | |
| 136 | String8 CameraParameters::flatten() const |
| 137 | { |
| 138 | String8 flattened(""); |
| 139 | size_t size = mMap.size(); |
| 140 | |
| 141 | for (size_t i = 0; i < size; i++) { |
| 142 | String8 k, v; |
| 143 | k = mMap.keyAt(i); |
| 144 | v = mMap.valueAt(i); |
| 145 | |
| 146 | flattened += k; |
| 147 | flattened += "="; |
| 148 | flattened += v; |
| 149 | if (i != size-1) |
| 150 | flattened += ";"; |
| 151 | } |
| 152 | |
| 153 | return flattened; |
| 154 | } |
| 155 | |
| 156 | void CameraParameters::unflatten(const String8 ¶ms) |
| 157 | { |
| 158 | const char *a = params.string(); |
| 159 | const char *b; |
| 160 | |
| 161 | mMap.clear(); |
| 162 | |
| 163 | for (;;) { |
| 164 | // Find the bounds of the key name. |
| 165 | b = strchr(a, '='); |
| 166 | if (b == 0) |
| 167 | break; |
| 168 | |
| 169 | // Create the key string. |
| 170 | String8 k(a, (size_t)(b-a)); |
| 171 | |
| 172 | // Find the value. |
| 173 | a = b+1; |
| 174 | b = strchr(a, ';'); |
| 175 | if (b == 0) { |
| 176 | // If there's no semicolon, this is the last item. |
| 177 | String8 v(a); |
| 178 | mMap.add(k, v); |
| 179 | break; |
| 180 | } |
| 181 | |
| 182 | String8 v(a, (size_t)(b-a)); |
| 183 | mMap.add(k, v); |
| 184 | a = b+1; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | |
| 189 | void CameraParameters::set(const char *key, const char *value) |
| 190 | { |
Wu-cheng Li | 8de57d8 | 2009-09-23 14:37:52 -0700 | [diff] [blame] | 191 | // XXX i think i can do this with strspn() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | if (strchr(key, '=') || strchr(key, ';')) { |
| 193 | //XXX LOGE("Key \"%s\"contains invalid character (= or ;)", key); |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | if (strchr(value, '=') || strchr(key, ';')) { |
| 198 | //XXX LOGE("Value \"%s\"contains invalid character (= or ;)", value); |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | mMap.replaceValueFor(String8(key), String8(value)); |
| 203 | } |
| 204 | |
| 205 | void CameraParameters::set(const char *key, int value) |
| 206 | { |
| 207 | char str[16]; |
| 208 | sprintf(str, "%d", value); |
| 209 | set(key, str); |
| 210 | } |
| 211 | |
| 212 | const char *CameraParameters::get(const char *key) const |
| 213 | { |
| 214 | String8 v = mMap.valueFor(String8(key)); |
| 215 | if (v.length() == 0) |
| 216 | return 0; |
| 217 | return v.string(); |
| 218 | } |
| 219 | |
| 220 | int CameraParameters::getInt(const char *key) const |
| 221 | { |
| 222 | const char *v = get(key); |
| 223 | if (v == 0) |
| 224 | return -1; |
| 225 | return strtol(v, 0, 0); |
| 226 | } |
| 227 | |
| 228 | static int parse_size(const char *str, int &width, int &height) |
| 229 | { |
| 230 | // Find the width. |
| 231 | char *end; |
| 232 | int w = (int)strtol(str, &end, 10); |
| 233 | // If an 'x' does not immediately follow, give up. |
| 234 | if (*end != 'x') |
| 235 | return -1; |
| 236 | |
| 237 | // Find the height, immediately after the 'x'. |
| 238 | int h = (int)strtol(end+1, 0, 10); |
| 239 | |
| 240 | width = w; |
| 241 | height = h; |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | void CameraParameters::setPreviewSize(int width, int height) |
| 247 | { |
| 248 | char str[32]; |
| 249 | sprintf(str, "%dx%d", width, height); |
Wu-cheng Li | 8de57d8 | 2009-09-23 14:37:52 -0700 | [diff] [blame] | 250 | set(KEY_PREVIEW_SIZE, str); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | void CameraParameters::getPreviewSize(int *width, int *height) const |
| 254 | { |
| 255 | *width = -1; |
| 256 | *height = -1; |
| 257 | |
| 258 | // Get the current string, if it doesn't exist, leave the -1x-1 |
Wu-cheng Li | 8de57d8 | 2009-09-23 14:37:52 -0700 | [diff] [blame] | 259 | const char *p = get(KEY_PREVIEW_SIZE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | if (p == 0) |
| 261 | return; |
| 262 | |
| 263 | int w, h; |
| 264 | if (parse_size(p, w, h) == 0) { |
| 265 | *width = w; |
| 266 | *height = h; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | void CameraParameters::setPreviewFrameRate(int fps) |
| 271 | { |
Wu-cheng Li | 8de57d8 | 2009-09-23 14:37:52 -0700 | [diff] [blame] | 272 | set(KEY_PREVIEW_FRAME_RATE, fps); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | int CameraParameters::getPreviewFrameRate() const |
| 276 | { |
Wu-cheng Li | 8de57d8 | 2009-09-23 14:37:52 -0700 | [diff] [blame] | 277 | return getInt(KEY_PREVIEW_FRAME_RATE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | void CameraParameters::setPreviewFormat(const char *format) |
| 281 | { |
Wu-cheng Li | 8de57d8 | 2009-09-23 14:37:52 -0700 | [diff] [blame] | 282 | set(KEY_PREVIEW_FORMAT, format); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | int CameraParameters::getOrientation() const |
| 286 | { |
| 287 | const char* orientation = get("orientation"); |
| 288 | if (orientation && !strcmp(orientation, portrait)) |
| 289 | return CAMERA_ORIENTATION_PORTRAIT; |
| 290 | return CAMERA_ORIENTATION_LANDSCAPE; |
| 291 | } |
| 292 | |
| 293 | void CameraParameters::setOrientation(int orientation) |
| 294 | { |
| 295 | if (orientation == CAMERA_ORIENTATION_PORTRAIT) { |
Wu-cheng Li | 8de57d8 | 2009-09-23 14:37:52 -0700 | [diff] [blame] | 296 | set("orientation", portrait); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 297 | } else { |
Wu-cheng Li | 8de57d8 | 2009-09-23 14:37:52 -0700 | [diff] [blame] | 298 | set("orientation", landscape); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | |
| 302 | const char *CameraParameters::getPreviewFormat() const |
| 303 | { |
Wu-cheng Li | 8de57d8 | 2009-09-23 14:37:52 -0700 | [diff] [blame] | 304 | return get(KEY_PREVIEW_FORMAT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | void CameraParameters::setPictureSize(int width, int height) |
| 308 | { |
| 309 | char str[32]; |
| 310 | sprintf(str, "%dx%d", width, height); |
Wu-cheng Li | 8de57d8 | 2009-09-23 14:37:52 -0700 | [diff] [blame] | 311 | set(KEY_PICTURE_SIZE, str); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | void CameraParameters::getPictureSize(int *width, int *height) const |
| 315 | { |
| 316 | *width = -1; |
| 317 | *height = -1; |
| 318 | |
| 319 | // Get the current string, if it doesn't exist, leave the -1x-1 |
Wu-cheng Li | 8de57d8 | 2009-09-23 14:37:52 -0700 | [diff] [blame] | 320 | const char *p = get(KEY_PICTURE_SIZE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 321 | if (p == 0) |
| 322 | return; |
| 323 | |
| 324 | int w, h; |
| 325 | if (parse_size(p, w, h) == 0) { |
| 326 | *width = w; |
| 327 | *height = h; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | void CameraParameters::setPictureFormat(const char *format) |
| 332 | { |
Wu-cheng Li | 8de57d8 | 2009-09-23 14:37:52 -0700 | [diff] [blame] | 333 | set(KEY_PICTURE_FORMAT, format); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | const char *CameraParameters::getPictureFormat() const |
| 337 | { |
Wu-cheng Li | 8de57d8 | 2009-09-23 14:37:52 -0700 | [diff] [blame] | 338 | return get(KEY_PICTURE_FORMAT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | void CameraParameters::dump() const |
| 342 | { |
| 343 | LOGD("dump: mMap.size = %d", mMap.size()); |
| 344 | for (size_t i = 0; i < mMap.size(); i++) { |
| 345 | String8 k, v; |
| 346 | k = mMap.keyAt(i); |
| 347 | v = mMap.valueAt(i); |
| 348 | LOGD("%s: %s\n", k.string(), v.string()); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | status_t CameraParameters::dump(int fd, const Vector<String16>& args) const |
| 353 | { |
| 354 | const size_t SIZE = 256; |
| 355 | char buffer[SIZE]; |
| 356 | String8 result; |
| 357 | snprintf(buffer, 255, "CameraParameters::dump: mMap.size = %d\n", mMap.size()); |
| 358 | result.append(buffer); |
| 359 | for (size_t i = 0; i < mMap.size(); i++) { |
| 360 | String8 k, v; |
| 361 | k = mMap.keyAt(i); |
| 362 | v = mMap.valueAt(i); |
| 363 | snprintf(buffer, 255, "\t%s: %s\n", k.string(), v.string()); |
| 364 | result.append(buffer); |
| 365 | } |
| 366 | write(fd, result.string(), result.size()); |
| 367 | return NO_ERROR; |
| 368 | } |
| 369 | |
| 370 | }; // namespace android |