Skip to content

Commit

Permalink
feat: Add rotation to iOS (#548)
Browse files Browse the repository at this point in the history
  • Loading branch information
juanmartin8a authored Apr 15, 2024
1 parent cac0c3a commit 40d3ce9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
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;
}
}
}

0 comments on commit 40d3ce9

Please sign in to comment.