AndroidCamera2Capabilities.java revision a97b7d1192e246a5f738991adca37cce282e1382
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        mMaxNumOfMeteringArea = p.get(CONTROL_MAX_REGIONS_AE);
86
87        // TODO: Populate mMaxZoomRatio
88        // TODO: Populate mHorizontalViewAngle
89        // TODO: Populate mVerticalViewAngle
90        // TODO: Populate mZoomRatioList
91        // TODO: Populate mMaxZoomIndex
92
93        if (supports(FocusMode.AUTO)) {
94            mMaxNumOfFocusAreas = p.get(CONTROL_MAX_REGIONS_AF);
95            if (mMaxNumOfFocusAreas > 0) {
96                mSupportedFeatures.add(Feature.FOCUS_AREA);
97            }
98        }
99        if (mMaxNumOfMeteringArea > 0) {
100            mSupportedFeatures.add(Feature.METERING_AREA);
101        }
102    }
103
104    public IntegralStringifier getIntegralStringifier() {
105        return mIntStringifier;
106    }
107
108    private void buildSceneModes(CameraCharacteristics p) {
109        for (int scene : p.get(CONTROL_AVAILABLE_SCENE_MODES)) {
110            SceneMode equiv = mIntStringifier.sceneModeFromInt(scene);
111            if (equiv != SceneMode.NO_SCENE_MODE) {
112                // equiv isn't a default generated because we couldn't handle this mode, so add it
113                mSupportedSceneModes.add(equiv);
114            }
115        }
116    }
117
118    private void buildFlashModes(CameraCharacteristics p) {
119        mSupportedFlashModes.add(FlashMode.OFF);
120        if (p.get(FLASH_INFO_AVAILABLE)) {
121            mSupportedFlashModes.add(FlashMode.ON);
122            mSupportedFlashModes.add(FlashMode.TORCH);
123            // TODO: New modes aren't represented here
124        }
125    }
126
127    private void buildFocusModes(CameraCharacteristics p) {
128        for (int focus : p.get(CONTROL_AF_AVAILABLE_MODES)) {
129            FocusMode equiv = mIntStringifier.focusModeFromInt(focus);
130            if (equiv != FocusMode.AUTO || focus == CONTROL_AF_MODE_AUTO) {
131                // equiv isn't a default generated because we couldn't handle this mode, so add it
132                mSupportedFocusModes.add(equiv);
133            }
134        }
135    }
136
137    private void buildWhiteBalances(CameraCharacteristics p) {
138        for (int bal : p.get(CONTROL_AWB_AVAILABLE_MODES)) {
139            WhiteBalance equiv = mIntStringifier.whiteBalanceFromInt(bal);
140            if (equiv != WhiteBalance.AUTO || bal == CONTROL_AWB_MODE_AUTO) {
141                // equiv isn't a default generated because we couldn't handle this mode, so add it
142                mSupportedWhiteBalances.add(equiv);
143            }
144        }
145    }
146
147    public static class IntegralStringifier extends Stringifier {
148        /**
149         * Converts the focus mode to API-related integer representation.
150         *
151         * @param fm The focus mode to convert.
152         * @return The corresponding {@code int} used by the camera framework
153         *         API, or {@link CONTROL_AF_MODE_AUTO} if that fails.
154         */
155        public int intify(FocusMode fm) {
156            switch (fm) {
157                case AUTO:
158                    return CONTROL_AF_MODE_AUTO;
159                case CONTINUOUS_PICTURE:
160                    return CONTROL_AF_MODE_CONTINUOUS_PICTURE;
161                case CONTINUOUS_VIDEO:
162                    return CONTROL_AF_MODE_CONTINUOUS_VIDEO;
163                case EXTENDED_DOF:
164                    return CONTROL_AF_MODE_EDOF;
165                case FIXED:
166                    return CONTROL_AF_MODE_OFF;
167                case MACRO:
168                    return CONTROL_AF_MODE_MACRO;
169                // TODO: New modes aren't represented here
170            }
171            return CONTROL_AF_MODE_AUTO;
172        }
173
174        /**
175         * Converts the API-related integer representation of the focus mode to
176         * the abstract representation.
177         *
178         * @param val The integral representation.
179         * @return The mode represented by the input integer, or the focus mode
180         *         with the lowest ordinal if it cannot be converted.
181         */
182        public FocusMode focusModeFromInt(int fm) {
183            switch (fm) {
184                case CONTROL_AF_MODE_AUTO:
185                    return FocusMode.AUTO;
186                case CONTROL_AF_MODE_CONTINUOUS_PICTURE:
187                    return FocusMode.CONTINUOUS_PICTURE;
188                case CONTROL_AF_MODE_CONTINUOUS_VIDEO:
189                    return FocusMode.CONTINUOUS_VIDEO;
190                case CONTROL_AF_MODE_EDOF:
191                    return FocusMode.EXTENDED_DOF;
192                case CONTROL_AF_MODE_OFF:
193                    return FocusMode.FIXED;
194                case CONTROL_AF_MODE_MACRO:
195                    return FocusMode.MACRO;
196                // TODO: New modes aren't represented here
197            }
198            return FocusMode.values()[0];
199        }
200
201        /**
202         * Converts the flash mode to API-related integer representation.
203         *
204         * @param fm The flash mode to convert.
205         * @return The corresponding {@code int} used by the camera framework
206         *         API, or {@link CONTROL_AF_MODE_AUTO} if that fails.
207         */
208        public int intify(FlashMode flm) {
209            switch (flm) {
210                case OFF:
211                    return FLASH_MODE_OFF;
212                case ON:
213                    return FLASH_MODE_SINGLE;
214                case TORCH:
215                    return FLASH_MODE_TORCH;
216                // TODO: New modes aren't represented here
217            }
218            return FLASH_MODE_OFF;
219        }
220
221        /**
222         * Converts the API-related integer representation of the flash mode to
223         * the abstract representation.
224         *
225         * @param flm The integral representation.
226         * @return The mode represented by the input integer, or the flash mode
227         *         with the lowest ordinal if it cannot be converted.
228         */
229        public FlashMode flashModeFromInt(int flm) {
230            switch (flm) {
231                case FLASH_MODE_OFF:
232                    return FlashMode.OFF;
233                case FLASH_MODE_SINGLE:
234                    return FlashMode.ON;
235                case FLASH_MODE_TORCH:
236                    return FlashMode.TORCH;
237                // TODO: New modes aren't represented here
238            }
239            return FlashMode.values()[0];
240        }
241
242        /**
243         * Converts the scene mode to API-related integer representation.
244         *
245         * @param fm The scene mode to convert.
246         * @return The corresponding {@code int} used by the camera framework
247         *         API, or {@link CONTROL_SCENE_MODE_DISABLED} if that fails.
248         */
249        public int intify(SceneMode sm) {
250            switch (sm) {
251                case AUTO:
252                    return CONTROL_SCENE_MODE_DISABLED;
253                case ACTION:
254                    return CONTROL_SCENE_MODE_ACTION;
255                case BARCODE:
256                    return CONTROL_SCENE_MODE_BARCODE;
257                case BEACH:
258                    return CONTROL_SCENE_MODE_BEACH;
259                case CANDLELIGHT:
260                    return CONTROL_SCENE_MODE_CANDLELIGHT;
261                case FIREWORKS:
262                    return CONTROL_SCENE_MODE_FIREWORKS;
263                case LANDSCAPE:
264                    return CONTROL_SCENE_MODE_LANDSCAPE;
265                case NIGHT:
266                    return CONTROL_SCENE_MODE_NIGHT;
267                case PARTY:
268                    return CONTROL_SCENE_MODE_PARTY;
269                case PORTRAIT:
270                    return CONTROL_SCENE_MODE_PORTRAIT;
271                case SNOW:
272                    return CONTROL_SCENE_MODE_SNOW;
273                case SPORTS:
274                    return CONTROL_SCENE_MODE_SPORTS;
275                case STEADYPHOTO:
276                    return CONTROL_SCENE_MODE_STEADYPHOTO;
277                case SUNSET:
278                    return CONTROL_SCENE_MODE_SUNSET;
279                case THEATRE:
280                    return CONTROL_SCENE_MODE_THEATRE;
281                // TODO: New modes aren't represented here
282            }
283            return CONTROL_SCENE_MODE_DISABLED;
284        }
285
286        /**
287         * Converts the API-related integer representation of the scene mode to
288         * the abstract representation.
289         *
290         * @param sm The integral representation.
291         * @return The mode represented by the input integer, or the scene mode
292         *         with the lowest ordinal if it cannot be converted.
293         */
294        public SceneMode sceneModeFromInt(int sm) {
295            switch (sm) {
296                case CONTROL_SCENE_MODE_DISABLED:
297                    return SceneMode.AUTO;
298                case CONTROL_SCENE_MODE_ACTION:
299                    return SceneMode.ACTION;
300                case CONTROL_SCENE_MODE_BARCODE:
301                    return SceneMode.BARCODE;
302                case CONTROL_SCENE_MODE_BEACH:
303                    return SceneMode.BEACH;
304                case CONTROL_SCENE_MODE_CANDLELIGHT:
305                    return SceneMode.CANDLELIGHT;
306                case CONTROL_SCENE_MODE_FIREWORKS:
307                    return SceneMode.FIREWORKS;
308                case CONTROL_SCENE_MODE_LANDSCAPE:
309                    return SceneMode.LANDSCAPE;
310                case CONTROL_SCENE_MODE_NIGHT:
311                    return SceneMode.NIGHT;
312                case CONTROL_SCENE_MODE_PARTY:
313                    return SceneMode.PARTY;
314                case CONTROL_SCENE_MODE_PORTRAIT:
315                    return SceneMode.PORTRAIT;
316                case CONTROL_SCENE_MODE_SNOW:
317                    return SceneMode.SNOW;
318                case CONTROL_SCENE_MODE_SPORTS:
319                    return SceneMode.SPORTS;
320                case CONTROL_SCENE_MODE_STEADYPHOTO:
321                    return SceneMode.STEADYPHOTO;
322                case CONTROL_SCENE_MODE_SUNSET:
323                    return SceneMode.SUNSET;
324                case CONTROL_SCENE_MODE_THEATRE:
325                    return SceneMode.THEATRE;
326                // TODO: New modes aren't represented here
327            }
328            return SceneMode.values()[0];
329        }
330
331        /**
332         * Converts the white balance to API-related integer representation.
333         *
334         * @param fm The white balance to convert.
335         * @return The corresponding {@code int} used by the camera framework
336         *         API, or {@link CONTROL_SCENE_MODE_DISABLED} if that fails.
337         */
338        public int intify(WhiteBalance wb) {
339            switch (wb) {
340                case AUTO:
341                    return CONTROL_AWB_MODE_AUTO;
342                case CLOUDY_DAYLIGHT:
343                    return CONTROL_AWB_MODE_CLOUDY_DAYLIGHT;
344                case DAYLIGHT:
345                    return CONTROL_AWB_MODE_DAYLIGHT;
346                case FLUORESCENT:
347                    return CONTROL_AWB_MODE_FLUORESCENT;
348                case INCANDESCENT:
349                    return CONTROL_AWB_MODE_INCANDESCENT;
350                case SHADE:
351                    return CONTROL_AWB_MODE_SHADE;
352                case TWILIGHT:
353                    return CONTROL_AWB_MODE_TWILIGHT;
354                case WARM_FLUORESCENT:
355                    return CONTROL_AWB_MODE_WARM_FLUORESCENT;
356                // TODO: New modes aren't represented here
357            }
358            return CONTROL_AWB_MODE_AUTO;
359        }
360
361        /**
362         * Converts the API-related integer representation of the white balance
363         * to the abstract representation.
364         *
365         * @param wb The integral representation.
366         * @return The balance represented by the input integer, or the white
367         *         balance with the lowest ordinal if it cannot be converted.
368         */
369        public WhiteBalance whiteBalanceFromInt(int wb) {
370            switch (wb) {
371                case CONTROL_AWB_MODE_AUTO:
372                    return WhiteBalance.AUTO;
373                case CONTROL_AWB_MODE_CLOUDY_DAYLIGHT:
374                    return WhiteBalance.CLOUDY_DAYLIGHT;
375                case CONTROL_AWB_MODE_DAYLIGHT:
376                    return WhiteBalance.DAYLIGHT;
377                case CONTROL_AWB_MODE_FLUORESCENT:
378                    return WhiteBalance.FLUORESCENT;
379                case CONTROL_AWB_MODE_INCANDESCENT:
380                    return WhiteBalance.INCANDESCENT;
381                case CONTROL_AWB_MODE_SHADE:
382                    return WhiteBalance.SHADE;
383                case CONTROL_AWB_MODE_TWILIGHT:
384                    return WhiteBalance.TWILIGHT;
385                case CONTROL_AWB_MODE_WARM_FLUORESCENT:
386                    return WhiteBalance.WARM_FLUORESCENT;
387                // TODO: New modes aren't represented here
388            }
389            return WhiteBalance.values()[0];
390        }
391    }
392}
393