@@ -7,24 +7,10 @@ import 'dart:math' show Point;
77
88import 'package:async/async.dart' ;
99import 'package:camera_platform_interface/camera_platform_interface.dart' ;
10- import 'package:flutter/foundation.dart' show Factory, Uint8List;
11- import 'package:flutter/gestures.dart' ;
12- import 'package:flutter/rendering.dart' ;
10+ import 'package:flutter/foundation.dart' show Uint8List;
1311import 'package:flutter/services.dart'
14- show
15- AndroidViewController,
16- DeviceOrientation,
17- PlatformException,
18- PlatformViewsService,
19- StandardMessageCodec;
20- import 'package:flutter/widgets.dart'
21- show
22- AndroidViewSurface,
23- PlatformViewLink,
24- TextDirection,
25- Texture,
26- Widget,
27- visibleForTesting;
12+ show DeviceOrientation, PlatformException;
13+ import 'package:flutter/widgets.dart' show Texture, Widget, visibleForTesting;
2814import 'package:stream_transform/stream_transform.dart' ;
2915import 'camerax_library.dart' ;
3016import 'rotated_preview_delegate.dart' ;
@@ -305,9 +291,6 @@ class AndroidCameraCameraX extends CameraPlatform {
305291 /// The configured format of outputted images from image streaming.
306292 int ? _imageAnalysisOutputImageFormat;
307293
308- // Platform view camera preview.
309- PreviewView ? _previewView;
310-
311294 /// Returns list of all available cameras and their descriptions.
312295 @override
313296 Future <List <CameraDescription >> availableCameras () async {
@@ -390,9 +373,6 @@ class AndroidCameraCameraX extends CameraPlatform {
390373 CameraDescription cameraDescription,
391374 MediaSettings ? mediaSettings,
392375 ) async {
393- print (
394- 'CAMILLE: CREATING CAMERA WITH LENS DIRECTION: ${cameraDescription .lensDirection }' ,
395- );
396376 enableRecordingAudio = mediaSettings? .enableAudio ?? false ;
397377 final CameraPermissionsError ? error = await systemServicesManager
398378 .requestCameraPermissions (enableRecordingAudio);
@@ -428,29 +408,16 @@ class AndroidCameraCameraX extends CameraPlatform {
428408
429409 // Retrieve a fresh ProcessCameraProvider instance.
430410 processCameraProvider ?? = await ProcessCameraProvider .getInstance ();
431- await processCameraProvider! .unbindAll ();
411+ unawaited ( processCameraProvider! .unbindAll () );
432412
433413 // Configure Preview instance.
434- // if (preview != null) {
435- // await preview!.setSurfaceProvider(null);
436- // }
437414 preview = Preview (
438415 resolutionSelector: _presetResolutionSelector,
439416 targetFpsRange: _targetFpsRange,
440417 );
441- // _flutterSurfaceTextureId = await preview!.setSurfaceProvider(
442- // systemServicesManager,
443- // );
444- if (_previewView == null ) {
445- _previewView = PreviewView ();
446- await _previewView! .registerPreviewView ();
447- }
448-
449- // TODO(camsim99): cache this value for additional speedup.
450- final SurfaceProvider surfaceProvider = await _previewView!
451- .getSurfaceProvider ();
452- await preview! .setSurfaceProvider (surfaceProvider);
453- _flutterSurfaceTextureId = 3 ;
418+ _flutterSurfaceTextureId = await preview! .setSurfaceProvider (
419+ systemServicesManager,
420+ );
454421
455422 // Configure ImageCapture instance.
456423 imageCapture = ImageCapture (
@@ -469,9 +436,8 @@ class AndroidCameraCameraX extends CameraPlatform {
469436 // Retrieve info required for correcting the rotation of the camera preview
470437 // if necessary.
471438 sensorOrientationDegrees = cameraDescription.sensorOrientation.toDouble ();
472- _handlesCropAndRotation = false ;
473- // _handlesCropAndRotation = await preview!
474- // .surfaceProducerHandlesCropAndRotation();
439+ _handlesCropAndRotation = await preview!
440+ .surfaceProducerHandlesCropAndRotation ();
475441 _initialDeviceOrientation = _deserializeDeviceOrientation (
476442 await deviceOrientationManager.getUiOrientation (),
477443 );
@@ -519,7 +485,6 @@ class AndroidCameraCameraX extends CameraPlatform {
519485 // Bind configured UseCases to ProcessCameraProvider instance & mark Preview
520486 // instance as bound but not paused. Video capture is bound at first use
521487 // instead of here.
522-
523488 camera = await processCameraProvider! .bindToLifecycle (
524489 cameraSelector! ,
525490 < UseCase > [preview! , imageCapture! , imageAnalysis! ],
@@ -532,11 +497,8 @@ class AndroidCameraCameraX extends CameraPlatform {
532497 // configured camera:
533498
534499 // Retrieve preview resolution.
535- ResolutionInfo ? previewResInfo = await preview! .getResolutionInfo ();
536- print (
537- 'CAMILLE: PREVIEW RESOLUTION INFO: ${previewResInfo !.resolution !.height } x ${previewResInfo !.resolution !.width }' ,
538- );
539- final ResolutionInfo previewResolutionInfo = previewResInfo! ;
500+ final ResolutionInfo previewResolutionInfo = (await preview!
501+ .getResolutionInfo ())! ;
540502
541503 // Mark auto-focus, auto-exposure and setting points for focus & exposure
542504 // as available operations as CameraX does its best across devices to
@@ -562,8 +524,7 @@ class AndroidCameraCameraX extends CameraPlatform {
562524 /// Releases the resources of the accessed camera with ID [cameraId] .
563525 @override
564526 Future <void > dispose (int cameraId) async {
565- // await preview?.releaseSurfaceProvider();
566- await preview? .setSurfaceProvider (null );
527+ await preview? .releaseSurfaceProvider ();
567528 await liveCameraState? .removeObservers ();
568529 await processCameraProvider? .unbindAll ();
569530 await imageAnalysis? .clearAnalyzer ();
@@ -1065,15 +1026,12 @@ class AndroidCameraCameraX extends CameraPlatform {
10651026 await _bindUseCaseToLifecycle (preview! , cameraId);
10661027 }
10671028
1068- PlatformViewLink ? platformView;
1069-
10701029 /// Returns a widget showing a live camera preview for the camera with ID [cameraId] .
10711030 ///
10721031 /// [createCamera] must be called before attempting to build this preview, and
10731032 /// [cameraId] can be retrieved from that call.
10741033 @override
10751034 Widget buildPreview (int cameraId) {
1076- print ("CAMILLE: rebuilding preview!" );
10771035 if (! previewInitiallyBound) {
10781036 // No camera has been created, and thus, the preview UseCase has not been
10791037 // bound to the camera lifecycle, restricting this preview from being
@@ -1084,53 +1042,22 @@ class AndroidCameraCameraX extends CameraPlatform {
10841042 );
10851043 }
10861044
1087- // This is used in the platform side to register the view.
1088- const viewType = 'plugins.flutter.dev/camera_android_camerax' ;
1089- // Pass parameters to the platform side.
1090- const creationParams = < String , dynamic > {};
1091-
1092- platformView ?? = PlatformViewLink (
1093- viewType: viewType,
1094- surfaceFactory: (context, controller) {
1095- return AndroidViewSurface (
1096- controller: controller as AndroidViewController ,
1097- gestureRecognizers: const < Factory <OneSequenceGestureRecognizer >> {},
1098- hitTestBehavior: PlatformViewHitTestBehavior .opaque,
1045+ final Stream <DeviceOrientation > deviceOrientationStream =
1046+ onDeviceOrientationChanged ().map (
1047+ (DeviceOrientationChangedEvent e) => e.orientation,
10991048 );
1100- },
1101- onCreatePlatformView: (params) {
1102- return PlatformViewsService .initSurfaceAndroidView (
1103- id: params.id,
1104- viewType: viewType,
1105- layoutDirection: TextDirection .ltr,
1106- creationParams: creationParams,
1107- creationParamsCodec: const StandardMessageCodec (),
1108- onFocus: () {
1109- params.onFocusChanged (true );
1110- },
1111- )
1112- ..addOnPlatformViewCreatedListener (params.onPlatformViewCreated)
1113- ..create ();
1114- },
1049+ final Widget preview = Texture (textureId: cameraId);
1050+
1051+ return RotatedPreviewDelegate (
1052+ handlesCropAndRotation: _handlesCropAndRotation,
1053+ initialDeviceOrientation: _initialDeviceOrientation,
1054+ initialDefaultDisplayRotation: _initialDefaultDisplayRotation,
1055+ deviceOrientationStream: deviceOrientationStream,
1056+ sensorOrientationDegrees: sensorOrientationDegrees,
1057+ cameraIsFrontFacing: cameraIsFrontFacing,
1058+ deviceOrientationManager: deviceOrientationManager,
1059+ child: preview,
11151060 );
1116-
1117- return platformView! ;
1118- // final Stream<DeviceOrientation> deviceOrientationStream =
1119- // onDeviceOrientationChanged().map(
1120- // (DeviceOrientationChangedEvent e) => e.orientation,
1121- // );
1122- // final Widget preview = Texture(textureId: cameraId);
1123-
1124- // return RotatedPreviewDelegate(
1125- // handlesCropAndRotation: _handlesCropAndRotation,
1126- // initialDeviceOrientation: _initialDeviceOrientation,
1127- // initialDefaultDisplayRotation: _initialDefaultDisplayRotation,
1128- // deviceOrientationStream: deviceOrientationStream,
1129- // sensorOrientationDegrees: sensorOrientationDegrees,
1130- // cameraIsFrontFacing: cameraIsFrontFacing,
1131- // deviceOrientationManager: deviceOrientationManager,
1132- // child: preview,
1133- // );
11341061 }
11351062
11361063 /// Captures an image using the camera with ID [cameraId] and returns the file where it was saved.
0 commit comments