Skip to content

Commit 631b962

Browse files
committed
more cleanup of source + example
1 parent b36cf24 commit 631b962

6 files changed

Lines changed: 2354 additions & 1968 deletions

File tree

packages/camera/camera_android_camerax/example/lib/camera_controller.dart

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -274,16 +274,12 @@ class CameraController extends ValueNotifier<CameraValue> {
274274
/// Initializes the camera on the device.
275275
///
276276
/// Throws a [CameraException] if the initialization fails.
277-
Future<void> initialize(VoidCallback? callback) =>
278-
_initializeWithDescription(description, callback);
277+
Future<void> initialize() => _initializeWithDescription(description);
279278

280279
/// Initializes the camera on the device with the specified description.
281280
///
282281
/// Throws a [CameraException] if the initialization fails.
283-
Future<void> _initializeWithDescription(
284-
CameraDescription description,
285-
VoidCallback? callback,
286-
) async {
282+
Future<void> _initializeWithDescription(CameraDescription description) async {
287283
if (_isDisposed) {
288284
throw CameraException(
289285
'Disposed CameraController',
@@ -309,7 +305,6 @@ class CameraController extends ValueNotifier<CameraValue> {
309305
CameraInitializedEvent event,
310306
) {
311307
initializeCompleter.complete(event);
312-
callback?.call();
313308
}),
314309
);
315310

@@ -623,7 +618,7 @@ class CameraController extends ValueNotifier<CameraValue> {
623618
await CameraPlatform.instance.setDescriptionWhileRecording(description);
624619
value = value.copyWith(description: description);
625620
} else {
626-
await _initializeWithDescription(description, null);
621+
await _initializeWithDescription(description);
627622
}
628623
}
629624

packages/camera/camera_android_camerax/example/lib/camera_preview.dart

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,13 @@ class CameraPreview extends StatelessWidget {
2626
valueListenable: controller,
2727
builder: (BuildContext context, Object? value, Widget? child) {
2828
return AspectRatio(
29-
aspectRatio: controller.value.aspectRatio,
30-
// aspectRatio: _isLandscape()
31-
// ? controller.value.aspectRatio
32-
// : (1 / controller.value.aspectRatio),
29+
aspectRatio: _isLandscape()
30+
? controller.value.aspectRatio
31+
: (1 / controller.value.aspectRatio),
3332
child: Stack(
3433
fit: StackFit.expand,
3534
children: <Widget>[
36-
// _wrapInRotatedBox(child: controller.buildPreview()),
37-
controller.buildPreview(),
35+
_wrapInRotatedBox(child: controller.buildPreview()),
3836
child ?? Container(),
3937
],
4038
),

packages/camera/camera_android_camerax/example/lib/main.dart

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -628,21 +628,16 @@ class _CameraExampleHomeState extends State<CameraExampleHome>
628628
}
629629

630630
Future<void> onNewCameraSelected(CameraDescription cameraDescription) async {
631-
if (controller != null && controller!.value.isRecordingVideo) {
631+
if (controller != null) {
632632
return controller!.setDescription(cameraDescription);
633+
} else {
634+
return _initializeCameraController(cameraDescription);
633635
}
634-
return _initializeCameraController(cameraDescription);
635636
}
636637

637638
Future<void> _initializeCameraController(
638639
CameraDescription cameraDescription,
639640
) async {
640-
print(
641-
'CAMILLE: EXAMPLE APP INITIALIZING CAMERA CONTROLLER WITH LENS DIRECTION: ${cameraDescription.lensDirection}',
642-
);
643-
print(
644-
'CAMILLE: CONTROLLER VALUE: ${controller?.description.lensDirection}',
645-
);
646641
final cameraController = CameraController(
647642
cameraDescription,
648643
mediaSettings: MediaSettings(
@@ -670,10 +665,7 @@ class _CameraExampleHomeState extends State<CameraExampleHome>
670665
});
671666

672667
try {
673-
await cameraController.initialize(() {
674-
print('CAMILLE: Calling set state');
675-
setState(() {});
676-
});
668+
await cameraController.initialize();
677669
await Future.wait(<Future<Object?>>[
678670
// The exposure mode is currently not supported on the web.
679671
...!kIsWeb
@@ -891,7 +883,7 @@ class _CameraExampleHomeState extends State<CameraExampleHome>
891883
}
892884

893885
try {
894-
await cameraController.startVideoRecording(onAvailable: (_) {});
886+
await cameraController.startVideoRecording();
895887
} on CameraException catch (e) {
896888
_showCameraException(e);
897889
return;

packages/camera/camera_android_camerax/lib/src/android_camera_camerax.dart

Lines changed: 26 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,10 @@ import 'dart:math' show Point;
77

88
import 'package:async/async.dart';
99
import '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;
1311
import '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;
2814
import 'package:stream_transform/stream_transform.dart';
2915
import 'camerax_library.dart';
3016
import '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

Comments
 (0)