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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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+1

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

## 0.6.18

* Adds support for the `MediaSettings.enableAudio` setting, which determines whether or not audio is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,21 @@ 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.18
version: 0.6.18+1

environment:
sdk: ^3.7.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 @@ -1167,8 +1167,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.
expect(transformedPreviewMatrix.storage[5], closeTo(-1.0, 0.0001));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

What do you think about creating a helper method for these sorts of checks? Something like checkYAxisIsMirrored(double matrixArg5) and checkXAxisIsMirrored(double matrixArg0) with docs for both explaining why this checks what we think? I think this may help for clarity in the future.

expect((transformedPreview.child! as Texture).textureId, cameraId);
expect(
rotatedBox.quarterTurns,
expectedQuarterTurns,
Expand Down Expand Up @@ -1216,8 +1227,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.
expect(transformedPreviewMatrix.storage[0], closeTo(-1.0, 0.0001));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I assume the closeTo is because a precision error? If there's no workaround for that, can you add a comment here (or to the helper methods if you're open to adding them) about that?

expect((transformedPreview.child! as Texture).textureId, cameraId);
expect(
rotatedBox.quarterTurns,
expectedQuarterTurns,
Expand Down Expand Up @@ -1265,8 +1288,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.
expect(transformedPreviewMatrix.storage[5], closeTo(-1.0, 0.0001));
expect((transformedPreview.child! as Texture).textureId, cameraId);
expect(
rotatedBox.quarterTurns,
expectedQuarterTurns,
Expand Down Expand Up @@ -1314,8 +1349,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.
expect(transformedPreviewMatrix.storage[0], closeTo(-1.0, 0.0001));
expect((transformedPreview.child! as Texture).textureId, cameraId);
expect(
rotatedBox.quarterTurns,
expectedQuarterTurns,
Expand Down Expand Up @@ -1406,8 +1453,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.
expect(transformedPreviewMatrix.storage[0], closeTo(-1.0, 0.0001));
expect((transformedPreview.child! as Texture).textureId, cameraId);
expect(
rotatedBox.quarterTurns,
expectedQuarterTurns,
Expand Down Expand Up @@ -1455,9 +1513,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.
expect(transformedPreviewMatrix.storage[0], closeTo(-1.0, 0.0001));
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 +1574,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.
expect(transformedPreviewMatrix.storage[0], closeTo(-1.0, 0.0001));
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 +1637,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.
expect(transformedPreviewMatrix.storage[0], closeTo(-1.0, 0.0001));
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 +1759,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.
expect(transformedPreviewMatrix.storage[0], closeTo(-1.0, 0.0001));
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 +1869,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) {
expect(transformedPreviewMatrix.storage[0], closeTo(-1.0, 0.0001));
} else {
expect(transformedPreviewMatrix.storage[5], closeTo(-1.0, 0.0001));
}
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 +2140,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.
expect(transformedPreviewMatrix.storage[0], closeTo(-1.0, 0.0001));
expect((transformedPreview.child! as Texture).textureId, cameraId);
expect(
rotatedBox.quarterTurns,
expectedQuarterTurns,
Expand Down