Skip to content

Commit

Permalink
feat(ios): add support for preview to match viewport cropping (reac…
Browse files Browse the repository at this point in the history
  • Loading branch information
Ignigena authored and rt2zz committed Dec 13, 2017
1 parent 874a29f commit 39f19cd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 3 additions & 1 deletion ios/RCTCameraManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ typedef NS_ENUM(NSInteger, RCTCameraCaptureSessionPreset) {
RCTCameraCaptureSessionPresetPhoto = 3,
RCTCameraCaptureSessionPreset480p = 4,
RCTCameraCaptureSessionPreset720p = 5,
RCTCameraCaptureSessionPreset1080p = 6
RCTCameraCaptureSessionPreset1080p = 6,
RCTCameraCaptureSessionPresetPreview = 7
};

typedef NS_ENUM(NSInteger, RCTCameraCaptureMode) {
Expand Down Expand Up @@ -75,6 +76,7 @@ typedef NS_ENUM(NSInteger, RCTCameraTorchMode) {
@property (nonatomic, strong) RCTPromiseResolveBlock videoResolve;
@property (nonatomic, strong) RCTPromiseRejectBlock videoReject;
@property (nonatomic, strong) RCTCamera *camera;
@property (nonatomic, assign) BOOL cropToViewport;


- (void)changeOrientation:(NSInteger)orientation;
Expand Down
17 changes: 16 additions & 1 deletion ios/RCTCameraManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ - (NSDictionary *)constantsToExport
@"720p": @(RCTCameraCaptureSessionPreset720p),
@"AVCaptureSessionPreset1280x720": @(RCTCameraCaptureSessionPreset720p),
@"1080p": @(RCTCameraCaptureSessionPreset1080p),
@"AVCaptureSessionPreset1920x1080": @(RCTCameraCaptureSessionPreset1080p)
@"AVCaptureSessionPreset1920x1080": @(RCTCameraCaptureSessionPreset1080p),
@"preview": @(RCTCameraCaptureSessionPresetPreview)
},
@"CaptureTarget": @{
@"memory": @(RCTCameraCaptureTargetMemory),
Expand Down Expand Up @@ -140,6 +141,7 @@ - (NSDictionary *)constantsToExport
RCT_EXPORT_VIEW_PROPERTY(onZoomChanged, BOOL);

RCT_CUSTOM_VIEW_PROPERTY(captureQuality, NSInteger, RCTCamera) {
self.cropToViewport = false;
NSInteger quality = [RCTConvert NSInteger:json];
NSString *qualityString;
switch (quality) {
Expand All @@ -165,6 +167,10 @@ - (NSDictionary *)constantsToExport
case RCTCameraCaptureSessionPreset480p:
qualityString = AVCaptureSessionPreset640x480;
break;
case RCTCameraCaptureSessionPresetPreview:
qualityString = AVCaptureSessionPresetPhoto;
self.cropToViewport = true;
break;
}

[self setCaptureQuality:qualityString];
Expand Down Expand Up @@ -653,6 +659,15 @@ - (void)captureStill:(NSInteger)target options:(NSDictionary *)options orientati
rotatedCGImage = cgImage;
}

// Crop it (if capture quality is set to preview)
if (self.cropToViewport) {
CGSize viewportSize = CGSizeMake(self.previewLayer.frame.size.width, self.previewLayer.frame.size.height);
CGRect captureRect = CGRectMake(0, 0, CGImageGetWidth(rotatedCGImage), CGImageGetHeight(rotatedCGImage));
CGRect croppedSize = AVMakeRectWithAspectRatioInsideRect(viewportSize, captureRect);

rotatedCGImage = CGImageCreateWithImageInRect(rotatedCGImage, croppedSize);
}

// Erase stupid TIFF stuff
[imageMetadata removeObjectForKey:(NSString *)kCGImagePropertyTIFFDictionary];

Expand Down

0 comments on commit 39f19cd

Please sign in to comment.