We finished the image analyzer.
Now let's continue setting up the image analysis and binding the camera to the life cycle.
Let's start by set up the image analysis.
Launch the effect unit.
Try camera controller dot set image analysis analyzer.
Passing two parameters.
The context compat dot get main executor passing the context and analyzer.
Catch a exception log dot e tag failed to set up image analysis.
You can put E dot message like this.
Okay.
The launched effect is used to set up the image analysis analyzer in a safe, lifecycle aware way.
The set image analysis analyzer is a side effect.
It interacts with external systems like camera, and the side effects should not be called directly
during compositions.
The solution is using the launched effect.
Launched effect provides a safe coroutine scope for side effects.
One time execution unit means here unit means no dependencies runs once, so the unit here means no
dependencies.
Runs once when the composable enters composition.
Okay, so we need to run this only one time.
Then another launched effect under this to bind the camera to the life cycle, starting with launched
effect passing life cycle owner dot lifecycle.
And here set the try and catch camera controller.
Bind to lifecycle and set the lifecycle own exception log failed to bind.
There is a very important note.
You should pay attention to it here.
The camera is a hardware resource that must respect Android lifecycle.
Pose.
Resume.
Destroy and others.
The solution here is the camera controller dot bind to lifecycle method connects the camera to the lifecycle.
Camera automatically opens when the lifecycle starts.
Resume closes when the lifecycle poses or stops and releases resource when destroyed.
Okay, so this is a very important note.
You should pay attention to it.
When the lifecycle owner changes, the configuration changes, fragment transitions or any other recomposition
ensures the camera is always bound to the correct current life cycle.
Without lifecycle binding, camera stays open when app goes to background battery drain camera resource
leaks.
App may crash when trying to use an available camera.
Other apps can't access the camera and so on.
So there are a lot of problems you should prevent using the bind to your lifecycle method.
Then the last thing is disposable effect.
Pass the unit.
And here on dispose camera controller dot not unbind.
We need to clear image analysis analyzer.
We need to free up the memory and quit the camera controller.
Also we need to set a box with detection overlay.
This is the Composables that we created before the box with detection overlay.
To set a box over the detected results and the detected objects.
Okay, so this is our camera screen containing many objects and many launched effects and disposable
effect.