-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[camerax] Implement
lockCaptureOrientation
& `unlockCaptureOrientat…
…ion` (#5285) Implements `lockCaptureOrientation` & `unlockCaptureOrientation` for all camera `UseCase`s. Also fixes small bug concerning not initially setting the target rotation of the `UseCase`s to the requested sensor orientation when `createCamera` is called. Fixes flutter/flutter#125915.
- Loading branch information
Showing
50 changed files
with
2,071 additions
and
810 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...roid/src/main/java/io/flutter/plugins/camerax/DeviceOrientationManagerFlutterApiImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package io.flutter.plugins.camerax; | ||
|
||
import androidx.annotation.NonNull; | ||
import io.flutter.plugin.common.BinaryMessenger; | ||
import io.flutter.plugins.camerax.GeneratedCameraXLibrary.DeviceOrientationManagerFlutterApi; | ||
|
||
public class DeviceOrientationManagerFlutterApiImpl extends DeviceOrientationManagerFlutterApi { | ||
public DeviceOrientationManagerFlutterApiImpl(@NonNull BinaryMessenger binaryMessenger) { | ||
super(binaryMessenger); | ||
} | ||
|
||
public void sendDeviceOrientationChangedEvent( | ||
@NonNull String orientation, @NonNull Reply<Void> reply) { | ||
super.onDeviceOrientationChanged(orientation, reply); | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
...android/src/main/java/io/flutter/plugins/camerax/DeviceOrientationManagerHostApiImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package io.flutter.plugins.camerax; | ||
|
||
import android.app.Activity; | ||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
import androidx.annotation.VisibleForTesting; | ||
import io.flutter.embedding.engine.systemchannels.PlatformChannel.DeviceOrientation; | ||
import io.flutter.plugin.common.BinaryMessenger; | ||
import io.flutter.plugins.camerax.CameraPermissionsManager.PermissionsRegistry; | ||
import io.flutter.plugins.camerax.GeneratedCameraXLibrary.DeviceOrientationManagerHostApi; | ||
|
||
public class DeviceOrientationManagerHostApiImpl implements DeviceOrientationManagerHostApi { | ||
private final BinaryMessenger binaryMessenger; | ||
private final InstanceManager instanceManager; | ||
|
||
@VisibleForTesting public @NonNull CameraXProxy cameraXProxy = new CameraXProxy(); | ||
@VisibleForTesting public @Nullable DeviceOrientationManager deviceOrientationManager; | ||
|
||
@VisibleForTesting | ||
public @NonNull DeviceOrientationManagerFlutterApiImpl deviceOrientationManagerFlutterApiImpl; | ||
|
||
private Activity activity; | ||
private PermissionsRegistry permissionsRegistry; | ||
|
||
public DeviceOrientationManagerHostApiImpl( | ||
@NonNull BinaryMessenger binaryMessenger, @NonNull InstanceManager instanceManager) { | ||
this.binaryMessenger = binaryMessenger; | ||
this.instanceManager = instanceManager; | ||
this.deviceOrientationManagerFlutterApiImpl = | ||
new DeviceOrientationManagerFlutterApiImpl(binaryMessenger); | ||
} | ||
|
||
public void setActivity(@NonNull Activity activity) { | ||
this.activity = activity; | ||
} | ||
|
||
/** | ||
* Starts listening for device orientation changes using an instance of a {@link | ||
* DeviceOrientationManager}. | ||
* | ||
* <p>Whenever a change in device orientation is detected by the {@code DeviceOrientationManager}, | ||
* the {@link SystemServicesFlutterApi} will be used to notify the Dart side. | ||
*/ | ||
@Override | ||
public void startListeningForDeviceOrientationChange( | ||
@NonNull Boolean isFrontFacing, @NonNull Long sensorOrientation) { | ||
deviceOrientationManager = | ||
cameraXProxy.createDeviceOrientationManager( | ||
activity, | ||
isFrontFacing, | ||
sensorOrientation.intValue(), | ||
(DeviceOrientation newOrientation) -> { | ||
deviceOrientationManagerFlutterApiImpl.sendDeviceOrientationChangedEvent( | ||
serializeDeviceOrientation(newOrientation), reply -> {}); | ||
}); | ||
deviceOrientationManager.start(); | ||
} | ||
|
||
/** Serializes {@code DeviceOrientation} into a String that the Dart side is able to recognize. */ | ||
String serializeDeviceOrientation(DeviceOrientation orientation) { | ||
return orientation.toString(); | ||
} | ||
|
||
/** | ||
* Tells the {@code deviceOrientationManager} to stop listening for orientation updates. | ||
* | ||
* <p>Has no effect if the {@code deviceOrientationManager} was never created to listen for device | ||
* orientation updates. | ||
*/ | ||
@Override | ||
public void stopListeningForDeviceOrientationChange() { | ||
if (deviceOrientationManager != null) { | ||
deviceOrientationManager.stop(); | ||
} | ||
} | ||
|
||
/** | ||
* Gets default capture rotation for CameraX {@code UseCase}s. | ||
* | ||
* <p>The default capture rotation for CameraX is the rotation of default {@code Display} at the | ||
* time that a {@code UseCase} is bound, but the default {@code Display} does not change in this | ||
* plugin, so this value is {@code Display}-agnostic. | ||
* | ||
* <p>See | ||
* https://developer.android.com/reference/androidx/camera/core/ImageCapture#setTargetRotation(int) | ||
* for instance for more information on how this default value is used. | ||
*/ | ||
@Override | ||
public @NonNull Long getDefaultDisplayRotation() { | ||
int defaultRotation; | ||
try { | ||
defaultRotation = deviceOrientationManager.getDefaultRotation(); | ||
} catch (NullPointerException e) { | ||
throw new IllegalStateException( | ||
"startListeningForDeviceOrientationChange must first be called to subscribe to device orientation changes in order to retrieve default rotation."); | ||
} | ||
|
||
return Long.valueOf(defaultRotation); | ||
} | ||
} |
Oops, something went wrong.