Skip to content

[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

Merged
merged 15 commits into from
Jun 12, 2025
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.18+3

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

## 0.6.18+2

* Fixes premature garbage collection of native objects when app is under memory pressure.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,22 @@ final class _ImageReaderRotatedPreviewState
sign: widget.facingSign,
);

// If the camera is front facing (widget.facingSign is 1 for front
// cameras, -1 for back cameras), mirror the camera preview
// according to the current device orientation.
Widget cameraPreview = widget.child;
if (widget.facingSign == 1) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is == 1 the correct comparison here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah because facingSign maps to the sign in https://developer.android.com/media/camera/camera2/camera-preview#orientation_calculation, which is 1 for front cameras, -1 for back facing cameras. I'll add a comment to the code!

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.18+2
version: 0.6.18+3

environment:
sdk: ^3.7.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ 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 MatrixUtils, RotatedBox, Texture, Transform;
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';

Expand Down Expand Up @@ -380,6 +381,70 @@ void main() {
) =>
'Expected the preview to be rotated by $expectedQuarterTurns quarter turns (which is ${expectedQuarterTurns * 90} degrees clockwise) but instead was rotated $actualQuarterTurns quarter turns.';

/// Checks that the transform matrix (Matrix4) mirrors across the x-axis by
/// confirming the following to be the transformation matrix:
/// [[-1.0, 0.0, 0.0, 0.0],
/// [ 0.0, 1.0, 0.0, 0.0],
/// [ 0.0, 0.0, 1.0, 0.0],
/// [ 0.0, 0.0, 0.0, 1.0]]
void checkXAxisIsMirrored(Matrix4 transformationMatrix) {
final Matrix4 mirrorAcrossXMatrix = Matrix4(
-1.0,
0.0,
0.0,
0.0,
0.0,
1.0,
0.0,
0.0,
0.0,
0.0,
1.0,
0.0,
0.0,
0.0,
0.0,
1.0,
);

expect(
MatrixUtils.matrixEquals(mirrorAcrossXMatrix, transformationMatrix),
isTrue,
);
}

/// Checks that the transform matrix (Matrix4) mirrors across the y-axis by
/// confirming the following to be the transformation matrix:
/// [[1.0, 0.0, 0.0, 0.0],
/// [ 0.0, -1.0, 0.0, 0.0],
/// [ 0.0, 0.0, 1.0, 0.0],
/// [ 0.0, 0.0, 0.0, 1.0]]
void checkYAxisIsMirrored(Matrix4 transformationMatrix) {
final Matrix4 mirrorAcrossYMatrix = Matrix4(
1.0,
0.0,
0.0,
0.0,
0.0,
-1.0,
0.0,
0.0,
0.0,
0.0,
1.0,
0.0,
0.0,
0.0,
0.0,
1.0,
);

expect(
MatrixUtils.matrixEquals(mirrorAcrossYMatrix, transformationMatrix),
isTrue,
);
}

group('when handlesCropAndRotation is true', () {
// Test that preview rotation responds to initial default display rotation:
group('initial device orientation is landscapeRight,', () {
Expand Down Expand Up @@ -1167,8 +1232,19 @@ void main() {
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 transformedPreview = rotatedBox.child! as Transform;
final Matrix4 transformedPreviewMatrix =
transformedPreview.transform;

// Since the front camera is in portrait mode, we expect the camera
// preview to be mirrored across the y-axis.
checkYAxisIsMirrored(transformedPreviewMatrix);
expect((transformedPreview.child! as Texture).textureId, cameraId);
expect(
rotatedBox.quarterTurns,
expectedQuarterTurns,
Expand Down Expand Up @@ -1216,8 +1292,20 @@ 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 transformedPreview = rotatedBox.child! as Transform;
final Matrix4 transformedPreviewMatrix =
transformedPreview.transform;

// Since the front camera is in landscape mode, we expect the camera
// preview to be mirrored across the x-axis.
checkXAxisIsMirrored(transformedPreviewMatrix);
expect((transformedPreview.child! as Texture).textureId, cameraId);
expect(
rotatedBox.quarterTurns,
expectedQuarterTurns,
Expand Down Expand Up @@ -1265,8 +1353,20 @@ 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 transformedPreview = rotatedBox.child! as Transform;
final Matrix4 transformedPreviewMatrix =
transformedPreview.transform;

// Since the front camera is in portrait mode, we expect the camera
// preview to be mirrored across the y-axis.
checkYAxisIsMirrored(transformedPreviewMatrix);
expect((transformedPreview.child! as Texture).textureId, cameraId);
expect(
rotatedBox.quarterTurns,
expectedQuarterTurns,
Expand Down Expand Up @@ -1314,8 +1414,20 @@ 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 transformedPreview = rotatedBox.child! as Transform;
final Matrix4 transformedPreviewMatrix =
transformedPreview.transform;

// Since the front camera is in landscape mode, we expect the camera
// preview to be mirrored across the x-axis.
checkXAxisIsMirrored(transformedPreviewMatrix);
expect((transformedPreview.child! as Texture).textureId, cameraId);
expect(
rotatedBox.quarterTurns,
expectedQuarterTurns,
Expand Down Expand Up @@ -1406,8 +1518,19 @@ void main() {
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 transformedPreview = rotatedBox.child! as Transform;
final Matrix4 transformedPreviewMatrix =
transformedPreview.transform;

// Since the front camera is in landscape mode, we expect the camera
// preview to be mirrored across the x-axis.
checkXAxisIsMirrored(transformedPreviewMatrix);
expect((transformedPreview.child! as Texture).textureId, cameraId);
expect(
rotatedBox.quarterTurns,
expectedQuarterTurns,
Expand Down Expand Up @@ -1455,9 +1578,22 @@ 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 transformedPreview = rotatedBox.child! as Transform;
final Matrix4 transformedPreviewMatrix =
transformedPreview.transform;

// Since the front camera is in landscape mode, we expect the camera
// preview to be mirrored across the x-axis.
checkXAxisIsMirrored(transformedPreviewMatrix);
expect((transformedPreview.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,
Expand Down Expand Up @@ -1503,9 +1639,22 @@ 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 transformedPreview = rotatedBox.child! as Transform;
final Matrix4 transformedPreviewMatrix =
transformedPreview.transform;

// Since the front camera is in landscape mode, we expect the camera
// preview to be mirrored across the x-axis.
checkXAxisIsMirrored(transformedPreviewMatrix);
expect((transformedPreview.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,
Expand Down Expand Up @@ -1553,9 +1702,22 @@ 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 transformedPreview = rotatedBox.child! as Transform;
final Matrix4 transformedPreviewMatrix =
transformedPreview.transform;

// Since the front camera is in landscape mode, we expect the camera
// preview to be mirrored across the x-axis.
checkXAxisIsMirrored(transformedPreviewMatrix);
expect((transformedPreview.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,
Expand Down Expand Up @@ -1662,12 +1824,24 @@ 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 transformedPreview = rotatedBox.child! as Transform;
final Matrix4 transformedPreviewMatrix = transformedPreview.transform;

// Since the front camera is in landscape mode, we expect the camera
// preview to be mirrored across the x-axis.
checkXAxisIsMirrored(transformedPreviewMatrix);
expect((transformedPreview.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,
Expand Down Expand Up @@ -1760,12 +1934,30 @@ 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 transformedPreview = rotatedBox.child! as Transform;
final Matrix4 transformedPreviewMatrix = transformedPreview.transform;

// When the front camera is in landscape mode, we expect the camera
// preview to be mirrored across the x-axis. When the front camera
// is in portrait mode, we expect the camera preview to be mirrored
// across the y-axis.
if (currentDeviceOrientation == DeviceOrientation.landscapeLeft ||
currentDeviceOrientation == DeviceOrientation.landscapeRight) {
checkXAxisIsMirrored(transformedPreviewMatrix);
} else {
checkYAxisIsMirrored(transformedPreviewMatrix);
}
expect((transformedPreview.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,
Expand Down Expand Up @@ -2013,8 +2205,20 @@ 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 transformedPreview = rotatedBox.child! as Transform;
final Matrix4 transformedPreviewMatrix =
transformedPreview.transform;

// Since the front camera is in landscape mode, we expect the camera
// preview to be mirrored across the x-axis.
checkXAxisIsMirrored(transformedPreviewMatrix);
expect((transformedPreview.child! as Texture).textureId, cameraId);
expect(
rotatedBox.quarterTurns,
expectedQuarterTurns,
Expand Down