fix: Use 0x0 as a fallback Size if SENSOR_INFO_PHYSICAL_SIZE is null (e.g. on USB cameras) (#2608)

2602:There is a NullPointerException here because CameraCharacteristics cannot get the SENSOR_INFO_PHYSICAL_SIZE value of the camera device
This commit is contained in:
Guoting Lei 2024-02-29 21:53:36 +08:00 committed by GitHub
parent bca9472ab8
commit e8dd1e0b2e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -10,6 +10,7 @@ import android.os.Build
import android.util.Log import android.util.Log
import android.util.Range import android.util.Range
import android.util.Size import android.util.Size
import android.util.SizeF
import android.view.SurfaceHolder import android.view.SurfaceHolder
import com.facebook.react.bridge.Arguments import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.ReadableArray import com.facebook.react.bridge.ReadableArray
@ -68,7 +69,7 @@ class CameraDeviceDetails(private val cameraManager: CameraManager, val cameraId
// 35mm is the film standard sensor size // 35mm is the film standard sensor size
characteristics.get(CameraCharacteristics.LENS_INFO_AVAILABLE_FOCAL_LENGTHS) ?: floatArrayOf(35f) characteristics.get(CameraCharacteristics.LENS_INFO_AVAILABLE_FOCAL_LENGTHS) ?: floatArrayOf(35f)
} }
val sensorSize by lazy { characteristics.get(CameraCharacteristics.SENSOR_INFO_PHYSICAL_SIZE)!! } val sensorSize by lazy { characteristics.get(CameraCharacteristics.SENSOR_INFO_PHYSICAL_SIZE) ?: SizeF(0f, 0f) }
val activeSize val activeSize
get() = characteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE)!! get() = characteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE)!!
val sensorOrientation by lazy { val sensorOrientation by lazy {
@ -230,6 +231,9 @@ class CameraDeviceDetails(private val cameraManager: CameraManager, val cameraId
} }
private fun getFieldOfView(focalLength: Float): Double { private fun getFieldOfView(focalLength: Float): Double {
if ((sensorSize.width == 0f) || (sensorSize.height == 0f)) {
return 0.0
}
val sensorDiagonal = sqrt((sensorSize.width * sensorSize.width + sensorSize.height * sensorSize.height).toDouble()) val sensorDiagonal = sqrt((sensorSize.width * sensorSize.width + sensorSize.height * sensorSize.height).toDouble())
val fovRadians = 2.0 * atan2(sensorDiagonal, (2.0 * focalLength)) val fovRadians = 2.0 * atan2(sensorDiagonal, (2.0 * focalLength))
return Math.toDegrees(fovRadians) return Math.toDegrees(fovRadians)