| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package android.hardware.camera2.params; |
| 17 |
|
| 18 |
import android.annotation.NonNull; |
| 19 |
import android.annotation.Nullable; |
| 20 |
|
| 21 |
import android.graphics.ImageFormat; |
| 22 |
import android.graphics.ImageFormat.Format; |
| 23 |
import android.graphics.PixelFormat; |
| 24 |
import android.hardware.camera2.params.MultiResolutionStreamInfo; |
| 25 |
import android.hardware.camera2.params.StreamConfigurationMap; |
| 26 |
import android.hardware.camera2.utils.HashCodeHelpers; |
| 27 |
|
| 28 |
import android.util.Size; |
| 29 |
|
| 30 |
import java.util.ArrayList; |
| 31 |
import java.util.Arrays; |
| 32 |
import java.util.Collection; |
| 33 |
import java.util.Collections; |
| 34 |
import java.util.Comparator; |
| 35 |
import java.util.HashMap; |
| 36 |
import java.util.Map; |
| 37 |
import java.util.List; |
| 38 |
import java.util.Set; |
| 39 |
|
| 40 |
import static com.android.internal.util.Preconditions.*; |
| 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 |
public final class MultiResolutionStreamConfigurationMap { |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public MultiResolutionStreamConfigurationMap( |
| 75 |
@NonNull Map<String, StreamConfiguration[]> configurations) { |
| 76 |
checkNotNull(configurations, "multi-resolution configurations must not be null"); |
| 77 |
if (configurations.size() == 0) { |
| 78 |
throw new IllegalArgumentException("multi-resolution configurations must not be empty"); |
| 79 |
} |
| 80 |
|
| 81 |
mConfigurations = configurations; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
for (Map.Entry<String, StreamConfiguration[]> entry : |
| 86 |
mConfigurations.entrySet()) { |
| 87 |
String cameraId = entry.getKey(); |
| 88 |
StreamConfiguration[] configs = entry.getValue(); |
| 89 |
|
| 90 |
for (int i = 0; i < configs.length; i++) { |
| 91 |
StreamConfiguration config = configs[i]; |
| 92 |
int format = config.getFormat(); |
| 93 |
|
| 94 |
MultiResolutionStreamInfo multiResolutionStreamInfo = new MultiResolutionStreamInfo( |
| 95 |
config.getWidth(), config.getHeight(), cameraId); |
| 96 |
Map<Integer, List<MultiResolutionStreamInfo>> destMap; |
| 97 |
if (config.isInput()) { |
| 98 |
destMap = mMultiResolutionInputConfigs; |
| 99 |
} else { |
| 100 |
destMap = mMultiResolutionOutputConfigs; |
| 101 |
} |
| 102 |
|
| 103 |
if (!destMap.containsKey(format)) { |
| 104 |
List<MultiResolutionStreamInfo> multiResolutionStreamInfoList = |
| 105 |
new ArrayList<MultiResolutionStreamInfo>(); |
| 106 |
destMap.put(format, multiResolutionStreamInfoList); |
| 107 |
} |
| 108 |
destMap.get(format).add(multiResolutionStreamInfo); |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public static class SizeComparator implements Comparator<MultiResolutionStreamInfo> { |
| 121 |
@Override |
| 122 |
public int compare(@NonNull MultiResolutionStreamInfo lhs, |
| 123 |
@NonNull MultiResolutionStreamInfo rhs) { |
| 124 |
return StreamConfigurationMap.compareSizes( |
| 125 |
lhs.getWidth(), lhs.getHeight(), rhs.getWidth(), rhs.getHeight()); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public @NonNull @Format int[] getOutputFormats() { |
| 149 |
return getPublicImageFormats(true); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public @NonNull @Format int[] getInputFormats() { |
| 173 |
return getPublicImageFormats(false); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
private int[] getPublicImageFormats(boolean output) { |
| 178 |
Map<Integer, List<MultiResolutionStreamInfo>> multiResolutionConfigs = |
| 179 |
output ? mMultiResolutionOutputConfigs : mMultiResolutionInputConfigs; |
| 180 |
int formatCount = multiResolutionConfigs.size(); |
| 181 |
|
| 182 |
int[] formats = new int[formatCount]; |
| 183 |
int i = 0; |
| 184 |
for (Integer format : multiResolutionConfigs.keySet()) { |
| 185 |
formats[i++] = StreamConfigurationMap.imageFormatToPublic(format); |
| 186 |
} |
| 187 |
|
| 188 |
return formats; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
public @NonNull Collection<MultiResolutionStreamInfo> getOutputInfo(@Format int format) { |
| 208 |
return getInfo(format, true); |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
public @NonNull Collection<MultiResolutionStreamInfo> getInputInfo(@Format int format) { |
| 227 |
return getInfo(format, false); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
private @NonNull Collection<MultiResolutionStreamInfo> getInfo(int format, boolean output) { |
| 232 |
int internalFormat = StreamConfigurationMap.imageFormatToInternal(format); |
| 233 |
Map<Integer, List<MultiResolutionStreamInfo>> multiResolutionConfigs = |
| 234 |
output ? mMultiResolutionOutputConfigs : mMultiResolutionInputConfigs; |
| 235 |
if (multiResolutionConfigs.containsKey(internalFormat)) { |
| 236 |
return Collections.unmodifiableCollection(multiResolutionConfigs.get(internalFormat)); |
| 237 |
} else { |
| 238 |
return Collections.emptyList(); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
private void appendConfigurationsString(StringBuilder sb, boolean output) { |
| 243 |
sb.append(output ? "Outputs(" : "Inputs("); |
| 244 |
int[] formats = getPublicImageFormats(output); |
| 245 |
if (formats != null) { |
| 246 |
for (int format : formats) { |
| 247 |
Collection<MultiResolutionStreamInfo> streamInfoList = |
| 248 |
getInfo(format, output); |
| 249 |
sb.append("[" + StreamConfigurationMap.formatToString(format) + ":"); |
| 250 |
for (MultiResolutionStreamInfo streamInfo : streamInfoList) { |
| 251 |
sb.append(String.format("[w:%d, h:%d, id:%s], ", |
| 252 |
streamInfo.getWidth(), streamInfo.getHeight(), |
| 253 |
streamInfo.getPhysicalCameraId())); |
| 254 |
} |
| 255 |
|
| 256 |
if (sb.charAt(sb.length() - 1) == ' ') { |
| 257 |
sb.delete(sb.length() - 2, sb.length()); |
| 258 |
} |
| 259 |
sb.append("]"); |
| 260 |
} |
| 261 |
} |
| 262 |
sb.append(")"); |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
@Override |
| 272 |
public boolean equals(final Object obj) { |
| 273 |
if (obj == null) { |
| 274 |
return false; |
| 275 |
} |
| 276 |
if (this == obj) { |
| 277 |
return true; |
| 278 |
} |
| 279 |
if (obj instanceof MultiResolutionStreamConfigurationMap) { |
| 280 |
final MultiResolutionStreamConfigurationMap other = |
| 281 |
(MultiResolutionStreamConfigurationMap) obj; |
| 282 |
if (!mConfigurations.keySet().equals(other.mConfigurations.keySet())) { |
| 283 |
return false; |
| 284 |
} |
| 285 |
|
| 286 |
for (String id : mConfigurations.keySet()) { |
| 287 |
if (!Arrays.equals(mConfigurations.get(id), other.mConfigurations.get(id))) { |
| 288 |
return false; |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
return true; |
| 293 |
} |
| 294 |
return false; |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
@Override |
| 301 |
public int hashCode() { |
| 302 |
return HashCodeHelpers.hashCodeGeneric( |
| 303 |
mConfigurations, mMultiResolutionOutputConfigs, mMultiResolutionInputConfigs); |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
@Override |
| 316 |
public String toString() { |
| 317 |
StringBuilder sb = new StringBuilder("MultiResolutionStreamConfigurationMap("); |
| 318 |
appendConfigurationsString(sb, true); |
| 319 |
sb.append(","); |
| 320 |
appendConfigurationsString(sb, false); |
| 321 |
sb.append(")"); |
| 322 |
|
| 323 |
return sb.toString(); |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
private final Map<String, StreamConfiguration[]> mConfigurations; |
| 328 |
|
| 329 |
|
| 330 |
private final Map<Integer, List<MultiResolutionStreamInfo>> mMultiResolutionOutputConfigs |
| 331 |
= new HashMap<Integer, List<MultiResolutionStreamInfo>>(); |
| 332 |
|
| 333 |
private final Map<Integer, List<MultiResolutionStreamInfo>> mMultiResolutionInputConfigs |
| 334 |
= new HashMap<Integer, List<MultiResolutionStreamInfo>>(); |
| 335 |
} |