In my app, I am analysing an image that is cropped, and detecting if it has text or not. This is the code of the icon that triggers the camera.
Icon(painter = painterResource(id = R.drawable.oval), contentDescription = null,
tint = Color.White,
modifier = Modifier
.size(100.dp)
.padding(16.dp)
.align(Alignment.BottomCenter)
.clickableWithoutRipple(interactionSource) {
if (!imageInProcess.value){
coroutineScope.launch {
viewModel.loadingCircleShown()
imageInProcess.value = true
val imageFile =
imageCaptureUseCase.takePicture(currentContext.executor)
onImageFile(imageFile)
imageUri = imageFile.toUri()
if (imageUri != emptyImageUri) {
val cropOptions = CropImageContractOptions(imageUri, CropImageOptions())
imageCropper.launch(
cropOptions)
viewModel.notLoadingCircleShown()
}
}
}
When the useer is in the cropper, te app the texts if there is text or not:
val imageCropper = rememberLauncherForActivityResult(
CropImageContract()
) { result ->
imageInProcess.value = false
if (result.isSuccessful) {
val encodedUri = Uri.encode(
result.uriContent!!
.toString()
.replace('%', '|')
)
val recognizer =
TextRecognition.getClient(TextRecognizerOptions.DEFAULT_OPTIONS)
val recognizedText = mutableStateOf("")
val image = InputImage.fromFilePath(currentContext, result.uriContent!!)
recognizer.process(image)
.addOnSuccessListener {
recognizedText.value = it.text
if (it.text.isNotEmpty()) {
navController.navigate(AppScreens.SecondScreenImage.route + "/$encodedUri" + "/${recognizedText.value}")
} else {
viewModel.clickedNullImageButton()
}
}.addOnFailureListener {
Log.e("TextCapture", "CropImageContract error: ")
Toast.makeText(currentContext, "holalal", Toast.LENGTH_SHORT).show()
}
} else {
val exception = result.error
Log.e("CameraCapture", "CropImageContract error: ${exception?.message}")
}
It is all going all right, but however, when I take a blurry photo, this error occurs and crashes the app : FATAL EXCEPTION: main Process: com.example.app, PID: 28738 java.lang.IllegalArgumentException