fix: Only use supported PixelFormats

This commit is contained in:
Marc Rousavy 2023-10-03 14:05:23 +02:00
parent 224bffd4dc
commit 9a777ba240

View File

@ -3,7 +3,6 @@ package com.mrousavy.camera.parsers
import android.graphics.ImageFormat
import com.mrousavy.camera.PixelFormatNotSupportedError
@Suppress("FoldInitializerAndIfToElvis")
enum class PixelFormat(override val unionValue: String) : JSUnionValue {
YUV("yuv"),
RGB("rgb"),
@ -11,29 +10,24 @@ enum class PixelFormat(override val unionValue: String) : JSUnionValue {
UNKNOWN("unknown");
fun toImageFormat(): Int {
val result = when (this) {
return when (this) {
YUV -> ImageFormat.YUV_420_888
RGB -> android.graphics.PixelFormat.RGBA_8888
NATIVE -> ImageFormat.PRIVATE
UNKNOWN -> null
else -> throw PixelFormatNotSupportedError(this.unionValue)
}
if (result == null) {
throw PixelFormatNotSupportedError(this.unionValue)
}
return result
}
companion object : JSUnionValue.Companion<PixelFormat> {
fun fromImageFormat(imageFormat: Int): PixelFormat =
when (imageFormat) {
fun fromImageFormat(imageFormat: Int): PixelFormat {
return when (imageFormat) {
ImageFormat.YUV_420_888 -> YUV
android.graphics.PixelFormat.RGBA_8888 -> RGB
ImageFormat.PRIVATE -> NATIVE
else -> UNKNOWN
}
}
override fun fromUnionValue(unionValue: String?): PixelFormat? =
when (unionValue) {
override fun fromUnionValue(unionValue: String?): PixelFormat? {
return when (unionValue) {
"yuv" -> YUV
"rgb" -> RGB
"native" -> NATIVE
@ -41,4 +35,5 @@ enum class PixelFormat(override val unionValue: String) : JSUnionValue {
else -> null
}
}
}
}