Skip to content
This repository has been archived by the owner on Jun 16, 2023. It is now read-only.

Commit

Permalink
feat(mirror): add option to give "mirrorImage" flag to takePicture.
Browse files Browse the repository at this point in the history
  • Loading branch information
jgfidelis committed Feb 14, 2018
1 parent 077b009 commit 0b6f0ab
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions ios/RN/RNCamera.m
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions ios/RN/RNImageUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
23 changes: 23 additions & 0 deletions ios/RN/RNImageUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down

0 comments on commit 0b6f0ab

Please sign in to comment.