Skip to content

Commit

Permalink
Android
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
lessthanoptimal committed Aug 21, 2024
1 parent 2866bd1 commit cab80c5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<CameraID, DeviceSurfaces>()
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {}
Expand Down

0 comments on commit cab80c5

Please sign in to comment.