From 0b6f0abda07b8a9ff3daa1722a254087f30eec08 Mon Sep 17 00:00:00 2001 From: Joao Fidelis Date: Wed, 14 Feb 2018 10:48:21 -0200 Subject: [PATCH] feat(mirror): add option to give "mirrorImage" flag to takePicture. --- .../tasks/ResolveTakenPictureAsyncTask.java | 10 ++++++++ ios/RN/RNCamera.m | 9 ++++++++ ios/RN/RNImageUtils.h | 1 + ios/RN/RNImageUtils.m | 23 +++++++++++++++++++ 4 files changed, 43 insertions(+) diff --git a/android/src/main/java/org/reactnative/camera/tasks/ResolveTakenPictureAsyncTask.java b/android/src/main/java/org/reactnative/camera/tasks/ResolveTakenPictureAsyncTask.java index ef8b69677..e8976de3a 100644 --- a/android/src/main/java/org/reactnative/camera/tasks/ResolveTakenPictureAsyncTask.java +++ b/android/src/main/java/org/reactnative/camera/tasks/ResolveTakenPictureAsyncTask.java @@ -73,6 +73,10 @@ protected WritableMap doInBackground(Void... voids) { mBitmap = rotateBitmap(mBitmap, getImageRotation(orientation)); } + if (mOptions.hasKey("mirrorImage") && mOptions.getBoolean("mirrorImage")) { + mBitmap = flipHorizontally(mBitmap); + } + // Write Exif data to the response if requested if (mOptions.hasKey("exif") && mOptions.getBoolean("exif")) { WritableMap exifData = RNCameraViewHelper.getExifData(exifInterface); @@ -133,6 +137,12 @@ private Bitmap rotateBitmap(Bitmap source, int angle) { return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true); } + private Bitmap flipHorizontally(Bitmap source) { + Matrix matrix = new Matrix(); + matrix.preScale(-1.0f, 1.0f); + return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true); + } + // Get rotation degrees from Exif orientation enum private int getImageRotation(int orientation) { diff --git a/ios/RN/RNCamera.m b/ios/RN/RNCamera.m index 14922319f..dc61b5592 100644 --- a/ios/RN/RNCamera.m +++ b/ios/RN/RNCamera.m @@ -322,6 +322,10 @@ - (void)takePicture:(NSDictionary *)options resolve:(RCTPromiseResolveBlock)reso CGRect cropRect = CGRectMake(frame.origin.x * width, frame.origin.y * height, frame.size.width * width, frame.size.height * height); takenImage = [RNImageUtils cropImage:takenImage toRect:cropRect]; + if ([options[@"mirrorImage"] boolValue]) { + takenImage = [RNImageUtils mirrorImage:takenImage]; + } + NSMutableDictionary *response = [[NSMutableDictionary alloc] init]; float quality = [options[@"quality"] floatValue]; NSData *takenImageData = UIImageJPEGRepresentation(takenImage, quality); @@ -338,16 +342,21 @@ - (void)takePicture:(NSDictionary *)options resolve:(RCTPromiseResolveBlock)reso int imageRotation; switch (takenImage.imageOrientation) { case UIImageOrientationLeft: + case UIImageOrientationRightMirrored: imageRotation = 90; break; case UIImageOrientationRight: + case UIImageOrientationLeftMirrored: imageRotation = -90; break; case UIImageOrientationDown: + case UIImageOrientationDownMirrored: imageRotation = 180; break; + case UIImageOrientationUpMirrored: default: imageRotation = 0; + break; } [RNImageUtils updatePhotoMetadata:imageSampleBuffer withAdditionalData:@{ @"Orientation": @(imageRotation) } inResponse:response]; // TODO } diff --git a/ios/RN/RNImageUtils.h b/ios/RN/RNImageUtils.h index b1da64f32..f173809d8 100644 --- a/ios/RN/RNImageUtils.h +++ b/ios/RN/RNImageUtils.h @@ -13,6 +13,7 @@ + (UIImage *)generatePhotoOfSize:(CGSize)size; + (UIImage *)cropImage:(UIImage *)image toRect:(CGRect)rect; ++ (UIImage *)mirrorImage:(UIImage *)image; + (NSString *)writeImage:(NSData *)image toPath:(NSString *)path; + (void)updatePhotoMetadata:(CMSampleBufferRef)imageSampleBuffer withAdditionalData:(NSDictionary *)additionalData inResponse:(NSMutableDictionary *)response; diff --git a/ios/RN/RNImageUtils.m b/ios/RN/RNImageUtils.m index 7f8767eda..e5d4966c7 100644 --- a/ios/RN/RNImageUtils.m +++ b/ios/RN/RNImageUtils.m @@ -38,6 +38,29 @@ + (UIImage *)cropImage:(UIImage *)image toRect:(CGRect)rect return image; } ++ (UIImage *)mirrorImage:(UIImage *)image +{ + UIImageOrientation flippedOrientation = UIImageOrientationUpMirrored; + switch (image.imageOrientation) { + case UIImageOrientationDown: + flippedOrientation = UIImageOrientationDownMirrored; + break; + case UIImageOrientationLeft: + flippedOrientation = UIImageOrientationRightMirrored; + break; + case UIImageOrientationUp: + flippedOrientation = UIImageOrientationUpMirrored; + break; + case UIImageOrientationRight: + flippedOrientation = UIImageOrientationLeftMirrored; + break; + default: + break; + } + UIImage * flippedImage = [UIImage imageWithCGImage:image.CGImage scale:image.scale orientation:flippedOrientation]; + return flippedImage; +} + + (NSString *)writeImage:(NSData *)image toPath:(NSString *)path { [image writeToFile:path atomically:YES];