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

chore: Separate out the format/config settings for cleaner code #2926

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
@@ -1,5 +1,6 @@
package com.mrousavy.camera.core

import android.util.Range
import androidx.camera.core.Preview.SurfaceProvider
import com.mrousavy.camera.core.types.CameraDeviceFormat
import com.mrousavy.camera.core.types.CodeType
Expand Down Expand Up @@ -43,6 +44,16 @@ data class CameraConfiguration(
// Audio Session
var audio: Output<Audio> = Output.Disabled.create()
) {
val targetFpsRange: Range<Int>?
get() {
val fps = fps ?: return null
return if (enableLowLightBoost) {
Range(fps / 2, fps)
} else {
Range(fps, fps)
}
}

// Output<T> types, those need to be comparable
data class CodeScanner(val codeTypes: List<CodeType>)
data class Photo(val enableHdr: Boolean, val photoQualityBalance: QualityBalance)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import com.mrousavy.camera.core.extensions.forSize
import com.mrousavy.camera.core.extensions.getCameraError
import com.mrousavy.camera.core.extensions.id
import com.mrousavy.camera.core.extensions.isSDR
import com.mrousavy.camera.core.extensions.setTargetFrameRate
import com.mrousavy.camera.core.extensions.takePicture
import com.mrousavy.camera.core.extensions.toCameraError
import com.mrousavy.camera.core.extensions.withExtension
Expand Down Expand Up @@ -347,6 +348,13 @@ class CameraSession(private val context: Context, private val callback: Callback
val analyzer = ImageAnalysis.Builder().also { analysis ->
analysis.setBackpressureStrategy(ImageAnalysis.STRATEGY_BLOCK_PRODUCER)
analysis.setOutputImageFormat(pixelFormat.toImageAnalysisFormat())
if (fpsRange != null) {
assertFormatRequirement("fps", format, InvalidFpsError(fpsRange.upper)) {
fpsRange.lower >= it.minFps &&
fpsRange.upper <= it.maxFps
}
analysis.setTargetFrameRate(fpsRange)
}
if (format != null) {
Log.i(TAG, "Frame Processor size: ${format.videoSize}")
val resolutionSelector = ResolutionSelector.Builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.mrousavy.camera.core.extensions

import android.hardware.camera2.CaptureRequest
import android.util.Range
import androidx.annotation.OptIn
import androidx.camera.camera2.interop.Camera2Interop
import androidx.camera.camera2.interop.ExperimentalCamera2Interop
import androidx.camera.core.ImageAnalysis

@OptIn(ExperimentalCamera2Interop::class)
fun ImageAnalysis.Builder.setTargetFrameRate(frameRate: Range<Int>) {
val camera2Interop = Camera2Interop.Extender(this)
camera2Interop.setCaptureRequestOption(
CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE,
frameRate
)
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.mrousavy.camera.core.types

import android.util.Size
import androidx.camera.core.resolutionselector.ResolutionSelector
import androidx.camera.video.FallbackStrategy
import androidx.camera.video.Quality
import androidx.camera.video.QualitySelector
import com.facebook.react.bridge.ReadableMap
import com.mrousavy.camera.core.InvalidTypeScriptUnionError
import com.mrousavy.camera.core.extensions.forSize
import kotlin.math.abs

data class CameraDeviceFormat(
Expand All @@ -29,6 +31,17 @@ data class CameraDeviceFormat(
val videoSize: Size
get() = Size(videoWidth, videoHeight)

val photoResolutionSelector: ResolutionSelector
get() = ResolutionSelector.Builder()
.forSize(photoSize)
.setAllowedResolutionMode(ResolutionSelector.PREFER_HIGHER_RESOLUTION_OVER_CAPTURE_RATE)
.build()
val videoResolutionSelector: ResolutionSelector
get() = ResolutionSelector.Builder()
.forSize(videoSize)
.setAllowedResolutionMode(ResolutionSelector.PREFER_CAPTURE_RATE_OVER_HIGHER_RESOLUTION)
.build()

private val qualitySizes = mapOf<Quality, Int>(
Quality.SD to 720 * 480,
Quality.HD to 1280 * 720,
Expand Down