Skip to content

Fix Camera Switching Bug #9005

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions packages/camera/camera/example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ android {
namespace 'io.flutter.plugins.cameraexample'
compileSdk flutter.compileSdkVersion

compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}

kotlinOptions {
jvmTarget = JavaVersion.VERSION_17.toString()
}

defaultConfig {
applicationId "io.flutter.plugins.cameraexample"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ public Long getSensorRotationDegrees(@NonNull Long identifier) {
return Long.valueOf(cameraInfo.getSensorRotationDegrees());
}

/**
* Returns the lens facing of the {@link androidx.camera.core.Camera} that is
* represented by the {@link CameraInfo} with the specified identifier.
*/
@Override
@NonNull
public Long getLensFacing(@NonNull Long identifier) {
CameraInfo cameraInfo =
(CameraInfo) Objects.requireNonNull(instanceManager.getInstance(identifier));
return Long.valueOf(cameraInfo.getLensFacing());

/**
* Retrieves the {@link LiveData} of the {@link CameraState} that is tied to the {@link
* androidx.camera.core.Camera} that is represented by the {@link CameraInfo} with the specified
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -262,25 +262,26 @@ class AndroidCameraCameraX extends CameraPlatform {
await processCameraProvider!.getAvailableCameraInfos();

CameraLensDirection? cameraLensDirection;
int? cameraLensDirectionNum;
int cameraCount = 0;
int? cameraSensorOrientation;
String? cameraName;

for (final CameraInfo cameraInfo in cameraInfos) {

cameraLensDirectionNum = await cameraInfo.getLensFacing();
// Determine the lens direction by filtering the CameraInfo
// TODO(gmackall): replace this with call to CameraInfo.getLensFacing when changes containing that method are available
if ((await proxy
.createCameraSelector(CameraSelector.lensFacingBack)
.filter(<CameraInfo>[cameraInfo]))
.isNotEmpty) {

// CameraSelector.LENS_FACING_FRONT;

if (cameraLensDirectionNum == 1) {
cameraLensDirection = CameraLensDirection.back;
} else if ((await proxy
.createCameraSelector(CameraSelector.lensFacingFront)
.filter(<CameraInfo>[cameraInfo]))
.isNotEmpty) {
}
else if (cameraLensDirectionNum == 0) {
cameraLensDirection = CameraLensDirection.front;
} else {
//Skip this CameraInfo as its lens direction is unknown
}
else {
continue;
}

Expand Down
12 changes: 12 additions & 0 deletions packages/camera/camera_android_camerax/lib/src/camera_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class CameraInfo extends JavaObject {
Future<int> getSensorRotationDegrees() =>
_api.getSensorRotationDegreesFromInstance(this);

/// Gets lens facing of the camera.
Future<int> getLensFacing() =>
_api.getLensFacingFromInstance(this);

/// Starts listening for the camera closing.
Future<LiveData<CameraState>> getCameraState() =>
_api.getCameraStateFromInstance(this);
Expand Down Expand Up @@ -71,6 +75,14 @@ class _CameraInfoHostApiImpl extends CameraInfoHostApi {
return sensorRotationDegrees;
}

/// Gets the lens facing of the specified [CameraInfo] instance.
Future<int> getLensFacingFromInstance(
CameraInfo instance,
) async {
final int lensFacing = await getLensFacing(instanceManager.getIdentifier(instance)!);
return lensFacing;
}

/// Gets the [LiveData<CameraState>] that represents the state of the camera
/// to which the CameraInfo [instance] pertains.
Future<LiveData<CameraState>> getCameraStateFromInstance(
Expand Down
Loading