-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[camerax] Implement onCameraClosing #3878
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
6125eca
5be1371
b2fce3d
ed62a9a
2e3027b
fbd3fe5
dfa477e
a651b92
884fd6c
d5f8b91
060b7e5
ff50c70
fe6f1fc
e2643ae
b7194f7
dadc230
d15e1d4
6dc2b77
f826b62
0f7f29a
1e9739b
9ba20d0
5bf6b17
5e58b2c
4c8c94d
6a0de08
d3ed3e7
d1c684f
c04d8c1
b2322d2
3545aec
e1f5bdb
2d3409b
1f40f47
6c9b04e
6c6cbb4
a0ea3c5
06c1545
681a117
1143a03
d431511
ab3c085
631f972
70c817a
426b438
2bc4877
0fcf7c6
6c49bd9
b9daeb1
3345878
9f6646f
265eb1e
bfc4b5e
4f3c342
e3c7a5b
f341f7f
0d4b021
ca3bd9e
540ee98
debcd3a
a79ee62
438f592
c44fc06
83aea10
5f435e8
581af94
dec3d69
02c039f
ce463ba
c525162
86304c3
b826c18
2cebb18
04976ec
bca72ee
2da0b8f
ea78ab0
d0b160f
bc7ebee
9014d3b
f3d6a06
34cab98
6f701a5
a636525
d5a21cb
bc4ebc1
491ab2c
bff96a9
ad40999
a3667dd
c3732fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,16 +4,20 @@ | |
|
|
||
| import 'dart:async'; | ||
|
|
||
| import 'package:async/async.dart'; | ||
| import 'package:camera_platform_interface/camera_platform_interface.dart'; | ||
| import 'package:flutter/widgets.dart'; | ||
| import 'package:stream_transform/stream_transform.dart'; | ||
|
|
||
| import 'camera.dart'; | ||
| import 'camera_info.dart'; | ||
| import 'camera_selector.dart'; | ||
| import 'camera_state.dart'; | ||
| import 'camera_state_error.dart'; | ||
| import 'camerax_library.g.dart'; | ||
| import 'image_capture.dart'; | ||
| import 'live_camera_state.dart'; | ||
| import 'live_data.dart'; | ||
| import 'observer.dart'; | ||
| import 'preview.dart'; | ||
| import 'process_camera_provider.dart'; | ||
| import 'surface.dart'; | ||
|
|
@@ -37,7 +41,7 @@ class AndroidCameraCameraX extends CameraPlatform { | |
| Camera? camera; | ||
|
|
||
| /// The [LiveCameraState] that represents the state of the [camera] instance. | ||
| LiveCameraState? liveCameraState; | ||
| LiveData<CameraState>? liveCameraState; | ||
|
|
||
| /// The [Preview] instance that can be configured to present a live camera preview. | ||
| @visibleForTesting | ||
|
|
@@ -167,7 +171,7 @@ class AndroidCameraCameraX extends CameraPlatform { | |
| // instance as bound but not paused. | ||
| camera = await processCameraProvider! | ||
| .bindToLifecycle(cameraSelector!, <UseCase>[preview!, imageCapture!]); | ||
| _updateLiveCameraState(); | ||
| _updateLiveCameraState(flutterSurfaceTextureId); | ||
| _previewIsPaused = false; | ||
|
|
||
| return flutterSurfaceTextureId; | ||
|
|
@@ -228,6 +232,7 @@ class AndroidCameraCameraX extends CameraPlatform { | |
| @override | ||
| Future<void> dispose(int cameraId) async { | ||
| preview?.releaseFlutterSurfaceTexture(); | ||
| liveCameraState?.removeObservers(); | ||
| processCameraProvider?.unbindAll(); | ||
| } | ||
|
|
||
|
|
@@ -240,20 +245,20 @@ class AndroidCameraCameraX extends CameraPlatform { | |
| /// The camera started to close. | ||
| @override | ||
| Stream<CameraClosingEvent> onCameraClosing(int cameraId) { | ||
| return LiveCameraState.cameraClosingStreamController.stream | ||
| .map<CameraClosingEvent>((bool isCameraClosing) { | ||
| assert(isCameraClosing); | ||
| return CameraClosingEvent(cameraId); | ||
| }); | ||
| return _cameraEvents(cameraId).whereType<CameraClosingEvent>(); | ||
| } | ||
|
|
||
| /// The camera experienced an error. | ||
| @override | ||
| Stream<CameraErrorEvent> onCameraError(int cameraId) { | ||
| return SystemServices.cameraErrorStreamController.stream | ||
| .map<CameraErrorEvent>((String errorDescription) { | ||
| return CameraErrorEvent(cameraId, errorDescription); | ||
| }); | ||
| return StreamGroup.mergeBroadcast< | ||
| CameraErrorEvent>(<Stream<CameraErrorEvent>>[ | ||
| SystemServices.cameraErrorStreamController.stream | ||
| .map<CameraErrorEvent>((String errorDescription) { | ||
| return CameraErrorEvent(cameraId, errorDescription); | ||
| }), | ||
| _cameraEvents(cameraId).whereType<CameraErrorEvent>() | ||
| ]); | ||
| } | ||
|
|
||
| /// The ui orientation changed. | ||
|
|
@@ -276,15 +281,15 @@ class AndroidCameraCameraX extends CameraPlatform { | |
| /// [cameraId] not used. | ||
| @override | ||
| Future<void> resumePreview(int cameraId) async { | ||
| await _bindPreviewToLifecycle(); | ||
| await _bindPreviewToLifecycle(cameraId); | ||
| _previewIsPaused = false; | ||
| } | ||
|
|
||
| /// Returns a widget showing a live camera preview. | ||
| @override | ||
| Widget buildPreview(int cameraId) { | ||
| return FutureBuilder<void>( | ||
| future: _bindPreviewToLifecycle(), | ||
| future: _bindPreviewToLifecycle(cameraId), | ||
| builder: (BuildContext context, AsyncSnapshot<void> snapshot) { | ||
| switch (snapshot.connectionState) { | ||
| case ConnectionState.none: | ||
|
|
@@ -319,7 +324,7 @@ class AndroidCameraCameraX extends CameraPlatform { | |
|
|
||
| /// Binds [preview] instance to the camera lifecycle controlled by the | ||
| /// [processCameraProvider]. | ||
| Future<void> _bindPreviewToLifecycle() async { | ||
| Future<void> _bindPreviewToLifecycle(int cameraId) async { | ||
camsim99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| assert(processCameraProvider != null); | ||
| assert(cameraSelector != null); | ||
| assert(preview != null); | ||
|
|
@@ -332,7 +337,7 @@ class AndroidCameraCameraX extends CameraPlatform { | |
|
|
||
| camera = await processCameraProvider! | ||
| .bindToLifecycle(cameraSelector!, <UseCase>[preview!]); | ||
| _updateLiveCameraState(); | ||
| _updateLiveCameraState(cameraId); | ||
| } | ||
|
|
||
| /// Unbinds [preview] instance to camera lifecycle controlled by the | ||
|
|
@@ -350,12 +355,26 @@ class AndroidCameraCameraX extends CameraPlatform { | |
|
|
||
| // Methods concerning camera information: | ||
|
|
||
| /// Adds fresh observers to the [LiveCameraState] of the current [camera]. | ||
| Future<void> _updateLiveCameraState() async { | ||
| /// Adds fresh observers to the [LiveData] of the [CameraState] of the | ||
|
||
| /// current [camera]. | ||
| Future<void> _updateLiveCameraState(int cameraId) async { | ||
| final CameraInfo cameraInfo = await camera!.getCameraInfo(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non blocking. I still find this weird that we need to pass a cameraId (and use that name) when we have a reference to a camera object already and we dont use one to validate against the other. Is there an oppetunity to use more descriptive variable names here?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this gets at the bigger question as to why I'm on the fence about renaming However, I am on board for bettering documentation in the implementation around what it is (always referring to the Flutter surface texture ID/
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. flutter/flutter#126270 filed for improving documentation |
||
| liveCameraState?.removeObservers(); | ||
| liveCameraState = await cameraInfo.getLiveCameraState(); | ||
| liveCameraState!.addObserver(); | ||
| liveCameraState!.observe(_createCameraClosingObserver(cameraId)); | ||
| } | ||
|
|
||
| Observer<CameraState> _createCameraClosingObserver(int cameraId) { | ||
| return Observer<CameraState>(onChanged: (CameraState state) { | ||
| if (state.type == CameraStateType.closing) { | ||
| cameraEventStreamController.add(CameraClosingEvent(cameraId)); | ||
| } | ||
|
|
||
| if (state.error != null) { | ||
| cameraEventStreamController | ||
| .add(CameraErrorEvent(cameraId, state.error!.description)); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| // Methods for mapping Flutter camera constants to CameraX constants: | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.