Skip to content

[WIP] [android_camera_camerax] Fix incorrect camera mirroring for front cameras on devices using ImageReader Impeller backend #9233

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 2 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
5 changes: 5 additions & 0 deletions packages/camera/camera_android_camerax/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.6.17

* Fixes incorrect camera preview mirroring for front cameras of devices using the `ImageReader`
Impeller backend.

## 0.6.16

* Fixes incorrect camera preview rotation for landscape-oriented devices.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,27 @@ final class _ImageReaderRotatedPreviewState
sign: widget.facingSign,
);

// If the camera is front facing, mirror the camera preview
// according to the current device orientation.
Widget cameraPreview = widget.child;
if (widget.facingSign == 1) {
if (deviceOrientation == DeviceOrientation.portraitDown ||
deviceOrientation == DeviceOrientation.portraitUp) {
cameraPreview = Transform.scale(
scaleY: -1,
child: cameraPreview,
);
} else {
cameraPreview = Transform.scale(
scaleX: -1,
child: cameraPreview,
);
}
}

return RotatedBox(
quarterTurns: rotationDegrees ~/ 90,
child: widget.child,
child: cameraPreview,
);
} else {
return const SizedBox.shrink();
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.16
version: 0.6.17

environment:
sdk: ^3.6.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:camera_android_camerax/src/camerax_library.dart';
import 'package:camera_android_camerax/src/camerax_proxy.dart';
import 'package:camera_platform_interface/camera_platform_interface.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart' show RotatedBox, Texture;
import 'package:flutter/widgets.dart' show RotatedBox, Texture, Transform;
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';

Expand Down Expand Up @@ -1031,8 +1031,13 @@ void main() {
final RotatedBox rotatedBox =
tester.widget<RotatedBox>(find.byType(RotatedBox));

expect(rotatedBox.child, isA<Texture>());
expect((rotatedBox.child! as Texture).textureId, cameraId);
// We expect a Transform widget to wrap the RotatedBox with the camera
// preview to mirror the preview, since the front camera is being
// used.
expect(rotatedBox.child, isA<Transform>());

final Transform tranformedPreview = rotatedBox.child! as Transform;
expect((tranformedPreview.child! as Texture).textureId, cameraId);
expect(
rotatedBox.quarterTurns,
expectedQuarterTurns,
Expand Down Expand Up @@ -1077,8 +1082,14 @@ void main() {
const int expectedQuarterTurns = _180DegreesClockwise;
final RotatedBox rotatedBox =
tester.widget<RotatedBox>(find.byType(RotatedBox));
expect(rotatedBox.child, isA<Texture>());
expect((rotatedBox.child! as Texture).textureId, cameraId);

// We expect a Transform widget to wrap the RotatedBox with the camera
// preview to mirror the preview, since the front camera is being
// used.
expect(rotatedBox.child, isA<Transform>());

final Transform tranformedPreview = rotatedBox.child! as Transform;
expect((tranformedPreview.child! as Texture).textureId, cameraId);
expect(
rotatedBox.quarterTurns,
expectedQuarterTurns,
Expand Down Expand Up @@ -1122,8 +1133,14 @@ void main() {
const int expectedQuarterTurns = _90DegreesClockwise;
final RotatedBox rotatedBox =
tester.widget<RotatedBox>(find.byType(RotatedBox));
expect(rotatedBox.child, isA<Texture>());
expect((rotatedBox.child! as Texture).textureId, cameraId);

// We expect a Transform widget to wrap the RotatedBox with the camera
// preview to mirror the preview, since the front camera is being
// used.
expect(rotatedBox.child, isA<Transform>());

final Transform tranformedPreview = rotatedBox.child! as Transform;
expect((tranformedPreview.child! as Texture).textureId, cameraId);
expect(
rotatedBox.quarterTurns,
expectedQuarterTurns,
Expand Down Expand Up @@ -1168,8 +1185,14 @@ void main() {
const int expectedQuarterTurns = _0DegreesClockwise;
final RotatedBox rotatedBox =
tester.widget<RotatedBox>(find.byType(RotatedBox));
expect(rotatedBox.child, isA<Texture>());
expect((rotatedBox.child! as Texture).textureId, cameraId);

// We expect a Transform widget to wrap the RotatedBox with the camera
// preview to mirror the preview, since the front camera is being
// used.
expect(rotatedBox.child, isA<Transform>());

final Transform tranformedPreview = rotatedBox.child! as Transform;
expect((tranformedPreview.child! as Texture).textureId, cameraId);
expect(rotatedBox.quarterTurns, expectedQuarterTurns,
reason: getExpectedRotationTestFailureReason(
expectedQuarterTurns, rotatedBox.quarterTurns));
Expand Down Expand Up @@ -1245,8 +1268,13 @@ void main() {
final RotatedBox rotatedBox =
tester.widget<RotatedBox>(find.byType(RotatedBox));

expect(rotatedBox.child, isA<Texture>());
expect((rotatedBox.child! as Texture).textureId, cameraId);
// We expect a Transform widget to wrap the RotatedBox with the camera
// preview to mirror the preview, since the front camera is being
// used.
expect(rotatedBox.child, isA<Transform>());

final Transform tranformedPreview = rotatedBox.child! as Transform;
expect((tranformedPreview.child! as Texture).textureId, cameraId);
expect(rotatedBox.quarterTurns, expectedQuarterTurns,
reason: getExpectedRotationTestFailureReason(
expectedQuarterTurns, rotatedBox.quarterTurns));
Expand Down Expand Up @@ -1283,9 +1311,16 @@ void main() {
const int expectedQuarterTurns = _90DegreesClockwise;
final RotatedBox rotatedBox =
tester.widget<RotatedBox>(find.byType(RotatedBox));

// We expect a Transform widget to wrap the RotatedBox with the camera
// preview to mirror the preview, since the front camera is being
// used.
expect(rotatedBox.child, isA<Transform>());

final Transform tranformedPreview = rotatedBox.child! as Transform;
expect((tranformedPreview.child! as Texture).textureId, cameraId);

final int clockwiseQuarterTurns = rotatedBox.quarterTurns + 4;
expect(rotatedBox.child, isA<Texture>());
expect((rotatedBox.child! as Texture).textureId, cameraId);
expect(clockwiseQuarterTurns, expectedQuarterTurns,
reason: getExpectedRotationTestFailureReason(
expectedQuarterTurns, rotatedBox.quarterTurns));
Expand Down Expand Up @@ -1320,9 +1355,16 @@ void main() {
const int expectedQuarterTurns = _180DegreesClockwise;
final RotatedBox rotatedBox =
tester.widget<RotatedBox>(find.byType(RotatedBox));

// We expect a Transform widget to wrap the RotatedBox with the camera
// preview to mirror the preview, since the front camera is being
// used.
expect(rotatedBox.child, isA<Transform>());

final Transform tranformedPreview = rotatedBox.child! as Transform;
expect((tranformedPreview.child! as Texture).textureId, cameraId);

final int clockwiseQuarterTurns = rotatedBox.quarterTurns + 4;
expect(rotatedBox.child, isA<Texture>());
expect((rotatedBox.child! as Texture).textureId, cameraId);
expect(clockwiseQuarterTurns, expectedQuarterTurns,
reason: getExpectedRotationTestFailureReason(
expectedQuarterTurns, rotatedBox.quarterTurns));
Expand Down Expand Up @@ -1359,9 +1401,16 @@ void main() {
const int expectedQuarterTurns = _270DegreesClockwise;
final RotatedBox rotatedBox =
tester.widget<RotatedBox>(find.byType(RotatedBox));

// We expect a Transform widget to wrap the RotatedBox with the camera
// preview to mirror the preview, since the front camera is being
// used.
expect(rotatedBox.child, isA<Transform>());

final Transform tranformedPreview = rotatedBox.child! as Transform;
expect((tranformedPreview.child! as Texture).textureId, cameraId);

final int clockwiseQuarterTurns = rotatedBox.quarterTurns + 4;
expect(rotatedBox.child, isA<Texture>());
expect((rotatedBox.child! as Texture).textureId, cameraId);
expect(clockwiseQuarterTurns, expectedQuarterTurns,
reason: getExpectedRotationTestFailureReason(
expectedQuarterTurns, rotatedBox.quarterTurns));
Expand Down Expand Up @@ -1449,11 +1498,18 @@ void main() {
currentDefaultDisplayRotation]!;
final RotatedBox rotatedBox =
tester.widget<RotatedBox>(find.byType(RotatedBox));

// We expect a Transform widget to wrap the RotatedBox with the camera
// preview to mirror the preview, since the front camera is being
// used.
expect(rotatedBox.child, isA<Transform>());

final Transform tranformedPreview = rotatedBox.child! as Transform;
expect((tranformedPreview.child! as Texture).textureId, cameraId);

final int clockwiseQuarterTurns = rotatedBox.quarterTurns < 0
? rotatedBox.quarterTurns + 4
: rotatedBox.quarterTurns;
expect(rotatedBox.child, isA<Texture>());
expect((rotatedBox.child! as Texture).textureId, cameraId);
expect(clockwiseQuarterTurns, expectedQuarterTurns,
reason:
'When the default display rotation is $currentDefaultDisplayRotation, expected the preview to be rotated by $expectedQuarterTurns quarter turns (which is ${expectedQuarterTurns * 90} degrees clockwise) but instead was rotated ${rotatedBox.quarterTurns} quarter turns.');
Expand Down Expand Up @@ -1539,11 +1595,17 @@ void main() {
final RotatedBox rotatedBox = tester.widget<RotatedBox>(
find.byType(RotatedBox),
);

// We expect a Transform widget to wrap the RotatedBox with the camera
// preview to mirror the preview, since the front camera is being
// used.
expect(rotatedBox.child, isA<Transform>());

final Transform tranformedPreview = rotatedBox.child! as Transform;
expect((tranformedPreview.child! as Texture).textureId, cameraId);
final int clockwiseQuarterTurns = rotatedBox.quarterTurns < 0
? rotatedBox.quarterTurns + 4
: rotatedBox.quarterTurns;
expect(rotatedBox.child, isA<Texture>());
expect((rotatedBox.child! as Texture).textureId, cameraId);
expect(clockwiseQuarterTurns, expectedQuarterTurns,
reason:
'When the device orientation is $currentDeviceOrientation, expected the preview to be rotated by $expectedQuarterTurns quarter turns (which is ${expectedQuarterTurns * 90} degrees clockwise) but instead was rotated ${rotatedBox.quarterTurns} quarter turns.');
Expand Down Expand Up @@ -1760,8 +1822,14 @@ void main() {
const int expectedQuarterTurns = _90DegreesClockwise;
final RotatedBox rotatedBox =
tester.widget<RotatedBox>(find.byType(RotatedBox));
expect(rotatedBox.child, isA<Texture>());
expect((rotatedBox.child! as Texture).textureId, cameraId);

// We expect a Transform widget to wrap the RotatedBox with the camera
// preview to mirror the preview, since the front camera is being
// used.
expect(rotatedBox.child, isA<Transform>());

final Transform tranformedPreview = rotatedBox.child! as Transform;
expect((tranformedPreview.child! as Texture).textureId, cameraId);
expect(rotatedBox.quarterTurns, expectedQuarterTurns,
reason: getExpectedRotationTestFailureReason(
expectedQuarterTurns, rotatedBox.quarterTurns));
Expand Down