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