@Shadman Adman - This is what I've understood from your comment but clearly it's wrong (it now crashes my app). The issue seems to be with previewView. I'm just not understanding how it's passed to the composable. To save time you could just edit this answer to point out what I'm doing wrong.
class MainActivity : ComponentActivity() {
private lateinit var controller: LifecycleCameraController
var previewView = null // is this correct?
setContent {
CameraApp1Theme {
// ignore all this - just putting it here as the variables are referenced
val resolutionSelector = ResolutionSelector.Builder()
.setResolutionStrategy(HIGHEST_AVAILABLE_STRATEGY)
.setAspectRatioStrategy(AspectRatioStrategy.RATIO_4_3_FALLBACK_AUTO_STRATEGY)
.build()
controller = remember {
LifecycleCameraController(applicationContext).apply {
setEnabledUseCases(CameraController.IMAGE_CAPTURE)
setImageCaptureMode(CAPTURE_MODE_MAXIMIZE_QUALITY)
setImageCaptureResolutionSelector(resolutionSelector)
}
}
// ignore all this - just putting it here as the variables are referenced
CameraPreview(
controller = controller,
previewView = PreviewView, // should I be passing PreviewView or previewView?
handleTouch = handleTouch,
modifier = Modifier
.align(Alignment.Center)
.size(pwidth.dp, pheight.dp)
)
}
}
}
@Composable
fun CameraPreview(
controller: LifecycleCameraController,
previewView: PreviewView, // are these both correct?
handleTouch: View.OnTouchListener, // are these both correct?
modifier: Modifier = Modifier
) {
val lifecycleOwner = LocalLifecycleOwner.current
val screenSize = Size(9, 12)
val resolutionSelector = ResolutionSelector.Builder()
.setResolutionStrategy(ResolutionStrategy(screenSize, FALLBACK_RULE_CLOSEST_LOWER_THEN_HIGHER))
.setAspectRatioStrategy(RATIO_4_3_FALLBACK_AUTO_STRATEGY)
.build()
AndroidView(
factory = {
PreviewView(it).apply {
this.controller = controller
controller.bindToLifecycle(lifecycleOwner)
controller.previewResolutionSelector = resolutionSelector
}
},
modifier = modifier
)
previewView.setOnTouchListener(handleTouch) // here?
}