-
Notifications
You must be signed in to change notification settings - Fork 2
MS-1494 Unified camera frame provider #1766
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
luhmirin-s
merged 3 commits into
main
from
feature/MS-1494-unified-camera-frame-provider
Aug 11, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /build |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| plugins { | ||
| id("simprints.infra") | ||
| } | ||
|
|
||
| android { | ||
| namespace = "com.simprints.infra.camera" | ||
| } | ||
|
|
||
| dependencies { | ||
| implementation(libs.androidX.cameraX.core) | ||
| implementation(libs.androidX.cameraX.lifecycle) | ||
| implementation(libs.androidX.cameraX.view) | ||
| implementation(libs.playServices.barcode) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <manifest /> |
258 changes: 258 additions & 0 deletions
258
infra/camera/src/main/java/com/simprints/infra/camera/CameraFrameProvider.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,258 @@ | ||
| package com.simprints.infra.camera | ||
|
|
||
| import android.content.Context | ||
| import android.graphics.Bitmap | ||
| import android.graphics.Rect | ||
| import android.util.Size | ||
| import androidx.camera.core.Camera | ||
| import androidx.camera.core.CameraSelector.DEFAULT_BACK_CAMERA | ||
| import androidx.camera.core.ImageAnalysis | ||
| import androidx.camera.core.ImageCapture | ||
| import androidx.camera.core.ImageCaptureException | ||
| import androidx.camera.core.ImageProxy | ||
| import androidx.camera.core.Preview | ||
| import androidx.camera.core.resolutionselector.ResolutionSelector | ||
| import androidx.camera.core.resolutionselector.ResolutionStrategy | ||
| import androidx.camera.lifecycle.ProcessCameraProvider | ||
| import androidx.camera.lifecycle.awaitInstance | ||
| import androidx.camera.view.PreviewView | ||
| import androidx.lifecycle.LifecycleOwner | ||
| import com.simprints.core.DispatcherBG | ||
| import com.simprints.core.DispatcherMain | ||
| import com.simprints.core.ExcludedFromGeneratedTestCoverageReports | ||
| import com.simprints.infra.camera.helpers.CameraFocusHelper | ||
| import com.simprints.infra.camera.helpers.FrameEmissionHelper | ||
| import com.simprints.infra.camera.usecase.NormalizeHighResBitmapToPreviewUseCase | ||
| import com.simprints.infra.logging.LoggingConstants.CrashReportTag | ||
| import com.simprints.infra.logging.Simber | ||
| import dagger.hilt.android.qualifiers.ApplicationContext | ||
| import kotlinx.coroutines.CoroutineDispatcher | ||
| import kotlinx.coroutines.channels.BufferOverflow | ||
| import kotlinx.coroutines.flow.Flow | ||
| import kotlinx.coroutines.flow.MutableSharedFlow | ||
| import kotlinx.coroutines.withContext | ||
| import java.util.concurrent.ExecutorService | ||
| import java.util.concurrent.Executors | ||
| import javax.inject.Inject | ||
|
|
||
| @ExcludedFromGeneratedTestCoverageReports(reason = "Camera API wrapper") | ||
| class CameraFrameProvider @Inject internal constructor( | ||
| @ApplicationContext private val context: Context, | ||
| @DispatcherBG private val bgDispatcher: CoroutineDispatcher, | ||
| @DispatcherMain private val mainDispatcher: CoroutineDispatcher, | ||
| private val cameraFocusManagerFactory: CameraFocusHelper.Factory, | ||
| private val normalizeHighResBitmapToPreviewUseCase: NormalizeHighResBitmapToPreviewUseCase, | ||
| ) { | ||
| private var executor: ExecutorService = Executors.newSingleThreadExecutor() | ||
|
|
||
| val frames: Flow<Frame> | ||
| field = MutableSharedFlow<Frame>( | ||
| extraBufferCapacity = 1, | ||
| onBufferOverflow = BufferOverflow.DROP_OLDEST, | ||
| ) | ||
|
|
||
| private var cameraProvider: ProcessCameraProvider? = null | ||
| private var imageCapture: ImageCapture? = null | ||
| private var imageAnalysis: ImageAnalysis? = null | ||
| private var camera: Camera? = null | ||
|
|
||
| private lateinit var previewRect: Rect | ||
| private lateinit var targetRect: Rect | ||
|
|
||
| private var previewSurface: PreviewView? = null | ||
| private val frameEmissionHelper = FrameEmissionHelper() | ||
|
|
||
| fun isInitialised() = camera != null | ||
|
|
||
| /** | ||
| * Initialise the camera and connect it to the provided UI elements. | ||
| * | ||
| * Call only after the preview and the targer has been laid out: | ||
| * ``` | ||
| * binding.preview.awaitLayout() | ||
| * binding.targerOverlay.awaitLayout() | ||
| * ``` | ||
| */ | ||
|
|
||
| @ExcludedFromGeneratedTestCoverageReports(reason = "Camera API wrapper") | ||
| suspend fun initialiseCamera( | ||
| lifecycleOwner: LifecycleOwner, | ||
| previewView: PreviewView, | ||
| target: Rect? = null, | ||
| highResolution: Boolean = false, | ||
| onError: (Throwable) -> Unit = {}, | ||
| ) = withContext(bgDispatcher) { | ||
| try { | ||
| // Caching to return with frames for post-processing and also to use for injection in future | ||
| previewRect = fullPreviewSizeRect(previewView) | ||
| targetRect = target ?: previewRect | ||
| } catch (e: Exception) { | ||
| Simber.e("Preview and target calculation failed", e, tag = CrashReportTag.CAMERA) | ||
| withContext(mainDispatcher) { onError(e) } | ||
| return@withContext | ||
| } | ||
|
|
||
| ensureExecutor() | ||
| previewSurface = previewView | ||
|
|
||
| frameEmissionHelper.configure(highResolution = highResolution) | ||
|
|
||
| val cameraSelector = DEFAULT_BACK_CAMERA | ||
| val resolutionSelector = ResolutionSelector | ||
| .Builder() | ||
| .setResolutionStrategy( | ||
| ResolutionStrategy( | ||
| Size(previewRect.width(), previewRect.height()), | ||
| ResolutionStrategy.FALLBACK_RULE_CLOSEST_HIGHER_THEN_LOWER, | ||
| ), | ||
| ).build() | ||
|
|
||
| imageAnalysis = ImageAnalysis | ||
| .Builder() | ||
| .setResolutionSelector(resolutionSelector) | ||
| .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST) | ||
| .setOutputImageRotationEnabled(true) | ||
| .setOutputImageFormat(ImageAnalysis.OUTPUT_IMAGE_FORMAT_RGBA_8888) | ||
| .build() | ||
|
|
||
| imageAnalysis?.setAnalyzer(executor) { proxy -> | ||
| proxy.use { frame -> | ||
| when { | ||
| frameEmissionHelper.shouldEmitAnalyserFrame() -> emitFrame(frame.toBitmap(), frame.imageInfo.rotationDegrees) | ||
| frameEmissionHelper.beginHighResolutionCapture() -> captureHighResolutionFrame() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| val capture = ImageCapture | ||
| .Builder() | ||
| .setCaptureMode(ImageCapture.CAPTURE_MODE_MAXIMIZE_QUALITY) | ||
| .build() | ||
| imageCapture = capture | ||
|
|
||
| withContext(mainDispatcher) { | ||
| val preview = Preview | ||
| .Builder() | ||
| .setResolutionSelector(resolutionSelector) | ||
| .build() | ||
| .also { it.surfaceProvider = previewView.surfaceProvider } | ||
|
|
||
| val provider = ProcessCameraProvider.awaitInstance(context).also { | ||
| cameraProvider = it | ||
| } | ||
|
|
||
| try { | ||
| provider.unbindAll() | ||
| camera = provider | ||
| .bindToLifecycle( | ||
| lifecycleOwner, | ||
| cameraSelector, | ||
| imageAnalysis, | ||
| capture, | ||
| preview, | ||
| ).also { boundCamera -> | ||
| with(cameraFocusManagerFactory.create(CrashReportTag.CAMERA)) { | ||
| setUpFocusOnTap(previewView, boundCamera) | ||
| setUpAutoFocus(previewView, boundCamera) | ||
| } | ||
| } | ||
| } catch (e: Exception) { | ||
| Simber.e("Camera binding failed", e, tag = CrashReportTag.CAMERA) | ||
| onError(e) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Toggle frame emission to save resources when processing of a | ||
| * single frame requires significant amount of time. | ||
| */ | ||
| fun setFrameEmissionEnabled(enabled: Boolean) { | ||
| frameEmissionHelper.setFrameEmissionEnabled(enabled) | ||
| } | ||
|
|
||
| /** | ||
| * Toggle camera flash. | ||
| */ | ||
| fun setTorchEnabled(enabled: Boolean) { | ||
| camera?.cameraControl?.enableTorch(enabled) | ||
| } | ||
|
|
||
| /** | ||
| * Clear all resources and stop the camera. | ||
| */ | ||
| fun release() { | ||
| imageAnalysis?.clearAnalyzer() | ||
| imageAnalysis = null | ||
|
|
||
| cameraProvider?.unbindAll() | ||
| cameraProvider = null | ||
|
|
||
| previewSurface = null | ||
| imageCapture = null | ||
| camera = null | ||
| frameEmissionHelper.reset() | ||
|
|
||
| if (!executor.isShutdown) executor.shutdown() | ||
| } | ||
|
|
||
| private fun ensureExecutor() { | ||
| if (executor.isShutdown) { | ||
| executor = Executors.newSingleThreadExecutor() | ||
| } | ||
| } | ||
|
|
||
| private fun emitFrame( | ||
| bitmap: Bitmap, | ||
| rotation: Int, | ||
| ) { | ||
| frames.tryEmit(Frame(bitmap = bitmap, rotation = rotation, previewBounds = previewRect, targetBounds = targetRect)) | ||
| } | ||
|
|
||
| private fun captureHighResolutionFrame() { | ||
| val capture = imageCapture | ||
| if (capture == null) { | ||
| frameEmissionHelper.cancelHighResolutionCapture() | ||
| return | ||
| } | ||
|
|
||
| try { | ||
| capture.takePicture( | ||
| executor, | ||
| @ExcludedFromGeneratedTestCoverageReports(reason = "Camera API wrapper") | ||
| object : ImageCapture.OnImageCapturedCallback() { | ||
| override fun onCaptureSuccess(imageProxy: ImageProxy) { | ||
| val shouldEmitFrame = frameEmissionHelper.completeHighResolutionCapture() | ||
| imageProxy.use { capturedFrame -> | ||
| val rotationDegrees = capturedFrame.imageInfo.rotationDegrees | ||
| val previewViewWidth = previewRect.width() | ||
| val previewViewHeight = previewRect.height() | ||
|
|
||
| if (shouldEmitFrame) { | ||
| emitFrame( | ||
| normalizeHighResBitmapToPreviewUseCase( | ||
| capturedFrame.toBitmap(), | ||
| rotationDegrees, | ||
| previewViewWidth, | ||
| previewViewHeight, | ||
| ), | ||
| rotationDegrees, | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override fun onError(exception: ImageCaptureException) { | ||
| frameEmissionHelper.cancelHighResolutionCapture() | ||
| Simber.e("High-res frame capture failed", exception, tag = CrashReportTag.CAMERA) | ||
| } | ||
| }, | ||
| ) | ||
| } catch (e: Exception) { | ||
| frameEmissionHelper.cancelHighResolutionCapture() | ||
| Simber.e("High-res frame capture failed", e, tag = CrashReportTag.CAMERA) | ||
| } | ||
| } | ||
|
|
||
| private fun fullPreviewSizeRect(surface: PreviewView): Rect = Rect(0, 0, surface.width, surface.height) | ||
| } | ||
11 changes: 11 additions & 0 deletions
11
infra/camera/src/main/java/com/simprints/infra/camera/Frame.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package com.simprints.infra.camera | ||
|
|
||
| import android.graphics.Bitmap | ||
| import android.graphics.Rect | ||
|
|
||
| data class Frame( | ||
| val bitmap: Bitmap, | ||
| val rotation: Int, | ||
| val targetBounds: Rect, | ||
| val previewBounds: Rect, | ||
| ) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.