From cab80c551c2f4befa60f804b8423530b2654753f Mon Sep 17 00:00:00 2001 From: Peter Abeles Date: Wed, 21 Aug 2024 14:53:43 -0700 Subject: [PATCH] Android - If a camera was already set initialized it would initialize it again - A redesign on how to handle that situation might be better - This prevents the crash and works in one specific use case --- .../boofcv/android/fragments/CameraProcessFragment.kt | 6 +++--- .../android/fragments/ImageProcessingFragment.kt | 10 ++++++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/integration/boofcv-android/src/main/java/boofcv/android/fragments/CameraProcessFragment.kt b/integration/boofcv-android/src/main/java/boofcv/android/fragments/CameraProcessFragment.kt index 4b9863d550..2e9935b5ae 100644 --- a/integration/boofcv-android/src/main/java/boofcv/android/fragments/CameraProcessFragment.kt +++ b/integration/boofcv-android/src/main/java/boofcv/android/fragments/CameraProcessFragment.kt @@ -49,7 +49,7 @@ import kotlin.math.roundToInt */ abstract class CameraProcessFragment : Fragment() { /** Used to adjust how it captures images. Affects quality and speed */ - private var captureRequestTemplateType = CameraDevice.TEMPLATE_RECORD + protected var captureRequestTemplateType = CameraDevice.TEMPLATE_RECORD /** Data structures for each camera and capture surface */ protected val cameraDevices = HashMap() @@ -92,7 +92,7 @@ abstract class CameraProcessFragment : Fragment() { for (reader in cam.readers) { reader.close() } - } catch( e: IllegalStateException) { + } catch (e: IllegalStateException) { Log.e(TAG, "Failed to close a camera", e) } } @@ -262,7 +262,7 @@ abstract class CameraProcessFragment : Fragment() { * Starts a [CameraCaptureSession] and returns the configured session (as the result of the * suspend coroutine */ - private suspend fun createCaptureSession(cameraID: CameraID, camera: DeviceSurfaces): + protected suspend fun createCaptureSession(cameraID: CameraID, camera: DeviceSurfaces): CameraCaptureSession = suspendCoroutine { cont -> // Configure it so it can point a camera inside a multi-camera system diff --git a/integration/boofcv-android/src/main/java/boofcv/android/fragments/ImageProcessingFragment.kt b/integration/boofcv-android/src/main/java/boofcv/android/fragments/ImageProcessingFragment.kt index 6437b14a06..d770524a0a 100644 --- a/integration/boofcv-android/src/main/java/boofcv/android/fragments/ImageProcessingFragment.kt +++ b/integration/boofcv-android/src/main/java/boofcv/android/fragments/ImageProcessingFragment.kt @@ -195,6 +195,12 @@ abstract class ImageProcessingFragment : CameraProcessFragment() { initializeCameraCoordinates(cameraID, right - left, bottom - top, rotation) } + // If the device already exists, don't create it again and crash + // NOTE: If the resolution changed this might not be the correct behavior + val device = cameraDevices[cameraID] + if (device != null && device.session != null) + return@addOnLayoutChangeListener + // Add image processor for computer vision stream val reader = addCameraProcessor( cameraID, @@ -319,8 +325,8 @@ abstract class ImageProcessingFragment : CameraProcessFragment() { setWillNotDraw(false) } - override fun onDraw(canvas: Canvas?) { - onDrawFrame(this, canvas!!) + override fun onDraw(canvas: Canvas) { + onDrawFrame(this, canvas) } override fun surfaceCreated(holder: SurfaceHolder) {}