Skip to content
Merged
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
4 changes: 4 additions & 0 deletions packages/camera/camera_android_camerax/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.6.29

* Modifies lens direction logic to request the value from CameraX directly versus manual detection.

## 0.6.28

* Adds more descriptive error to camera error stream when image capture fails.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
package io.flutter.plugins.camerax;

import androidx.annotation.NonNull;
import androidx.annotation.OptIn;
import androidx.camera.core.CameraInfo;
import androidx.camera.core.CameraSelector;
import androidx.camera.core.ExperimentalLensFacing;
import androidx.camera.core.ExposureState;

/**
Expand All @@ -23,6 +26,22 @@ public long sensorRotationDegrees(CameraInfo pigeonInstance) {
return pigeonInstance.getSensorRotationDegrees();
}

@Override
@OptIn(markerClass = ExperimentalLensFacing.class)
public LensFacing lensFacing(CameraInfo pigeonInstance) {
int lensFacing = pigeonInstance.getLensFacing();
switch (lensFacing) {
case CameraSelector.LENS_FACING_FRONT:
return LensFacing.FRONT;
case CameraSelector.LENS_FACING_BACK:
return LensFacing.BACK;
case CameraSelector.LENS_FACING_EXTERNAL:
return LensFacing.EXTERNAL;
default:
return LensFacing.UNKNOWN;
}
Comment on lines +32 to +42

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For improved conciseness and readability, you can use a Java switch expression here, as the project's Java version supports it. This modern language feature can make the code more streamlined.

    return switch (pigeonInstance.getLensFacing()) {
      case CameraSelector.LENS_FACING_FRONT -> LensFacing.FRONT;
      case CameraSelector.LENS_FACING_BACK -> LensFacing.BACK;
      case CameraSelector.LENS_FACING_EXTERNAL -> LensFacing.EXTERNAL;
      default -> LensFacing.UNKNOWN;
    };

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indifferent. Also I think our formatter is too outdated for this, so this would require an update to that tool. Tried this locally and I get errors.

}

@NonNull
@Override
public ExposureState exposureState(CameraInfo pigeonInstance) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2181,6 +2181,9 @@ abstract class PigeonApiCameraInfo(
*/
abstract fun sensorRotationDegrees(pigeon_instance: androidx.camera.core.CameraInfo): Long

/** Returns the lens facing of this camera. */
abstract fun lensFacing(pigeon_instance: androidx.camera.core.CameraInfo): LensFacing

/** Returns a ExposureState. */
abstract fun exposureState(
pigeon_instance: androidx.camera.core.CameraInfo
Expand Down Expand Up @@ -2263,23 +2266,26 @@ abstract class PigeonApiCameraInfo(
val pigeon_identifierArg =
pigeonRegistrar.instanceManager.addHostCreatedInstance(pigeon_instanceArg)
val sensorRotationDegreesArg = sensorRotationDegrees(pigeon_instanceArg)
val lensFacingArg = lensFacing(pigeon_instanceArg)
val exposureStateArg = exposureState(pigeon_instanceArg)
val binaryMessenger = pigeonRegistrar.binaryMessenger
val codec = pigeonRegistrar.codec
val channelName = "dev.flutter.pigeon.camera_android_camerax.CameraInfo.pigeon_newInstance"
val channel = BasicMessageChannel<Any?>(binaryMessenger, channelName, codec)
channel.send(listOf(pigeon_identifierArg, sensorRotationDegreesArg, exposureStateArg)) {
if (it is List<*>) {
if (it.size > 1) {
callback(
Result.failure(CameraXError(it[0] as String, it[1] as String, it[2] as String?)))
} else {
callback(Result.success(Unit))
channel.send(
listOf(pigeon_identifierArg, sensorRotationDegreesArg, lensFacingArg, exposureStateArg)) {
if (it is List<*>) {
if (it.size > 1) {
callback(
Result.failure(
CameraXError(it[0] as String, it[1] as String, it[2] as String?)))
} else {
callback(Result.success(Unit))
}
} else {
callback(Result.failure(CameraXLibraryPigeonUtils.createConnectionError(channelName)))
}
}
} else {
callback(Result.failure(CameraXLibraryPigeonUtils.createConnectionError(channelName)))
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@
import static org.mockito.Mockito.when;

import androidx.camera.core.CameraInfo;
import androidx.camera.core.CameraSelector;
import androidx.camera.core.CameraState;
import androidx.camera.core.ExposureState;
import androidx.camera.core.ZoomState;
import androidx.lifecycle.LiveData;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;

public class CameraInfoTest {
@Test
public void getSensorRotationDegrees_makesCallToRetrieveSensorRotationDegrees() {
public void sensorRotationDegrees_makesCallToRetrieveSensorRotationDegrees() {
final PigeonApiCameraInfo api = new TestProxyApiRegistrar().getPigeonApiCameraInfo();

final CameraInfo instance = mock(CameraInfo.class);
Expand All @@ -27,6 +30,41 @@ public void getSensorRotationDegrees_makesCallToRetrieveSensorRotationDegrees()
assertEquals(value, api.sensorRotationDegrees(instance));
}

@Test
public void lensFacing_makesCallToRetrieveLensFacing() {
final PigeonApiCameraInfo api = new TestProxyApiRegistrar().getPigeonApiCameraInfo();

final CameraInfo instance = mock(CameraInfo.class);
List<Integer> testedLensFacingValues =
Arrays.asList(
CameraSelector.LENS_FACING_BACK,
CameraSelector.LENS_FACING_FRONT,
CameraSelector.LENS_FACING_EXTERNAL,
CameraSelector.LENS_FACING_UNKNOWN);

for (int testedLensFacingValue : testedLensFacingValues) {
when(instance.getLensFacing()).thenReturn(testedLensFacingValue);

LensFacing expected;
switch (testedLensFacingValue) {
case CameraSelector.LENS_FACING_BACK:
expected = LensFacing.BACK;
break;
case CameraSelector.LENS_FACING_FRONT:
expected = LensFacing.FRONT;
break;
case CameraSelector.LENS_FACING_EXTERNAL:
expected = LensFacing.EXTERNAL;
break;
default:
expected = LensFacing.UNKNOWN;
break;
}
Comment on lines +48 to +62

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Similar to the implementation in CameraInfoProxyApi, you can use a Java switch expression here in the test to make the code more concise and readable. This also helps keep the test code style consistent with the implementation.

      LensFacing expected = 
          switch (testedLensFacingValue) {
            case CameraSelector.LENS_FACING_BACK -> LensFacing.BACK;
            case CameraSelector.LENS_FACING_FRONT -> LensFacing.FRONT;
            case CameraSelector.LENS_FACING_EXTERNAL -> LensFacing.EXTERNAL;
            default -> LensFacing.UNKNOWN;
          };


assertEquals(expected, api.lensFacing(instance));
}
}

@SuppressWarnings("unchecked")
@Test
public void getCameraState_makesCallToRetrieveLiveCameraState() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,19 +307,17 @@ class AndroidCameraCameraX extends CameraPlatform {
String? cameraName;

for (final cameraInfo in cameraInfos) {
// 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 CameraSelector(
requireLensFacing: LensFacing.back,
).filter(<CameraInfo>[cameraInfo])).isNotEmpty) {
cameraLensDirection = CameraLensDirection.back;
} else if ((await CameraSelector(
requireLensFacing: LensFacing.front,
).filter(<CameraInfo>[cameraInfo])).isNotEmpty) {
cameraLensDirection = CameraLensDirection.front;
} else {
//Skip this CameraInfo as its lens direction is unknown
continue;
final LensFacing lensFacing = cameraInfo.lensFacing;
switch (lensFacing) {
case LensFacing.front:
cameraLensDirection = CameraLensDirection.front;
case LensFacing.back:
cameraLensDirection = CameraLensDirection.back;
case LensFacing.external:
cameraLensDirection = CameraLensDirection.external;
case LensFacing.unknown:
// Skip this CameraInfo as its lens direction is unknown
continue;
}

cameraSensorOrientation = cameraInfo.sensorRotationDegrees;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,14 @@ void setUpGenerics({BinaryMessenger? pigeonBinaryMessenger}) {

camerax.CameraInfo.pigeon_setUpMessageHandlers(
pigeon_newInstance:
(int sensorRotationDegrees, camerax.ExposureState exposureState) {
(
int sensorRotationDegrees,
camerax.LensFacing lensFacing,
camerax.ExposureState exposureState,
) {
return CameraInfo.detached(
sensorRotationDegrees: sensorRotationDegrees,
lensFacing: lensFacing,
exposureState: exposureState,
pigeon_binaryMessenger: pigeonBinaryMessenger,
);
Expand Down Expand Up @@ -101,6 +106,7 @@ class CameraInfo extends camerax.CameraInfo {
/// create copies for an [PigeonInstanceManager].
CameraInfo.detached({
required super.sensorRotationDegrees,
required super.lensFacing,
required super.exposureState,
// ignore: non_constant_identifier_names
super.pigeon_binaryMessenger,
Expand All @@ -121,6 +127,7 @@ class CameraInfo extends camerax.CameraInfo {
CameraInfo pigeon_copy() {
return CameraInfo.detached(
sensorRotationDegrees: sensorRotationDegrees,
lensFacing: lensFacing,
exposureState: exposureState,
pigeon_binaryMessenger: pigeon_binaryMessenger,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2107,6 +2107,7 @@ class CameraInfo extends PigeonInternalProxyApiBaseClass {
super.pigeon_binaryMessenger,
super.pigeon_instanceManager,
required this.sensorRotationDegrees,
required this.lensFacing,
required this.exposureState,
});

Expand All @@ -2117,14 +2118,21 @@ class CameraInfo extends PigeonInternalProxyApiBaseClass {
/// (default) orientation.
final int sensorRotationDegrees;

/// Returns the lens facing of this camera.
final LensFacing lensFacing;

/// Returns a ExposureState.
final ExposureState exposureState;

static void pigeon_setUpMessageHandlers({
bool pigeon_clearHandlers = false,
BinaryMessenger? pigeon_binaryMessenger,
PigeonInstanceManager? pigeon_instanceManager,
CameraInfo Function(int sensorRotationDegrees, ExposureState exposureState)?
CameraInfo Function(
int sensorRotationDegrees,
LensFacing lensFacing,
ExposureState exposureState,
)?
pigeon_newInstance,
}) {
final _PigeonInternalProxyApiBaseCodec pigeonChannelCodec =
Expand Down Expand Up @@ -2157,7 +2165,12 @@ class CameraInfo extends PigeonInternalProxyApiBaseClass {
arg_sensorRotationDegrees != null,
'Argument for dev.flutter.pigeon.camera_android_camerax.CameraInfo.pigeon_newInstance was null, expected non-null int.',
);
final ExposureState? arg_exposureState = (args[2] as ExposureState?);
final LensFacing? arg_lensFacing = (args[2] as LensFacing?);
assert(
arg_lensFacing != null,
'Argument for dev.flutter.pigeon.camera_android_camerax.CameraInfo.pigeon_newInstance was null, expected non-null LensFacing.',
);
final ExposureState? arg_exposureState = (args[3] as ExposureState?);
assert(
arg_exposureState != null,
'Argument for dev.flutter.pigeon.camera_android_camerax.CameraInfo.pigeon_newInstance was null, expected non-null ExposureState.',
Expand All @@ -2167,12 +2180,14 @@ class CameraInfo extends PigeonInternalProxyApiBaseClass {
.addHostCreatedInstance(
pigeon_newInstance?.call(
arg_sensorRotationDegrees!,
arg_lensFacing!,
arg_exposureState!,
) ??
CameraInfo.pigeon_detached(
pigeon_binaryMessenger: pigeon_binaryMessenger,
pigeon_instanceManager: pigeon_instanceManager,
sensorRotationDegrees: arg_sensorRotationDegrees!,
lensFacing: arg_lensFacing!,
exposureState: arg_exposureState!,
),
arg_pigeon_instanceIdentifier!,
Expand Down Expand Up @@ -2264,6 +2279,7 @@ class CameraInfo extends PigeonInternalProxyApiBaseClass {
pigeon_binaryMessenger: pigeon_binaryMessenger,
pigeon_instanceManager: pigeon_instanceManager,
sensorRotationDegrees: sensorRotationDegrees,
lensFacing: lensFacing,
exposureState: exposureState,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ abstract class CameraInfo {
/// (default) orientation.
late int sensorRotationDegrees;

/// Returns the lens direction of this camera.
late LensFacing lensFacing;

/// Returns a ExposureState.
late ExposureState exposureState;

Expand Down
2 changes: 1 addition & 1 deletion packages/camera/camera_android_camerax/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: camera_android_camerax
description: Android implementation of the camera plugin using the CameraX library.
repository: https://github.com/flutter/packages/tree/main/packages/camera/camera_android_camerax
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22
version: 0.6.28
version: 0.6.29

environment:
sdk: ^3.9.0
Expand Down
Loading
Loading