I am attaching the code below. I am inverting an camera on click of a button starting the camera on view created of an fragment and click image on click of a button.
//used for inverting the camera.
private fun invertCamera() {
cameraSelector = if (cameraSelector == CameraSelector.DEFAULT_FRONT_CAMERA) {
CameraSelector.DEFAULT_BACK_CAMERA
} else {
CameraSelector.DEFAULT_FRONT_CAMERA
}
//start the camera again..
startCamera()
}
private fun startCamera() {
val processCameraProvider =
ProcessCameraProvider.getInstance(requireActivity().applicationContext)
val aspectRatioStrategy = AspectRatioStrategy.RATIO_4_3_FALLBACK_AUTO_STRATEGY
processCameraProvider.addListener({
try {
cameraProvider = processCameraProvider.get()
imageCapture = ImageCapture.Builder()
.setCaptureMode(CAPTURE_MODE_MINIMIZE_LATENCY)
.setResolutionSelector(ResolutionSelector.Builder()
.setResolutionStrategy(ResolutionStrategy( Size(800,600),
ResolutionStrategy.FALLBACK_RULE_CLOSEST_LOWER_THEN_HIGHER))
.setAspectRatioStrategy(aspectRatioStrategy).build())
.build()
val previewUseCase =
Preview.Builder().build()
previewUseCase.setSurfaceProvider(binding.cameraView.surfaceProvider)
cameraProvider?.unbindAll()
cameraProvider?.bindToLifecycle(this, cameraSelector, previewUseCase)
} catch (e: IOException) {
Log.d("Sunny", "Error$e")
}
}, ContextCompat.getMainExecutor(requireActivity().applicationContext))
if (imagePath != "" && firstTimePhoto){
firstTimePhoto = false
binding.imagePreview.isVisible = true
binding.captureImage.text = "Upload"
Picasso.get().load(FILE_UPLOAD +imagePath).into(binding.imagePreview)
binding.captureAgain.isVisible = true
}
}
private fun captureImage() {
val imageCapture = imageCapture ?: return
cameraProvider?.bindToLifecycle(this, cameraSelector, imageCapture)
imageCapture.takePicture(ContextCompat.getMainExecutor(requireActivity().applicationContext),
object : ImageCapture.OnImageCapturedCallback() {
@ExperimentalGetImage
override fun onCaptureSuccess(image: ImageProxy) {
super.onCaptureSuccess(image)
//get bitmap from image and handle rotation...
val capturedImageBitmap =
image.image?.toBitmap()?.rotate(image.imageInfo.rotationDegrees.toFloat())
//unbind camera ....
cameraProvider?.unbindAll()
binding.imagePreview.isVisible = true
//rotation handled...
binding.imagePreview.setImageBitmap(capturedImageBitmap?.let { flip(it) })
//validate from python api...
if (capturedImageBitmap != null) {
validateUserImage(capturedImageBitmap)
}
image.close()
}
override fun onError(exception: ImageCaptureException) {
super.onError(exception)
Log.d("Sunny", "$exception")
}
})
}
I am unable to understand why this error occurs as I am unable to replicate it. It exits of various devices according to firebase crashlytics.
New contributor
Sunny Gahlot is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.