Skip to content
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

feat: Add rotation to iOS (the sequel) #548

Merged
merged 16 commits into from
Apr 15, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ + (MLKVisionImage *)visionImageFromData:(NSDictionary *)imageData {
if ([@"file" isEqualToString:imageType]) {
return [self filePathToVisionImage:imageData[@"path"]];
} else if ([@"bytes" isEqualToString:imageType]) {
return [self bytesToVisionImage:imageData];
int rotation = [imageData[@"metadata"][@"rotation"] intValue];
int cameraLensDirection = [imageData[@"metadata"][@"camera_lens_direction"] intValue];
UIImageOrientation imageOrientation = [self imageOrientationFromRotation:rotation cameraLensDirection:cameraLensDirection];
MLKVisionImage *image = [self bytesToVisionImage:imageData];
image.orientation = imageOrientation;
return image;
} else {
NSString *errorReason = [NSString stringWithFormat:@"No image type for: %@", imageType];
@throw [NSException exceptionWithName:NSInvalidArgumentException
Expand All @@ -17,6 +22,23 @@ + (MLKVisionImage *)visionImageFromData:(NSDictionary *)imageData {
}
}

+ (UIImageOrientation)imageOrientationFromRotation:(int)rotation cameraLensDirection:(int)cameraLensDirection {
switch (rotation) {
case 90:
return cameraLensDirection == 0 ? UIImageOrientationRight // Rotates the image 270 degrees to the left
: UIImageOrientationRightMirrored; // Rotates the image 270 degrees to the left (image is mirrored)
case 180:
return cameraLensDirection == 0 ? UIImageOrientationDown // Rotates the image 180 degrees to the left
: UIImageOrientationDownMirrored; // Rotates the image 180 degrees to the left (image is mirrored)
case 270:
return cameraLensDirection == 0 ? UIImageOrientationLeft // Rotates the image 90 degrees to the left
: UIImageOrientationLeftMirrored; // Rotates the image 90 degrees to the left (image is mirrored)
default:
return cameraLensDirection == 0 ? UIImageOrientationUp // Rotates 0 degreees
: UIImageOrientationUpMirrored; // Rotates the image 0 degrees (image is mirrored)
}
}

+ (MLKVisionImage *)filePathToVisionImage:(NSString *)filePath {
UIImage *image = [UIImage imageWithContentsOfFile:filePath];
MLKVisionImage *visionImage = [[MLKVisionImage alloc] initWithImage:image];
Expand Down
35 changes: 35 additions & 0 deletions packages/google_mlkit_commons/lib/src/input_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,18 @@ class InputImageMetadata {
/// Not used on Android.
final int bytesPerRow;

// Camera lens direction.
///
/// Not used on Android.
final InputImageCameraLensDirection cameraLensDirection;

/// Constructor to create an instance of [InputImageMetadata].
InputImageMetadata({
required this.size,
required this.rotation,
required this.format,
required this.bytesPerRow,
required this.cameraLensDirection,
});

/// Returns a json representation of an instance of [InputImageMetadata].
Expand All @@ -85,6 +91,7 @@ class InputImageMetadata {
'rotation': rotation.rawValue,
'image_format': format.rawValue,
'bytes_per_row': bytesPerRow,
'camera_lens_direction': cameraLensDirection.rawValue,
};
}

Expand Down Expand Up @@ -155,3 +162,31 @@ extension InputImageFormatValue on InputImageFormat {
}
}
}

/// The camera lens direction to be specified
///
/// Not used on Android.
enum InputImageCameraLensDirection {
front,
back,
}

extension InputImageCameraLensDirectionValue on InputImageCameraLensDirection {
int get rawValue {
switch (this) {
case InputImageCameraLensDirection.back:
return 0;
case InputImageCameraLensDirection.front:
return 1;
}
}

static InputImageCameraLensDirection? fromRawValue(int rawValue) {
try {
return InputImageCameraLensDirection.values
.firstWhere((element) => element.rawValue == rawValue);
} catch (_) {
return null;
}
}
}
Loading