CameraDeviceInfo.java revision de48004068f8c16f9a56c60b0ed2485a67687b4b
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 android.hardware.Camera; 20 21import com.android.ex.camera2.portability.debug.Log; 22 23/** 24 * The device info for all attached cameras. 25 */ 26public interface CameraDeviceInfo { 27 28 static final int NO_DEVICE = -1; 29 30 /** 31 * @param cameraId Which device to interrogate. 32 * @return The static characteristics of the specified device, or {@code null} on error. 33 */ 34 Characteristics getCharacteristics(int cameraId); 35 36 /** 37 * @return The total number of the available camera devices. 38 */ 39 int getNumberOfCameras(); 40 41 /** 42 * @return The first (lowest) ID of the back cameras or {@code NO_DEVICE} 43 * if not available. 44 */ 45 int getFirstBackCameraId(); 46 47 /** 48 * @return The first (lowest) ID of the front cameras or {@code NO_DEVICE} 49 * if not available. 50 */ 51 int getFirstFrontCameraId(); 52 53 /** 54 * Device characteristics for a single camera. 55 */ 56 public abstract class Characteristics { 57 private static final Log.Tag TAG = new Log.Tag("CamDvcInfChar"); 58 59 /** 60 * @return Whether the camera faces the back of the device. 61 */ 62 public abstract boolean isFacingBack(); 63 64 /** 65 * @return Whether the camera faces the device's screen. 66 */ 67 public abstract boolean isFacingFront(); 68 69 /** 70 * @return The camera image orientation, or the clockwise rotation angle 71 * that must be applied to display it in its natural orientation 72 * (in degrees, always a multiple of 90, and between [90,270]). 73 */ 74 public abstract int getSensorOrientation(); 75 76 /** 77 * @param currentDisplayOrientation 78 * The current display orientation, as measured clockwise from 79 * the device's natural orientation (in degrees, always a 80 * multiple of 90, and between 0 and 270, inclusive). 81 * @return 82 * The relative preview image orientation, or the clockwise 83 * rotation angle that must be applied to display preview 84 * frames in the matching orientation, accounting for implicit 85 * mirroring, if applicable (in degrees, always a multiple of 86 * 90, and between 0 and 270, inclusive). 87 */ 88 public int getPreviewOrientation(int currentDisplayOrientation) { 89 // Drivers tend to mirror the image during front camera preview. 90 return getRelativeImageOrientation(currentDisplayOrientation, true); 91 } 92 93 /** 94 * @param currentDisplayOrientation 95 * The current display orientation, as measured clockwise from 96 * the device's natural orientation (in degrees, always a 97 * multiple of 90, and between 0 and 270, inclusive). 98 * @return 99 * The relative capture image orientation, or the clockwise 100 * rotation angle that must be applied to display these frames 101 * in the matching orientation (in degrees, always a multiple 102 * of 90, and between 0 and 270, inclusive). 103 */ 104 public int getJpegOrientation(int currentDisplayOrientation) { 105 // Don't mirror during capture! 106 return getRelativeImageOrientation(currentDisplayOrientation, false); 107 } 108 109 /** 110 * @param currentDisplayOrientaiton 111 * {@link #getPreviewOrientation}, {@link #getJpegOrientation} 112 * @param compensateForMirroring 113 * Whether to account for mirroring in the case of front-facing 114 * cameras, which is necessary iff the OS/driver is 115 * automatically reflecting the image. 116 * @return 117 * {@link #getPreviewOrientation}, {@link #getJpegOrientation} 118 * 119 * @see android.hardware.Camera.setDisplayOrientation 120 */ 121 protected int getRelativeImageOrientation(int currentDisplayOrientation, 122 boolean compensateForMirroring) { 123 if (currentDisplayOrientation % 90 != 0) { 124 Log.e(TAG, "Provided display orientation is not divisible by 90"); 125 } 126 if (currentDisplayOrientation < 0 || currentDisplayOrientation > 270) { 127 Log.e(TAG, "Provided display orientation is outside expected range"); 128 } 129 130 int result = 0; 131 if (isFacingFront()) { 132 result = (getSensorOrientation() + currentDisplayOrientation) % 360; 133 if (compensateForMirroring) { 134 result = (360 - result) % 360; 135 } 136 } else if (isFacingBack()) { 137 result = (getSensorOrientation() - currentDisplayOrientation + 360) % 360; 138 } else { 139 Log.e(TAG, "Camera is facing unhandled direction"); 140 } 141 return result; 142 } 143 144 /** 145 * @return Whether the shutter sound can be disabled. 146 */ 147 public abstract boolean canDisableShutterSound(); 148 } 149} 150