Skip to content
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

🚀 Use sensors to determine capture orientation #218

Merged
merged 9 commits into from
Oct 30, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
⚡️ Throttle harder
  • Loading branch information
AlexV525 committed Oct 27, 2023
commit 7c9849f4cc0183a4c1ddc4c6efa4eb182aedaeb2
18 changes: 13 additions & 5 deletions lib/src/states/camera_picker_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ class CameraPickerState extends State<CameraPicker>
/// Subscribe to the accelerometer.
late final StreamSubscription<AccelerometerEvent> accelerometerSubscription;

/// The locked capture orientation of the current camera instance.
DeviceOrientation? lockedCaptureOrientation;

@override
void initState() {
super.initState();
Expand Down Expand Up @@ -332,6 +335,7 @@ class CameraPickerState extends State<CameraPicker>
lastExposurePoint.value = null;
currentExposureOffset.value = 0;
currentExposureSliderOffset.value = 0;
lockedCaptureOrientation = pickerConfig.lockCaptureOrientation;
});
// **IMPORTANT**: Push methods into a post frame callback, which ensures the
// controller has already unbind from widgets.
Expand Down Expand Up @@ -481,11 +485,14 @@ class CameraPickerState extends State<CameraPicker>
void handleAccelerometerEvent(AccelerometerEvent event) {
if (pickerConfig.lockCaptureOrientation != null ||
innerController == null ||
isControllerBusy) {
!controller.value.isInitialized ||
controller.value.isPreviewPaused ||
controller.value.isRecordingVideo ||
controller.value.isTakingPicture) {
return;
}
final x = event.x, y = event.y, z = event.z;
realDebugPrint('X:$x Y:$y Z:$z');
// realDebugPrint('X:$x Y:$y Z:$z');
final bool isLeft;
if (x > 0) {
isLeft = Platform.isAndroid ? true : false;
Expand All @@ -498,10 +505,10 @@ class CameraPickerState extends State<CameraPicker>
final DeviceOrientation newOrientation;
if (z < 9) {
if (y > 5) {
realDebugPrint('Accelerometer portrait');
// realDebugPrint('Accelerometer portrait');
newOrientation = DeviceOrientation.portraitUp;
} else if (y < 5) {
realDebugPrint('Accelerometer landscape');
// realDebugPrint('Accelerometer landscape');
newOrientation = isLeft
? DeviceOrientation.landscapeLeft
: DeviceOrientation.landscapeRight;
Expand All @@ -513,7 +520,8 @@ class CameraPickerState extends State<CameraPicker>
newOrientation = DeviceOrientation.portraitUp;
}
// Throttle.
if (controller.value.lockedCaptureOrientation != newOrientation) {
if (lockedCaptureOrientation != newOrientation) {
lockedCaptureOrientation = newOrientation;
controller.lockCaptureOrientation(newOrientation);
}
}
Expand Down
Loading