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 | ** |
| 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 <ui/CameraParameters.h> |
| 24 | |
| 25 | namespace android { |
| 26 | |
| 27 | static const char* portrait = "portrait"; |
| 28 | static const char* landscape = "landscape"; |
| 29 | |
| 30 | CameraParameters::CameraParameters() |
| 31 | : mMap() |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | CameraParameters::~CameraParameters() |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | String8 CameraParameters::flatten() const |
| 40 | { |
| 41 | String8 flattened(""); |
| 42 | size_t size = mMap.size(); |
| 43 | |
| 44 | for (size_t i = 0; i < size; i++) { |
| 45 | String8 k, v; |
| 46 | k = mMap.keyAt(i); |
| 47 | v = mMap.valueAt(i); |
| 48 | |
| 49 | flattened += k; |
| 50 | flattened += "="; |
| 51 | flattened += v; |
| 52 | if (i != size-1) |
| 53 | flattened += ";"; |
| 54 | } |
| 55 | |
| 56 | return flattened; |
| 57 | } |
| 58 | |
| 59 | void CameraParameters::unflatten(const String8 ¶ms) |
| 60 | { |
| 61 | const char *a = params.string(); |
| 62 | const char *b; |
| 63 | |
| 64 | mMap.clear(); |
| 65 | |
| 66 | for (;;) { |
| 67 | // Find the bounds of the key name. |
| 68 | b = strchr(a, '='); |
| 69 | if (b == 0) |
| 70 | break; |
| 71 | |
| 72 | // Create the key string. |
| 73 | String8 k(a, (size_t)(b-a)); |
| 74 | |
| 75 | // Find the value. |
| 76 | a = b+1; |
| 77 | b = strchr(a, ';'); |
| 78 | if (b == 0) { |
| 79 | // If there's no semicolon, this is the last item. |
| 80 | String8 v(a); |
| 81 | mMap.add(k, v); |
| 82 | break; |
| 83 | } |
| 84 | |
| 85 | String8 v(a, (size_t)(b-a)); |
| 86 | mMap.add(k, v); |
| 87 | a = b+1; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | |
| 92 | void CameraParameters::set(const char *key, const char *value) |
| 93 | { |
| 94 | // XXX i think i can do this with strspn() |
| 95 | if (strchr(key, '=') || strchr(key, ';')) { |
| 96 | //XXX LOGE("Key \"%s\"contains invalid character (= or ;)", key); |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | if (strchr(value, '=') || strchr(key, ';')) { |
| 101 | //XXX LOGE("Value \"%s\"contains invalid character (= or ;)", value); |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | mMap.replaceValueFor(String8(key), String8(value)); |
| 106 | } |
| 107 | |
| 108 | void CameraParameters::set(const char *key, int value) |
| 109 | { |
| 110 | char str[16]; |
| 111 | sprintf(str, "%d", value); |
| 112 | set(key, str); |
| 113 | } |
| 114 | |
| 115 | const char *CameraParameters::get(const char *key) const |
| 116 | { |
| 117 | String8 v = mMap.valueFor(String8(key)); |
| 118 | if (v.length() == 0) |
| 119 | return 0; |
| 120 | return v.string(); |
| 121 | } |
| 122 | |
| 123 | int CameraParameters::getInt(const char *key) const |
| 124 | { |
| 125 | const char *v = get(key); |
| 126 | if (v == 0) |
| 127 | return -1; |
| 128 | return strtol(v, 0, 0); |
| 129 | } |
| 130 | |
| 131 | static int parse_size(const char *str, int &width, int &height) |
| 132 | { |
| 133 | // Find the width. |
| 134 | char *end; |
| 135 | int w = (int)strtol(str, &end, 10); |
| 136 | // If an 'x' does not immediately follow, give up. |
| 137 | if (*end != 'x') |
| 138 | return -1; |
| 139 | |
| 140 | // Find the height, immediately after the 'x'. |
| 141 | int h = (int)strtol(end+1, 0, 10); |
| 142 | |
| 143 | width = w; |
| 144 | height = h; |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | void CameraParameters::setPreviewSize(int width, int height) |
| 150 | { |
| 151 | char str[32]; |
| 152 | sprintf(str, "%dx%d", width, height); |
| 153 | set("preview-size", str); |
| 154 | } |
| 155 | |
| 156 | void CameraParameters::getPreviewSize(int *width, int *height) const |
| 157 | { |
| 158 | *width = -1; |
| 159 | *height = -1; |
| 160 | |
| 161 | // Get the current string, if it doesn't exist, leave the -1x-1 |
| 162 | const char *p = get("preview-size"); |
| 163 | if (p == 0) |
| 164 | return; |
| 165 | |
| 166 | int w, h; |
| 167 | if (parse_size(p, w, h) == 0) { |
| 168 | *width = w; |
| 169 | *height = h; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | void CameraParameters::setPreviewFrameRate(int fps) |
| 174 | { |
| 175 | set("preview-frame-rate", fps); |
| 176 | } |
| 177 | |
| 178 | int CameraParameters::getPreviewFrameRate() const |
| 179 | { |
| 180 | return getInt("preview-frame-rate"); |
| 181 | } |
| 182 | |
| 183 | void CameraParameters::setPreviewFormat(const char *format) |
| 184 | { |
| 185 | set("preview-format", format); |
| 186 | } |
| 187 | |
| 188 | int CameraParameters::getOrientation() const |
| 189 | { |
| 190 | const char* orientation = get("orientation"); |
| 191 | if (orientation && !strcmp(orientation, portrait)) |
| 192 | return CAMERA_ORIENTATION_PORTRAIT; |
| 193 | return CAMERA_ORIENTATION_LANDSCAPE; |
| 194 | } |
| 195 | |
| 196 | void CameraParameters::setOrientation(int orientation) |
| 197 | { |
| 198 | if (orientation == CAMERA_ORIENTATION_PORTRAIT) { |
| 199 | set("preview-format", portrait); |
| 200 | } else { |
| 201 | set("preview-format", landscape); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | const char *CameraParameters::getPreviewFormat() const |
| 206 | { |
| 207 | return get("preview-format"); |
| 208 | } |
| 209 | |
| 210 | void CameraParameters::setPictureSize(int width, int height) |
| 211 | { |
| 212 | char str[32]; |
| 213 | sprintf(str, "%dx%d", width, height); |
| 214 | set("picture-size", str); |
| 215 | } |
| 216 | |
| 217 | void CameraParameters::getPictureSize(int *width, int *height) const |
| 218 | { |
| 219 | *width = -1; |
| 220 | *height = -1; |
| 221 | |
| 222 | // Get the current string, if it doesn't exist, leave the -1x-1 |
| 223 | const char *p = get("picture-size"); |
| 224 | if (p == 0) |
| 225 | return; |
| 226 | |
| 227 | int w, h; |
| 228 | if (parse_size(p, w, h) == 0) { |
| 229 | *width = w; |
| 230 | *height = h; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | void CameraParameters::setPictureFormat(const char *format) |
| 235 | { |
| 236 | set("picture-format", format); |
| 237 | } |
| 238 | |
| 239 | const char *CameraParameters::getPictureFormat() const |
| 240 | { |
| 241 | return get("picture-format"); |
| 242 | } |
| 243 | |
| 244 | void CameraParameters::dump() const |
| 245 | { |
| 246 | LOGD("dump: mMap.size = %d", mMap.size()); |
| 247 | for (size_t i = 0; i < mMap.size(); i++) { |
| 248 | String8 k, v; |
| 249 | k = mMap.keyAt(i); |
| 250 | v = mMap.valueAt(i); |
| 251 | LOGD("%s: %s\n", k.string(), v.string()); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | status_t CameraParameters::dump(int fd, const Vector<String16>& args) const |
| 256 | { |
| 257 | const size_t SIZE = 256; |
| 258 | char buffer[SIZE]; |
| 259 | String8 result; |
| 260 | snprintf(buffer, 255, "CameraParameters::dump: mMap.size = %d\n", mMap.size()); |
| 261 | result.append(buffer); |
| 262 | for (size_t i = 0; i < mMap.size(); i++) { |
| 263 | String8 k, v; |
| 264 | k = mMap.keyAt(i); |
| 265 | v = mMap.valueAt(i); |
| 266 | snprintf(buffer, 255, "\t%s: %s\n", k.string(), v.string()); |
| 267 | result.append(buffer); |
| 268 | } |
| 269 | write(fd, result.string(), result.size()); |
| 270 | return NO_ERROR; |
| 271 | } |
| 272 | |
| 273 | }; // namespace android |