Home | History | Annotate | Download | only in camera
      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 const char CameraParameters::KEY_LIGHTFX[] = "light-fx";
     94 
     95 const char CameraParameters::TRUE[] = "true";
     96 const char CameraParameters::FALSE[] = "false";
     97 const char CameraParameters::FOCUS_DISTANCE_INFINITY[] = "Infinity";
     98 
     99 // Values for white balance settings.
    100 const char CameraParameters::WHITE_BALANCE_AUTO[] = "auto";
    101 const char CameraParameters::WHITE_BALANCE_INCANDESCENT[] = "incandescent";
    102 const char CameraParameters::WHITE_BALANCE_FLUORESCENT[] = "fluorescent";
    103 const char CameraParameters::WHITE_BALANCE_WARM_FLUORESCENT[] = "warm-fluorescent";
    104 const char CameraParameters::WHITE_BALANCE_DAYLIGHT[] = "daylight";
    105 const char CameraParameters::WHITE_BALANCE_CLOUDY_DAYLIGHT[] = "cloudy-daylight";
    106 const char CameraParameters::WHITE_BALANCE_TWILIGHT[] = "twilight";
    107 const char CameraParameters::WHITE_BALANCE_SHADE[] = "shade";
    108 
    109 // Values for effect settings.
    110 const char CameraParameters::EFFECT_NONE[] = "none";
    111 const char CameraParameters::EFFECT_MONO[] = "mono";
    112 const char CameraParameters::EFFECT_NEGATIVE[] = "negative";
    113 const char CameraParameters::EFFECT_SOLARIZE[] = "solarize";
    114 const char CameraParameters::EFFECT_SEPIA[] = "sepia";
    115 const char CameraParameters::EFFECT_POSTERIZE[] = "posterize";
    116 const char CameraParameters::EFFECT_WHITEBOARD[] = "whiteboard";
    117 const char CameraParameters::EFFECT_BLACKBOARD[] = "blackboard";
    118 const char CameraParameters::EFFECT_AQUA[] = "aqua";
    119 
    120 // Values for antibanding settings.
    121 const char CameraParameters::ANTIBANDING_AUTO[] = "auto";
    122 const char CameraParameters::ANTIBANDING_50HZ[] = "50hz";
    123 const char CameraParameters::ANTIBANDING_60HZ[] = "60hz";
    124 const char CameraParameters::ANTIBANDING_OFF[] = "off";
    125 
    126 // Values for flash mode settings.
    127 const char CameraParameters::FLASH_MODE_OFF[] = "off";
    128 const char CameraParameters::FLASH_MODE_AUTO[] = "auto";
    129 const char CameraParameters::FLASH_MODE_ON[] = "on";
    130 const char CameraParameters::FLASH_MODE_RED_EYE[] = "red-eye";
    131 const char CameraParameters::FLASH_MODE_TORCH[] = "torch";
    132 
    133 // Values for scene mode settings.
    134 const char CameraParameters::SCENE_MODE_AUTO[] = "auto";
    135 const char CameraParameters::SCENE_MODE_ACTION[] = "action";
    136 const char CameraParameters::SCENE_MODE_PORTRAIT[] = "portrait";
    137 const char CameraParameters::SCENE_MODE_LANDSCAPE[] = "landscape";
    138 const char CameraParameters::SCENE_MODE_NIGHT[] = "night";
    139 const char CameraParameters::SCENE_MODE_NIGHT_PORTRAIT[] = "night-portrait";
    140 const char CameraParameters::SCENE_MODE_THEATRE[] = "theatre";
    141 const char CameraParameters::SCENE_MODE_BEACH[] = "beach";
    142 const char CameraParameters::SCENE_MODE_SNOW[] = "snow";
    143 const char CameraParameters::SCENE_MODE_SUNSET[] = "sunset";
    144 const char CameraParameters::SCENE_MODE_STEADYPHOTO[] = "steadyphoto";
    145 const char CameraParameters::SCENE_MODE_FIREWORKS[] = "fireworks";
    146 const char CameraParameters::SCENE_MODE_SPORTS[] = "sports";
    147 const char CameraParameters::SCENE_MODE_PARTY[] = "party";
    148 const char CameraParameters::SCENE_MODE_CANDLELIGHT[] = "candlelight";
    149 const char CameraParameters::SCENE_MODE_BARCODE[] = "barcode";
    150 const char CameraParameters::SCENE_MODE_HDR[] = "hdr";
    151 
    152 const char CameraParameters::PIXEL_FORMAT_YUV422SP[] = "yuv422sp";
    153 const char CameraParameters::PIXEL_FORMAT_YUV420SP[] = "yuv420sp";
    154 const char CameraParameters::PIXEL_FORMAT_YUV422I[] = "yuv422i-yuyv";
    155 const char CameraParameters::PIXEL_FORMAT_YUV420P[]  = "yuv420p";
    156 const char CameraParameters::PIXEL_FORMAT_RGB565[] = "rgb565";
    157 const char CameraParameters::PIXEL_FORMAT_RGBA8888[] = "rgba8888";
    158 const char CameraParameters::PIXEL_FORMAT_JPEG[] = "jpeg";
    159 const char CameraParameters::PIXEL_FORMAT_BAYER_RGGB[] = "bayer-rggb";
    160 const char CameraParameters::PIXEL_FORMAT_ANDROID_OPAQUE[] = "android-opaque";
    161 
    162 // Values for focus mode settings.
    163 const char CameraParameters::FOCUS_MODE_AUTO[] = "auto";
    164 const char CameraParameters::FOCUS_MODE_INFINITY[] = "infinity";
    165 const char CameraParameters::FOCUS_MODE_MACRO[] = "macro";
    166 const char CameraParameters::FOCUS_MODE_FIXED[] = "fixed";
    167 const char CameraParameters::FOCUS_MODE_EDOF[] = "edof";
    168 const char CameraParameters::FOCUS_MODE_CONTINUOUS_VIDEO[] = "continuous-video";
    169 const char CameraParameters::FOCUS_MODE_CONTINUOUS_PICTURE[] = "continuous-picture";
    170 
    171 // Values for light fx settings
    172 const char CameraParameters::LIGHTFX_LOWLIGHT[] = "low-light";
    173 const char CameraParameters::LIGHTFX_HDR[] = "high-dynamic-range";
    174 
    175 CameraParameters::CameraParameters()
    176                 : mMap()
    177 {
    178 }
    179 
    180 CameraParameters::~CameraParameters()
    181 {
    182 }
    183 
    184 String8 CameraParameters::flatten() const
    185 {
    186     String8 flattened("");
    187     size_t size = mMap.size();
    188 
    189     for (size_t i = 0; i < size; i++) {
    190         String8 k, v;
    191         k = mMap.keyAt(i);
    192         v = mMap.valueAt(i);
    193 
    194         flattened += k;
    195         flattened += "=";
    196         flattened += v;
    197         if (i != size-1)
    198             flattened += ";";
    199     }
    200 
    201     return flattened;
    202 }
    203 
    204 void CameraParameters::unflatten(const String8 &params)
    205 {
    206     const char *a = params.string();
    207     const char *b;
    208 
    209     mMap.clear();
    210 
    211     for (;;) {
    212         // Find the bounds of the key name.
    213         b = strchr(a, '=');
    214         if (b == 0)
    215             break;
    216 
    217         // Create the key string.
    218         String8 k(a, (size_t)(b-a));
    219 
    220         // Find the value.
    221         a = b+1;
    222         b = strchr(a, ';');
    223         if (b == 0) {
    224             // If there's no semicolon, this is the last item.
    225             String8 v(a);
    226             mMap.add(k, v);
    227             break;
    228         }
    229 
    230         String8 v(a, (size_t)(b-a));
    231         mMap.add(k, v);
    232         a = b+1;
    233     }
    234 }
    235 
    236 
    237 void CameraParameters::set(const char *key, const char *value)
    238 {
    239     // XXX i think i can do this with strspn()
    240     if (strchr(key, '=') || strchr(key, ';')) {
    241         //XXX ALOGE("Key \"%s\"contains invalid character (= or ;)", key);
    242         return;
    243     }
    244 
    245     if (strchr(value, '=') || strchr(value, ';')) {
    246         //XXX ALOGE("Value \"%s\"contains invalid character (= or ;)", value);
    247         return;
    248     }
    249 
    250     mMap.replaceValueFor(String8(key), String8(value));
    251 }
    252 
    253 void CameraParameters::set(const char *key, int value)
    254 {
    255     char str[16];
    256     sprintf(str, "%d", value);
    257     set(key, str);
    258 }
    259 
    260 void CameraParameters::setFloat(const char *key, float value)
    261 {
    262     char str[16];  // 14 should be enough. We overestimate to be safe.
    263     snprintf(str, sizeof(str), "%g", value);
    264     set(key, str);
    265 }
    266 
    267 const char *CameraParameters::get(const char *key) const
    268 {
    269     String8 v = mMap.valueFor(String8(key));
    270     if (v.length() == 0)
    271         return 0;
    272     return v.string();
    273 }
    274 
    275 int CameraParameters::getInt(const char *key) const
    276 {
    277     const char *v = get(key);
    278     if (v == 0)
    279         return -1;
    280     return strtol(v, 0, 0);
    281 }
    282 
    283 float CameraParameters::getFloat(const char *key) const
    284 {
    285     const char *v = get(key);
    286     if (v == 0) return -1;
    287     return strtof(v, 0);
    288 }
    289 
    290 void CameraParameters::remove(const char *key)
    291 {
    292     mMap.removeItem(String8(key));
    293 }
    294 
    295 // Parse string like "640x480" or "10000,20000"
    296 static int parse_pair(const char *str, int *first, int *second, char delim,
    297                       char **endptr = NULL)
    298 {
    299     // Find the first integer.
    300     char *end;
    301     int w = (int)strtol(str, &end, 10);
    302     // If a delimeter does not immediately follow, give up.
    303     if (*end != delim) {
    304         ALOGE("Cannot find delimeter (%c) in str=%s", delim, str);
    305         return -1;
    306     }
    307 
    308     // Find the second integer, immediately after the delimeter.
    309     int h = (int)strtol(end+1, &end, 10);
    310 
    311     *first = w;
    312     *second = h;
    313 
    314     if (endptr) {
    315         *endptr = end;
    316     }
    317 
    318     return 0;
    319 }
    320 
    321 static void parseSizesList(const char *sizesStr, Vector<Size> &sizes)
    322 {
    323     if (sizesStr == 0) {
    324         return;
    325     }
    326 
    327     char *sizeStartPtr = (char *)sizesStr;
    328 
    329     while (true) {
    330         int width, height;
    331         int success = parse_pair(sizeStartPtr, &width, &height, 'x',
    332                                  &sizeStartPtr);
    333         if (success == -1 || (*sizeStartPtr != ',' && *sizeStartPtr != '\0')) {
    334             ALOGE("Picture sizes string \"%s\" contains invalid character.", sizesStr);
    335             return;
    336         }
    337         sizes.push(Size(width, height));
    338 
    339         if (*sizeStartPtr == '\0') {
    340             return;
    341         }
    342         sizeStartPtr++;
    343     }
    344 }
    345 
    346 void CameraParameters::setPreviewSize(int width, int height)
    347 {
    348     char str[32];
    349     sprintf(str, "%dx%d", width, height);
    350     set(KEY_PREVIEW_SIZE, str);
    351 }
    352 
    353 void CameraParameters::getPreviewSize(int *width, int *height) const
    354 {
    355     *width = *height = -1;
    356     // Get the current string, if it doesn't exist, leave the -1x-1
    357     const char *p = get(KEY_PREVIEW_SIZE);
    358     if (p == 0)  return;
    359     parse_pair(p, width, height, 'x');
    360 }
    361 
    362 void CameraParameters::getPreferredPreviewSizeForVideo(int *width, int *height) const
    363 {
    364     *width = *height = -1;
    365     const char *p = get(KEY_PREFERRED_PREVIEW_SIZE_FOR_VIDEO);
    366     if (p == 0)  return;
    367     parse_pair(p, width, height, 'x');
    368 }
    369 
    370 void CameraParameters::getSupportedPreviewSizes(Vector<Size> &sizes) const
    371 {
    372     const char *previewSizesStr = get(KEY_SUPPORTED_PREVIEW_SIZES);
    373     parseSizesList(previewSizesStr, sizes);
    374 }
    375 
    376 void CameraParameters::setVideoSize(int width, int height)
    377 {
    378     char str[32];
    379     sprintf(str, "%dx%d", width, height);
    380     set(KEY_VIDEO_SIZE, str);
    381 }
    382 
    383 void CameraParameters::getVideoSize(int *width, int *height) const
    384 {
    385     *width = *height = -1;
    386     const char *p = get(KEY_VIDEO_SIZE);
    387     if (p == 0) return;
    388     parse_pair(p, width, height, 'x');
    389 }
    390 
    391 void CameraParameters::getSupportedVideoSizes(Vector<Size> &sizes) const
    392 {
    393     const char *videoSizesStr = get(KEY_SUPPORTED_VIDEO_SIZES);
    394     parseSizesList(videoSizesStr, sizes);
    395 }
    396 
    397 void CameraParameters::setPreviewFrameRate(int fps)
    398 {
    399     set(KEY_PREVIEW_FRAME_RATE, fps);
    400 }
    401 
    402 int CameraParameters::getPreviewFrameRate() const
    403 {
    404     return getInt(KEY_PREVIEW_FRAME_RATE);
    405 }
    406 
    407 void CameraParameters::getPreviewFpsRange(int *min_fps, int *max_fps) const
    408 {
    409     *min_fps = *max_fps = -1;
    410     const char *p = get(KEY_PREVIEW_FPS_RANGE);
    411     if (p == 0) return;
    412     parse_pair(p, min_fps, max_fps, ',');
    413 }
    414 
    415 void CameraParameters::setPreviewFormat(const char *format)
    416 {
    417     set(KEY_PREVIEW_FORMAT, format);
    418 }
    419 
    420 const char *CameraParameters::getPreviewFormat() const
    421 {
    422     return get(KEY_PREVIEW_FORMAT);
    423 }
    424 
    425 void CameraParameters::setPictureSize(int width, int height)
    426 {
    427     char str[32];
    428     sprintf(str, "%dx%d", width, height);
    429     set(KEY_PICTURE_SIZE, str);
    430 }
    431 
    432 void CameraParameters::getPictureSize(int *width, int *height) const
    433 {
    434     *width = *height = -1;
    435     // Get the current string, if it doesn't exist, leave the -1x-1
    436     const char *p = get(KEY_PICTURE_SIZE);
    437     if (p == 0) return;
    438     parse_pair(p, width, height, 'x');
    439 }
    440 
    441 void CameraParameters::getSupportedPictureSizes(Vector<Size> &sizes) const
    442 {
    443     const char *pictureSizesStr = get(KEY_SUPPORTED_PICTURE_SIZES);
    444     parseSizesList(pictureSizesStr, sizes);
    445 }
    446 
    447 void CameraParameters::setPictureFormat(const char *format)
    448 {
    449     set(KEY_PICTURE_FORMAT, format);
    450 }
    451 
    452 const char *CameraParameters::getPictureFormat() const
    453 {
    454     return get(KEY_PICTURE_FORMAT);
    455 }
    456 
    457 void CameraParameters::dump() const
    458 {
    459     ALOGD("dump: mMap.size = %d", mMap.size());
    460     for (size_t i = 0; i < mMap.size(); i++) {
    461         String8 k, v;
    462         k = mMap.keyAt(i);
    463         v = mMap.valueAt(i);
    464         ALOGD("%s: %s\n", k.string(), v.string());
    465     }
    466 }
    467 
    468 status_t CameraParameters::dump(int fd, const Vector<String16>& args) const
    469 {
    470     const size_t SIZE = 256;
    471     char buffer[SIZE];
    472     String8 result;
    473     snprintf(buffer, 255, "CameraParameters::dump: mMap.size = %d\n", mMap.size());
    474     result.append(buffer);
    475     for (size_t i = 0; i < mMap.size(); i++) {
    476         String8 k, v;
    477         k = mMap.keyAt(i);
    478         v = mMap.valueAt(i);
    479         snprintf(buffer, 255, "\t%s: %s\n", k.string(), v.string());
    480         result.append(buffer);
    481     }
    482     write(fd, result.string(), result.size());
    483     return NO_ERROR;
    484 }
    485 
    486 }; // namespace android
    487