mirror of
https://github.com/status-im/react-native-camera.git
synced 2025-02-24 01:38:18 +00:00
feat(mirror): add option to give "mirrorImage" flag to takePicture.
This commit is contained in:
parent
077b0091ed
commit
0b6f0abda0
@ -73,6 +73,10 @@ public class ResolveTakenPictureAsyncTask extends AsyncTask<Void, Void, Writable
|
||||
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 @@ public class ResolveTakenPictureAsyncTask extends AsyncTask<Void, Void, Writable
|
||||
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) {
|
||||
|
@ -322,6 +322,10 @@ static NSDictionary *defaultFaceDetectorOptions = nil;
|
||||
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 @@ static NSDictionary *defaultFaceDetectorOptions = nil;
|
||||
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
|
||||
}
|
||||
|
@ -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;
|
||||
|
||||
|
@ -38,6 +38,29 @@
|
||||
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];
|
||||
|
Loading…
x
Reference in New Issue
Block a user