Skip to content

Commit e66f187

Browse files
authored
fix: Fix any initialization errors in getFormats() (e.g. IllegalArgumentException - width must be positive) (#3236)
* fix: Fix any initialization errors in `getFormats()` (e.g. `IllegalArgumentException - width must be positive`) * Update CameraDeviceDetails.kt * fix: Format
1 parent 6a12216 commit e66f187

File tree

1 file changed

+34
-17
lines changed

1 file changed

+34
-17
lines changed

package/android/src/main/java/com/mrousavy/camera/core/CameraDeviceDetails.kt

+34-17
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.mrousavy.camera.core
33
import android.annotation.SuppressLint
44
import android.graphics.ImageFormat
55
import android.hardware.camera2.CameraCharacteristics
6+
import android.util.Log
67
import android.util.Range
78
import android.util.Size
89
import android.util.SizeF
@@ -36,6 +37,10 @@ import kotlin.math.sqrt
3637
@SuppressLint("RestrictedApi")
3738
@Suppress("FoldInitializerAndIfToElvis")
3839
class CameraDeviceDetails(private val cameraInfo: CameraInfo, extensionsManager: ExtensionsManager) {
40+
companion object {
41+
private const val TAG = "CameraDeviceDetails"
42+
}
43+
3944
// Generic props available on all implementations
4045
private val cameraId = cameraInfo.id ?: throw NoCameraDeviceError()
4146
private val position = Position.fromLensFacing(cameraInfo.lensFacing)
@@ -113,24 +118,36 @@ class CameraDeviceDetails(private val cameraInfo: CameraInfo, extensionsManager:
113118
val dynamicRangeProfiles = videoCapabilities.supportedDynamicRanges
114119

115120
dynamicRangeProfiles.forEach { dynamicRange ->
116-
val qualities = videoCapabilities.getSupportedQualities(dynamicRange)
117-
val videoSizes = qualities.map { it as ConstantQuality }.flatMap { it.typicalSizes }
118-
val photoSizes = cameraInfoInternal.getSupportedResolutions(ImageFormat.JPEG)
119-
val fpsRanges = cameraInfo.supportedFrameRateRanges
120-
val minFps = fpsRanges.minOf { it.lower }
121-
val maxFps = fpsRanges.maxOf { it.upper }
122-
123-
videoSizes.forEach { videoSize ->
124-
// not every size supports the maximum FPS range
125-
val maxFpsForSize = CamcorderProfileUtils.getMaximumFps(cameraId, videoSize) ?: maxFps
126-
// if the FPS range for this size is even smaller than min FPS, we need to clamp that as well.
127-
val minFpsForSize = min(minFps, maxFpsForSize)
128-
val fpsRange = Range(minFpsForSize, maxFpsForSize)
129-
130-
photoSizes.forEach { photoSize ->
131-
val map = buildFormatMap(photoSize, videoSize, fpsRange)
132-
array.pushMap(map)
121+
try {
122+
val qualities = videoCapabilities.getSupportedQualities(dynamicRange)
123+
val videoSizes = qualities.map { it as ConstantQuality }.flatMap { it.typicalSizes }
124+
val photoSizes = cameraInfoInternal.getSupportedResolutions(ImageFormat.JPEG)
125+
val fpsRanges = cameraInfo.supportedFrameRateRanges
126+
val minFps = fpsRanges.minOf { it.lower }
127+
val maxFps = fpsRanges.maxOf { it.upper }
128+
129+
videoSizes.forEach { videoSize ->
130+
try {
131+
// not every size supports the maximum FPS range
132+
val maxFpsForSize = CamcorderProfileUtils.getMaximumFps(cameraId, videoSize) ?: maxFps
133+
// if the FPS range for this size is even smaller than min FPS, we need to clamp that as well.
134+
val minFpsForSize = min(minFps, maxFpsForSize)
135+
val fpsRange = Range(minFpsForSize, maxFpsForSize)
136+
137+
photoSizes.forEach { photoSize ->
138+
try {
139+
val map = buildFormatMap(photoSize, videoSize, fpsRange)
140+
array.pushMap(map)
141+
} catch (error: Throwable) {
142+
Log.w(TAG, "Photo size ${photoSize.width}x${photoSize.height} cannot be used as a format!", error)
143+
}
144+
}
145+
} catch (error: Throwable) {
146+
Log.w(TAG, "Video size ${videoSize.width}x${videoSize.height} cannot be used as a format!", error)
147+
}
133148
}
149+
} catch (error: Throwable) {
150+
Log.w(TAG, "Dynamic Range Profile $dynamicRange cannot be used as a format!", error)
134151
}
135152
}
136153

0 commit comments

Comments
 (0)