Try to select HDR profile

This commit is contained in:
Marc Rousavy 2023-08-21 14:42:34 +02:00
parent e1b04088c6
commit 4d77bc3ea9
4 changed files with 53 additions and 4 deletions

View File

@ -116,6 +116,7 @@ class CameraSession(private val context: Context,
preview,
photo,
video,
hdr == true,
this)
if (this.cameraId == cameraId && this.outputs == outputs && isActive == isRunning) {
Log.i(TAG, "Nothing changed in configuration, canceling..")
@ -137,6 +138,18 @@ class CameraSession(private val context: Context,
this.videoStabilizationMode = videoStabilizationMode
this.hdr = hdr
this.lowLightBoost = lowLightBoost
val currentOutputs = outputs
if (currentOutputs != null && currentOutputs.enableHdr != hdr) {
// Update existing HDR for Outputs
this.outputs = CameraOutputs(currentOutputs.cameraId,
cameraManager,
currentOutputs.preview,
currentOutputs.photo,
currentOutputs.video,
hdr,
this)
}
launch {
startRunning()
}

View File

@ -75,6 +75,14 @@ suspend fun CameraDevice.createCaptureSession(cameraManager: CameraManager,
outputs.videoOutput?.let { output ->
outputConfigurations.add(output.toOutputConfiguration(characteristics))
}
if (outputs.enableHdr == true && Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
val supportedProfiles = characteristics.get(CameraCharacteristics.REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES)
val hdrProfile = supportedProfiles?.bestProfile
if (hdrProfile != null) {
Log.i(TAG, "Camera $id: Using HDR Profile $hdrProfile...")
outputConfigurations.forEach { it.dynamicRangeProfile = hdrProfile }
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
// API >=28

View File

@ -0,0 +1,27 @@
package com.mrousavy.camera.extensions
import android.hardware.camera2.params.DynamicRangeProfiles
import android.os.Build
import androidx.annotation.RequiresApi
private fun Set<Long>.firstMatch(filter: Set<Long>): Long? {
filter.forEach { f ->
if (this.contains(f)) {
return f
}
}
return null
}
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
private val bestProfiles = setOf(
DynamicRangeProfiles.HDR10_PLUS,
DynamicRangeProfiles.HDR10,
DynamicRangeProfiles.HLG10
)
val DynamicRangeProfiles.bestProfile: Long?
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
get() {
return supportedProfiles.firstMatch(bestProfiles)
}

View File

@ -21,6 +21,7 @@ class CameraOutputs(val cameraId: String,
val preview: PreviewOutput? = null,
val photo: PhotoOutput? = null,
val video: VideoOutput? = null,
val enableHdr: Boolean? = false,
val callback: Callback): Closeable {
companion object {
private const val TAG = "CameraOutputs"
@ -34,8 +35,7 @@ class CameraOutputs(val cameraId: String,
data class VideoOutput(val targetSize: Size? = null,
val enableRecording: Boolean = false,
val enableFrameProcessor: Boolean? = false,
val format: Int = ImageFormat.PRIVATE,
val hdrProfile: Long? = null /* DynamicRangeProfiles */)
val format: Int = ImageFormat.PRIVATE)
interface Callback {
fun onPhotoCaptured(image: Image)
@ -61,12 +61,13 @@ class CameraOutputs(val cameraId: String,
override fun equals(other: Any?): Boolean {
if (other !is CameraOutputs) return false
return this.cameraId == other.cameraId
&& (this.preview == null) == (other.preview == null)
&& this.preview?.surface == other.preview?.surface
&& this.photo?.targetSize == other.photo?.targetSize
&& this.photo?.format == other.photo?.format
&& this.video?.enableRecording == other.video?.enableRecording
&& this.video?.targetSize == other.video?.targetSize
&& this.video?.format == other.video?.format
&& this.enableHdr == other.enableHdr
}
override fun hashCode(): Int {
@ -132,7 +133,7 @@ class CameraOutputs(val cameraId: String,
}
}, CameraQueues.videoQueue.handler)
Log.i(TAG, "Adding ${size.width}x${size.height} video output. (Format: ${video.format} | HDR: ${video.hdrProfile})")
Log.i(TAG, "Adding ${size.width}x${size.height} video output. (Format: ${video.format})")
videoOutput = ImageReaderOutput(imageReader, SurfaceOutput.OutputType.VIDEO)
}