Extracted setupCameraView()

This commit is contained in:
dankito 2024-10-04 07:07:05 +02:00
parent 1ed96fce7d
commit 1228b6884d
1 changed files with 11 additions and 7 deletions

View File

@ -16,7 +16,6 @@ import com.google.zxing.common.HybridBinarizer
import com.google.zxing.qrcode.QRCodeReader
import net.codinux.banking.persistence.AndroidContext
import net.codinux.banking.ui.MainActivity
import net.codinux.banking.ui.config.DI
import net.codinux.log.logger
import java.nio.ByteBuffer
import java.util.concurrent.Executors
@ -41,12 +40,20 @@ actual object QrCodeService {
return // we don't have the permission to start the camera
}
val cameraProviderFuture = ProcessCameraProvider.getInstance(mainActivity)
val previewView = remember {
PreviewView(mainActivity)
}
setupCameraView(previewView, mainActivity, resultCallback)
AndroidView(factory = { previewView }, modifier = Modifier.fillMaxSize())
}
@Composable
private fun setupCameraView(previewView: PreviewView, mainActivity: MainActivity, resultCallback: (QrCodeReadResult) -> Unit) {
val cameraProviderFuture = ProcessCameraProvider.getInstance(mainActivity)
cameraProviderFuture.addListener({
// Used to bind the lifecycle of cameras to the lifecycle owner
val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()
@ -74,14 +81,11 @@ actual object QrCodeService {
// Bind use cases to camera
cameraProvider.bindToLifecycle(mainActivity, cameraSelector, preview, imageAnalyzer)
} catch(e: Exception) {
} catch (e: Exception) {
log.error(e) { "Use case binding failed" }
}
}, ContextCompat.getMainExecutor(mainActivity))
AndroidView(factory = { previewView }, modifier = Modifier.fillMaxSize())
}
}