Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix any initialization errors in getFormats() (e.g. IllegalArgumentException - width must be positive) #3236

Merged
merged 3 commits into from
Oct 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.mrousavy.camera.core
import android.annotation.SuppressLint
import android.graphics.ImageFormat
import android.hardware.camera2.CameraCharacteristics
import android.util.Log
import android.util.Range
import android.util.Size
import android.util.SizeF
Expand Down Expand Up @@ -36,6 +37,10 @@ import kotlin.math.sqrt
@SuppressLint("RestrictedApi")
@Suppress("FoldInitializerAndIfToElvis")
class CameraDeviceDetails(private val cameraInfo: CameraInfo, extensionsManager: ExtensionsManager) {
companion object {
private const val TAG = "CameraDeviceDetails"
}

// Generic props available on all implementations
private val cameraId = cameraInfo.id ?: throw NoCameraDeviceError()
private val position = Position.fromLensFacing(cameraInfo.lensFacing)
Expand Down Expand Up @@ -113,24 +118,36 @@ class CameraDeviceDetails(private val cameraInfo: CameraInfo, extensionsManager:
val dynamicRangeProfiles = videoCapabilities.supportedDynamicRanges

dynamicRangeProfiles.forEach { dynamicRange ->
val qualities = videoCapabilities.getSupportedQualities(dynamicRange)
val videoSizes = qualities.map { it as ConstantQuality }.flatMap { it.typicalSizes }
val photoSizes = cameraInfoInternal.getSupportedResolutions(ImageFormat.JPEG)
val fpsRanges = cameraInfo.supportedFrameRateRanges
val minFps = fpsRanges.minOf { it.lower }
val maxFps = fpsRanges.maxOf { it.upper }

videoSizes.forEach { videoSize ->
// not every size supports the maximum FPS range
val maxFpsForSize = CamcorderProfileUtils.getMaximumFps(cameraId, videoSize) ?: maxFps
// if the FPS range for this size is even smaller than min FPS, we need to clamp that as well.
val minFpsForSize = min(minFps, maxFpsForSize)
val fpsRange = Range(minFpsForSize, maxFpsForSize)

photoSizes.forEach { photoSize ->
val map = buildFormatMap(photoSize, videoSize, fpsRange)
array.pushMap(map)
try {
val qualities = videoCapabilities.getSupportedQualities(dynamicRange)
val videoSizes = qualities.map { it as ConstantQuality }.flatMap { it.typicalSizes }
val photoSizes = cameraInfoInternal.getSupportedResolutions(ImageFormat.JPEG)
val fpsRanges = cameraInfo.supportedFrameRateRanges
val minFps = fpsRanges.minOf { it.lower }
val maxFps = fpsRanges.maxOf { it.upper }

videoSizes.forEach { videoSize ->
try {
// not every size supports the maximum FPS range
val maxFpsForSize = CamcorderProfileUtils.getMaximumFps(cameraId, videoSize) ?: maxFps
// if the FPS range for this size is even smaller than min FPS, we need to clamp that as well.
val minFpsForSize = min(minFps, maxFpsForSize)
val fpsRange = Range(minFpsForSize, maxFpsForSize)

photoSizes.forEach { photoSize ->
try {
val map = buildFormatMap(photoSize, videoSize, fpsRange)
array.pushMap(map)
} catch (error: Throwable) {
Log.w(TAG, "Photo size ${photoSize.width}x${photoSize.height} cannot be used as a format!", error)
}
}
} catch (error: Throwable) {
Log.w(TAG, "Video size ${videoSize.width}x${videoSize.height} cannot be used as a format!", error)
}
}
} catch (error: Throwable) {
Log.w(TAG, "Dynamic Range Profile $dynamicRange cannot be used as a format!", error)
}
}

Expand Down