summaryrefslogtreecommitdiff
path: root/src/com/android/devcamera/CameraInfoCache.java
blob: 7b97a4e4017e8fbe03564e71116c1cb3df745371 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.devcamera;

import android.graphics.ImageFormat;
import android.graphics.Rect;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CameraMetadata;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.os.Build;
import android.util.Log;
import android.util.Size;
import android.util.SizeF;

/**
 * Caches (static) information about the first/main camera.
 * Convenience functions represent data from CameraCharacteristics.
 */

public class CameraInfoCache {
    private static final String TAG = "DevCamera_CAMINFO";

    public static final boolean IS_NEXUS_6 = "shamu".equalsIgnoreCase(Build.DEVICE);

    public int[] noiseModes;
    public int[] edgeModes;

    private CameraCharacteristics mCameraCharacteristics;
    private String mCameraId;
    private Size mLargestYuvSize;
    private Size mLargestJpegSize;
    private Size mRawSize;
    private Rect mActiveArea;
    private Integer mSensorOrientation;
    private Integer mRawFormat;
    private int mBestFaceMode;
    private int mHardwareLevel;
    private Size mDepthCloudSize = null;

    /**
     * Constructor.
     */
    public CameraInfoCache(CameraManager cameraMgr, boolean useFrontCamera) {
        String[] cameralist;
        try {
            cameralist = cameraMgr.getCameraIdList();
            for (String id : cameralist) {
                mCameraCharacteristics = cameraMgr.getCameraCharacteristics(id);
                Integer facing = mCameraCharacteristics.get(CameraCharacteristics.LENS_FACING);
                if (facing == (useFrontCamera ? CameraMetadata.LENS_FACING_FRONT : CameraMetadata.LENS_FACING_BACK)) {
                    mCameraId = id;
                    break;
                }
            }
        } catch (Exception e) {
            Log.e(TAG, "ERROR: Could not get camera ID list / no camera information is available: " + e);
            return;
        }
        // Should have mCameraId as this point.
        if (mCameraId == null) {
            Log.e(TAG, "ERROR: Could not find a suitable rear or front camera.");
            return;
        }

        // Store YUV_420_888, JPEG, Raw info
        StreamConfigurationMap map = mCameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
        int[] formats = map.getOutputFormats();
        long lowestStall = Long.MAX_VALUE;
        for (int i = 0; i < formats.length; i++) {
            if (formats[i] == ImageFormat.YUV_420_888) {
                mLargestYuvSize = returnLargestSize(map.getOutputSizes(formats[i]));
            }
            if (formats[i] == ImageFormat.JPEG) {
                mLargestJpegSize = returnLargestSize(map.getOutputSizes(formats[i]));
            }
            if (formats[i] == ImageFormat.RAW10 || formats[i] == ImageFormat.RAW_SENSOR) { // TODO: Add RAW12
                Size size = returnLargestSize(map.getOutputSizes(formats[i]));
                long stall = map.getOutputStallDuration(formats[i], size);
                if (stall < lowestStall) {
                    mRawFormat = formats[i];
                    mRawSize = size;
                    lowestStall = stall;
                }
            }
            if (formats[i] == ImageFormat.DEPTH_POINT_CLOUD) {
                Size size = returnLargestSize(map.getOutputSizes(formats[i]));
                mDepthCloudSize = size;
            }
        }

        mActiveArea = mCameraCharacteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);

        // Compute best face mode.
        int[] faceModes = mCameraCharacteristics.get(CameraCharacteristics.STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES);
        for (int i=0; i<faceModes.length; i++) {
            if (faceModes[i] > mBestFaceMode) {
                mBestFaceMode = faceModes[i];
            }
        }
        edgeModes = mCameraCharacteristics.get(CameraCharacteristics.EDGE_AVAILABLE_EDGE_MODES);
        noiseModes = mCameraCharacteristics.get(CameraCharacteristics.NOISE_REDUCTION_AVAILABLE_NOISE_REDUCTION_MODES);

        // Misc stuff.
        mHardwareLevel = mCameraCharacteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL);

        mSensorOrientation = mCameraCharacteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
    }

    boolean supportedModesContains(int[] modes, int mode) {
        for (int m : modes) {
            if (m == mode) return true;
        }
        return false;
    }

    public int sensorOrientation() {
        return mSensorOrientation;
    }

    public boolean isCamera2FullModeAvailable() {
        return isHardwareLevelAtLeast(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_FULL);
    }

    public boolean isHardwareLevelAtLeast(int level) {
        // Special-case LEGACY since it has numerical value 2
        if (level == CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY) {
            // All devices are at least LEGACY
            return true;
        }
        if (mHardwareLevel == CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY) {
            // Since level isn't LEGACY
            return false;
        }
        // All other levels can be compared numerically
        return mHardwareLevel >= level;
    }

    public boolean isCapabilitySupported(int capability) {
        int[] caps = mCameraCharacteristics.get(CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES);
        for (int c: caps) {
            if (c == capability) return true;
        }
        return false;
    }

    public float getDiopterLow() {
        return 0f; // Infinity
    }

    public float getDiopterHi() {
        Float minFocusDistance =
                mCameraCharacteristics.get(CameraCharacteristics.LENS_INFO_MINIMUM_FOCUS_DISTANCE);
        // LEGACY devices don't report this, but they won't report focus distance anyway, so just
        // default to zero
        return (minFocusDistance == null) ? 0.0f : minFocusDistance;
    }

    /**
     * Calculate camera device horizontal and vertical fields of view.
     *
     * @return horizontal and vertical field of view, in degrees.
     */
    public float[] getFieldOfView() {
        float[] availableFocalLengths =
                mCameraCharacteristics.get(CameraCharacteristics.LENS_INFO_AVAILABLE_FOCAL_LENGTHS);
        float focalLength = 4.5f; // mm, default from Nexus 6P
        if (availableFocalLengths == null || availableFocalLengths.length == 0) {
            Log.e(TAG, "No focal length reported by camera device, assuming default " +
                    focalLength);
        } else {
            focalLength = availableFocalLengths[0];
        }
        SizeF physicalSize =
                mCameraCharacteristics.get(CameraCharacteristics.SENSOR_INFO_PHYSICAL_SIZE);
        if (physicalSize == null) {
            physicalSize = new SizeF(6.32f, 4.69f); // mm, default from Nexus 6P
            Log.e(TAG, "No physical sensor dimensions reported by camera device, assuming default "
                    + physicalSize);
        }

        // Only active array is actually visible, so calculate fraction of physicalSize that it takes up
        Size pixelArraySize = mCameraCharacteristics.get(CameraCharacteristics.SENSOR_INFO_PIXEL_ARRAY_SIZE);
        Rect activeArraySize = mCameraCharacteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);

        float activeWidthFraction = activeArraySize.width() / (float) pixelArraySize.getWidth();
        float activeHeightFraction = activeArraySize.height() / (float) pixelArraySize.getHeight();

        // Simple rectilinear lens field of view formula:
        //   angle of view = 2 * arctan ( active size / (2 * focal length) )
        float[] fieldOfView = new float[2];
        fieldOfView[0] = (float) Math.toDegrees(
                2 * Math.atan(physicalSize.getWidth() * activeWidthFraction / 2 / focalLength));
        fieldOfView[1] = (float) Math.toDegrees(
                2 * Math.atan(physicalSize.getHeight() * activeHeightFraction / 2 / focalLength));

        return fieldOfView;
    }
    /**
     * Private utility function.
     */
    private Size returnLargestSize(Size[] sizes) {
        Size largestSize = null;
        int area = 0;
        for (int j = 0; j < sizes.length; j++) {
            if (sizes[j].getHeight() * sizes[j].getWidth() > area) {
                area = sizes[j].getHeight() * sizes[j].getWidth();
                largestSize = sizes[j];
            }
        }
        return largestSize;
    }

    public int bestFaceDetectionMode() {
        return mBestFaceMode;
    }

    public int faceOffsetX() {
        return (mActiveArea.width() - mLargestYuvSize.getWidth()) / 2;
    }

    public int faceOffsetY() {
        return (mActiveArea.height() - mLargestYuvSize.getHeight()) / 2;
    }

    public int activeAreaWidth() {
        return mActiveArea.width();
    }

    public int activeAreaHeight() {
        return mActiveArea.height();
    }

    public Rect getActiveAreaRect() {
        return mActiveArea;
    }

    public String getCameraId() {
        return mCameraId;
    }

    public Size getPreviewSize() {
        float aspect = mLargestYuvSize.getWidth() / mLargestYuvSize.getHeight();
        aspect = aspect > 1f ? aspect : 1f / aspect;
        if (aspect > 1.6) {
            return new Size(1920, 1080); // TODO: Check available resolutions.
        }
        if (isHardwareLevelAtLeast(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_3)) {
            // Bigger preview size for more advanced devices
            return new Size(1440, 1080);
        }
        return new Size(1280, 960); // TODO: Check available resolutions.
    }

    public Size getJpegStreamSize() {
        return mLargestJpegSize;
    }

    public Size getYuvStream1Size() {
        return mLargestYuvSize;
    }

    public Size getYuvStream2Size() {
        return new Size(320, 240);
    }

    public boolean rawAvailable() {
        return mRawSize != null;
    }
    public boolean isYuvReprocessingAvailable() {
        return isCapabilitySupported(
                CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES_YUV_REPROCESSING);
    }

    public Integer getRawFormat() {
        return mRawFormat;
    }

    public Size getRawStreamSize() {
        return mRawSize;
    }

    public Size getDepthCloudSize() {
        return mDepthCloudSize;
    }
}