WEBVTT

00:00.080 --> 00:02.200
We finished the image analyzer.

00:02.240 --> 00:08.280
Now let's continue setting up the image analysis and binding the camera to the life cycle.

00:08.400 --> 00:13.160
Let's start by set up the image analysis.

00:13.200 --> 00:15.640
Launch the effect unit.

00:15.760 --> 00:21.120
Try camera controller dot set image analysis analyzer.

00:21.320 --> 00:23.440
Passing two parameters.

00:23.480 --> 00:32.560
The context compat dot get main executor passing the context and analyzer.

00:32.600 --> 00:39.360
Catch a exception log dot e tag failed to set up image analysis.

00:39.400 --> 00:42.720
You can put E dot message like this.

00:42.760 --> 00:43.200
Okay.

00:43.360 --> 00:53.280
The launched effect is used to set up the image analysis analyzer in a safe, lifecycle aware way.

00:53.520 --> 00:58.320
The set image analysis analyzer is a side effect.

00:58.320 --> 01:05.410
It interacts with external systems like camera, and the side effects should not be called directly

01:05.410 --> 01:07.170
during compositions.

01:07.210 --> 01:09.890
The solution is using the launched effect.

01:09.930 --> 01:15.370
Launched effect provides a safe coroutine scope for side effects.

01:15.570 --> 01:27.730
One time execution unit means here unit means no dependencies runs once, so the unit here means no

01:27.730 --> 01:28.770
dependencies.

01:29.290 --> 01:33.490
Runs once when the composable enters composition.

01:33.530 --> 01:36.850
Okay, so we need to run this only one time.

01:36.890 --> 01:46.130
Then another launched effect under this to bind the camera to the life cycle, starting with launched

01:46.130 --> 01:50.930
effect passing life cycle owner dot lifecycle.

01:51.050 --> 01:56.170
And here set the try and catch camera controller.

01:56.170 --> 02:03.050
Bind to lifecycle and set the lifecycle own exception log failed to bind.

02:03.130 --> 02:05.290
There is a very important note.

02:05.330 --> 02:07.500
You should pay attention to it here.

02:07.540 --> 02:15.420
The camera is a hardware resource that must respect Android lifecycle.

02:15.460 --> 02:16.060
Pose.

02:16.060 --> 02:16.820
Resume.

02:16.820 --> 02:18.420
Destroy and others.

02:18.460 --> 02:28.220
The solution here is the camera controller dot bind to lifecycle method connects the camera to the lifecycle.

02:28.220 --> 02:31.740
Camera automatically opens when the lifecycle starts.

02:31.740 --> 02:39.940
Resume closes when the lifecycle poses or stops and releases resource when destroyed.

02:39.980 --> 02:42.660
Okay, so this is a very important note.

02:42.660 --> 02:44.220
You should pay attention to it.

02:44.260 --> 02:50.620
When the lifecycle owner changes, the configuration changes, fragment transitions or any other recomposition

02:50.620 --> 02:56.500
ensures the camera is always bound to the correct current life cycle.

02:56.620 --> 03:05.900
Without lifecycle binding, camera stays open when app goes to background battery drain camera resource

03:05.940 --> 03:06.580
leaks.

03:06.740 --> 03:10.780
App may crash when trying to use an available camera.

03:11.020 --> 03:13.820
Other apps can't access the camera and so on.

03:14.140 --> 03:22.020
So there are a lot of problems you should prevent using the bind to your lifecycle method.

03:22.060 --> 03:26.300
Then the last thing is disposable effect.

03:26.340 --> 03:27.540
Pass the unit.

03:27.580 --> 03:33.180
And here on dispose camera controller dot not unbind.

03:33.220 --> 03:37.540
We need to clear image analysis analyzer.

03:37.580 --> 03:41.940
We need to free up the memory and quit the camera controller.

03:41.980 --> 03:46.860
Also we need to set a box with detection overlay.

03:46.900 --> 03:52.460
This is the Composables that we created before the box with detection overlay.

03:52.820 --> 03:58.460
To set a box over the detected results and the detected objects.

03:58.500 --> 04:08.420
Okay, so this is our camera screen containing many objects and many launched effects and disposable

04:08.620 --> 04:09.340
effect.
