AndroidCamera2Capabilities.java revision a0842b40441db5332a5290f941021636b1182761
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.ex.camera2.portability;
18
19import static android.hardware.camera2.CameraCharacteristics.*;
20
21import android.graphics.Point;
22import android.hardware.camera2.CameraCharacteristics;
23import android.hardware.camera2.params.StreamConfigurationMap;
24import android.media.ImageReader;
25import android.media.MediaRecorder;
26import android.util.Range;
27import android.util.Rational;
28import android.view.SurfaceHolder;
29
30import com.android.ex.camera2.portability.debug.Log;
31
32import java.util.ArrayList;
33import java.util.Arrays;
34
35/**
36 * The subclass of {@link CameraCapabilities} for Android Camera 2 API.
37 */
38public class AndroidCamera2Capabilities extends CameraCapabilities {
39    private static Log.Tag TAG = new Log.Tag("AndCam2Capabs");
40
41    private IntegralStringifier mIntStringifier;
42
43    AndroidCamera2Capabilities(CameraCharacteristics p) {
44        super(new IntegralStringifier());
45        mIntStringifier = (IntegralStringifier) getStringifier();
46
47        StreamConfigurationMap s = p.get(SCALER_STREAM_CONFIGURATION_MAP);
48
49        for (Range<Integer> fpsRange : p.get(CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES)) {
50            mSupportedPreviewFpsRange.add(new int[] { fpsRange.getLower(), fpsRange.getUpper() });
51        }
52
53        // TODO: We only support SurfaceView preview rendering
54        mSupportedPreviewSizes.addAll(Size.buildListFromAndroidSizes(Arrays.asList(
55                s.getOutputSizes(SurfaceHolder.class))));
56        for (int format : s.getOutputFormats()) {
57            mSupportedPreviewFormats.add(format);
58        }
59
60        // TODO: We only support MediaRecorder videos capture
61        mSupportedVideoSizes.addAll(Size.buildListFromAndroidSizes(Arrays.asList(
62                s.getOutputSizes(MediaRecorder.class))));
63
64        // TODO: We only support ImageReader image capture
65        mSupportedPhotoSizes.addAll(Size.buildListFromAndroidSizes(Arrays.asList(
66                s.getOutputSizes(ImageReader.class))));
67        mSupportedPhotoFormats.addAll(mSupportedPreviewFormats);
68
69        buildSceneModes(p);
70        buildFlashModes(p);
71        buildFocusModes(p);
72        buildWhiteBalances(p);
73        // TODO: Populate mSupportedFeatures
74
75        // TODO: Populate mPreferredPreviewSizeForVideo
76
77        Range<Integer> ecRange = p.get(CONTROL_AE_COMPENSATION_RANGE);
78        mMinExposureCompensation = ecRange.getLower();
79        mMaxExposureCompensation = ecRange.getUpper();
80
81        Rational ecStep = p.get(CONTROL_AE_COMPENSATION_STEP);
82        mExposureCompensationStep = (float) ecStep.getNumerator() / ecStep.getDenominator();
83
84        mMaxNumOfFacesSupported = p.get(STATISTICS_INFO_MAX_FACE_COUNT);
85        mMaxNumOfFocusAreas = p.get(CONTROL_MAX_REGIONS_AF);
86        mMaxNumOfMeteringArea = p.get(CONTROL_MAX_REGIONS_AE);
87
88        // TODO: Populate mMaxZoomRatio
89        // TODO: Populate mHorizontalViewAngle
90        // TODO: Populate mVerticalViewAngle
91        // TODO: Populate mZoomRatioList
92        // TODO: Populate mMaxZoomIndex
93    }
94
95    public IntegralStringifier getIntegralStringifier() {
96        return mIntStringifier;
97    }
98
99    private void buildSceneModes(CameraCharacteristics p) {
100        for (int scene : p.get(CONTROL_AVAILABLE_SCENE_MODES)) {
101            SceneMode equiv = mIntStringifier.sceneModeFromInt(scene);
102            if (equiv != SceneMode.NO_SCENE_MODE) {
103                // equiv isn't a default generated because we couldn't handle this mode, so add it
104                mSupportedSceneModes.add(equiv);
105            }
106        }
107    }
108
109    private void buildFlashModes(CameraCharacteristics p) {
110        mSupportedFlashModes.add(FlashMode.OFF);
111        if (p.get(FLASH_INFO_AVAILABLE)) {
112            mSupportedFlashModes.add(FlashMode.ON);
113            mSupportedFlashModes.add(FlashMode.TORCH);
114            // TODO: New modes aren't represented here
115        }
116    }
117
118    private void buildFocusModes(CameraCharacteristics p) {
119        for (int focus : p.get(CONTROL_AF_AVAILABLE_MODES)) {
120            FocusMode equiv = mIntStringifier.focusModeFromInt(focus);
121            if (equiv != FocusMode.AUTO || focus == CONTROL_AF_MODE_AUTO) {
122                // equiv isn't a default generated because we couldn't handle this mode, so add it
123                mSupportedFocusModes.add(equiv);
124            }
125        }
126    }
127
128    private void buildWhiteBalances(CameraCharacteristics p) {
129        for (int bal : p.get(CONTROL_AWB_AVAILABLE_MODES)) {
130            WhiteBalance equiv = mIntStringifier.whiteBalanceFromInt(bal);
131            if (equiv != WhiteBalance.AUTO || bal == CONTROL_AWB_MODE_AUTO) {
132                // equiv isn't a default generated because we couldn't handle this mode, so add it
133                mSupportedWhiteBalances.add(equiv);
134            }
135        }
136    }
137
138    public static class IntegralStringifier extends Stringifier {
139        /**
140         * Converts the focus mode to API-related integer representation.
141         *
142         * @param fm The focus mode to convert.
143         * @return The corresponding {@code int} used by the camera framework
144         *         API, or {@link CONTROL_AF_MODE_AUTO} if that fails.
145         */
146        public int intify(FocusMode fm) {
147            switch (fm) {
148                case AUTO:
149                    return CONTROL_AF_MODE_AUTO;
150                case CONTINUOUS_PICTURE:
151                    return CONTROL_AF_MODE_CONTINUOUS_PICTURE;
152                case CONTINUOUS_VIDEO:
153                    return CONTROL_AF_MODE_CONTINUOUS_VIDEO;
154                case EXTENDED_DOF:
155                    return CONTROL_AF_MODE_EDOF;
156                case FIXED:
157                    return CONTROL_AF_MODE_OFF;
158                case MACRO:
159                    return CONTROL_AF_MODE_MACRO;
160                // TODO: New modes aren't represented here
161            }
162            return CONTROL_AF_MODE_AUTO;
163        }
164
165        /**
166         * Converts the API-related integer representation of the focus mode to
167         * the abstract representation.
168         *
169         * @param val The integral representation.
170         * @return The mode represented by the input integer, or the focus mode
171         *         with the lowest ordinal if it cannot be converted.
172         */
173        public FocusMode focusModeFromInt(int fm) {
174            switch (fm) {
175                case CONTROL_AF_MODE_AUTO:
176                    return FocusMode.AUTO;
177                case CONTROL_AF_MODE_CONTINUOUS_PICTURE:
178                    return FocusMode.CONTINUOUS_PICTURE;
179                case CONTROL_AF_MODE_CONTINUOUS_VIDEO:
180                    return FocusMode.CONTINUOUS_VIDEO;
181                case CONTROL_AF_MODE_EDOF:
182                    return FocusMode.EXTENDED_DOF;
183                case CONTROL_AF_MODE_OFF:
184                    return FocusMode.FIXED;
185                case CONTROL_AF_MODE_MACRO:
186                    return FocusMode.MACRO;
187                // TODO: New modes aren't represented here
188            }
189            return FocusMode.values()[0];
190        }
191
192        /**
193         * Converts the flash mode to API-related integer representation.
194         *
195         * @param fm The flash mode to convert.
196         * @return The corresponding {@code int} used by the camera framework
197         *         API, or {@link CONTROL_AF_MODE_AUTO} if that fails.
198         */
199        public int intify(FlashMode flm) {
200            switch (flm) {
201                case OFF:
202                    return FLASH_MODE_OFF;
203                case ON:
204                    return FLASH_MODE_SINGLE;
205                case TORCH:
206                    return FLASH_MODE_TORCH;
207                // TODO: New modes aren't represented here
208            }
209            return FLASH_MODE_OFF;
210        }
211
212        /**
213         * Converts the API-related integer representation of the flash mode to
214         * the abstract representation.
215         *
216         * @param flm The integral representation.
217         * @return The mode represented by the input integer, or the flash mode
218         *         with the lowest ordinal if it cannot be converted.
219         */
220        public FlashMode flashModeFromInt(int flm) {
221            switch (flm) {
222                case FLASH_MODE_OFF:
223                    return FlashMode.OFF;
224                case FLASH_MODE_SINGLE:
225                    return FlashMode.ON;
226                case FLASH_MODE_TORCH:
227                    return FlashMode.TORCH;
228                // TODO: New modes aren't represented here
229            }
230            return FlashMode.values()[0];
231        }
232
233        /**
234         * Converts the scene mode to API-related integer representation.
235         *
236         * @param fm The scene mode to convert.
237         * @return The corresponding {@code int} used by the camera framework
238         *         API, or {@link CONTROL_SCENE_MODE_DISABLED} if that fails.
239         */
240        public int intify(SceneMode sm) {
241            switch (sm) {
242                case AUTO:
243                    return CONTROL_SCENE_MODE_DISABLED;
244                case ACTION:
245                    return CONTROL_SCENE_MODE_ACTION;
246                case BARCODE:
247                    return CONTROL_SCENE_MODE_BARCODE;
248                case BEACH:
249                    return CONTROL_SCENE_MODE_BEACH;
250                case CANDLELIGHT:
251                    return CONTROL_SCENE_MODE_CANDLELIGHT;
252                case FIREWORKS:
253                    return CONTROL_SCENE_MODE_FIREWORKS;
254                case LANDSCAPE:
255                    return CONTROL_SCENE_MODE_LANDSCAPE;
256                case NIGHT:
257                    return CONTROL_SCENE_MODE_NIGHT;
258                case PARTY:
259                    return CONTROL_SCENE_MODE_PARTY;
260                case PORTRAIT:
261                    return CONTROL_SCENE_MODE_PORTRAIT;
262                case SNOW:
263                    return CONTROL_SCENE_MODE_SNOW;
264                case SPORTS:
265                    return CONTROL_SCENE_MODE_SPORTS;
266                case STEADYPHOTO:
267                    return CONTROL_SCENE_MODE_STEADYPHOTO;
268                case SUNSET:
269                    return CONTROL_SCENE_MODE_SUNSET;
270                case THEATRE:
271                    return CONTROL_SCENE_MODE_THEATRE;
272                // TODO: New modes aren't represented here
273            }
274            return CONTROL_SCENE_MODE_DISABLED;
275        }
276
277        /**
278         * Converts the API-related integer representation of the scene mode to
279         * the abstract representation.
280         *
281         * @param sm The integral representation.
282         * @return The mode represented by the input integer, or the scene mode
283         *         with the lowest ordinal if it cannot be converted.
284         */
285        public SceneMode sceneModeFromInt(int sm) {
286            switch (sm) {
287                case CONTROL_SCENE_MODE_DISABLED:
288                    return SceneMode.AUTO;
289                case CONTROL_SCENE_MODE_ACTION:
290                    return SceneMode.ACTION;
291                case CONTROL_SCENE_MODE_BARCODE:
292                    return SceneMode.BARCODE;
293                case CONTROL_SCENE_MODE_BEACH:
294                    return SceneMode.BEACH;
295                case CONTROL_SCENE_MODE_CANDLELIGHT:
296                    return SceneMode.CANDLELIGHT;
297                case CONTROL_SCENE_MODE_FIREWORKS:
298                    return SceneMode.FIREWORKS;
299                case CONTROL_SCENE_MODE_LANDSCAPE:
300                    return SceneMode.LANDSCAPE;
301                case CONTROL_SCENE_MODE_NIGHT:
302                    return SceneMode.NIGHT;
303                case CONTROL_SCENE_MODE_PARTY:
304                    return SceneMode.PARTY;
305                case CONTROL_SCENE_MODE_PORTRAIT:
306                    return SceneMode.PORTRAIT;
307                case CONTROL_SCENE_MODE_SNOW:
308                    return SceneMode.SNOW;
309                case CONTROL_SCENE_MODE_SPORTS:
310                    return SceneMode.SPORTS;
311                case CONTROL_SCENE_MODE_STEADYPHOTO:
312                    return SceneMode.STEADYPHOTO;
313                case CONTROL_SCENE_MODE_SUNSET:
314                    return SceneMode.SUNSET;
315                case CONTROL_SCENE_MODE_THEATRE:
316                    return SceneMode.THEATRE;
317                // TODO: New modes aren't represented here
318            }
319            return SceneMode.values()[0];
320        }
321
322        /**
323         * Converts the white balance to API-related integer representation.
324         *
325         * @param fm The white balance to convert.
326         * @return The corresponding {@code int} used by the camera framework
327         *         API, or {@link CONTROL_SCENE_MODE_DISABLED} if that fails.
328         */
329        public int intify(WhiteBalance wb) {
330            switch (wb) {
331                case AUTO:
332                    return CONTROL_AWB_MODE_AUTO;
333                case CLOUDY_DAYLIGHT:
334                    return CONTROL_AWB_MODE_CLOUDY_DAYLIGHT;
335                case DAYLIGHT:
336                    return CONTROL_AWB_MODE_DAYLIGHT;
337                case FLUORESCENT:
338                    return CONTROL_AWB_MODE_FLUORESCENT;
339                case INCANDESCENT:
340                    return CONTROL_AWB_MODE_INCANDESCENT;
341                case SHADE:
342                    return CONTROL_AWB_MODE_SHADE;
343                case TWILIGHT:
344                    return CONTROL_AWB_MODE_TWILIGHT;
345                case WARM_FLUORESCENT:
346                    return CONTROL_AWB_MODE_WARM_FLUORESCENT;
347                // TODO: New modes aren't represented here
348            }
349            return CONTROL_AWB_MODE_AUTO;
350        }
351
352        /**
353         * Converts the API-related integer representation of the white balance
354         * to the abstract representation.
355         *
356         * @param wb The integral representation.
357         * @return The balance represented by the input integer, or the white
358         *         balance with the lowest ordinal if it cannot be converted.
359         */
360        public WhiteBalance whiteBalanceFromInt(int wb) {
361            switch (wb) {
362                case CONTROL_AWB_MODE_AUTO:
363                    return WhiteBalance.AUTO;
364                case CONTROL_AWB_MODE_CLOUDY_DAYLIGHT:
365                    return WhiteBalance.CLOUDY_DAYLIGHT;
366                case CONTROL_AWB_MODE_DAYLIGHT:
367                    return WhiteBalance.DAYLIGHT;
368                case CONTROL_AWB_MODE_FLUORESCENT:
369                    return WhiteBalance.FLUORESCENT;
370                case CONTROL_AWB_MODE_INCANDESCENT:
371                    return WhiteBalance.INCANDESCENT;
372                case CONTROL_AWB_MODE_SHADE:
373                    return WhiteBalance.SHADE;
374                case CONTROL_AWB_MODE_TWILIGHT:
375                    return WhiteBalance.TWILIGHT;
376                case CONTROL_AWB_MODE_WARM_FLUORESCENT:
377                    return WhiteBalance.WARM_FLUORESCENT;
378                // TODO: New modes aren't represented here
379            }
380            return WhiteBalance.values()[0];
381        }
382    }
383}
384