Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CB-11441] iOS: camera direction #63

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ capturing a video clip, the `CaptureErrorCB` callback executes with a
### Properties

- __limit__: The maximum number of images the user can capture in a single capture operation. The value must be greater than or equal to 1 (defaults to 1).
- __cameraDirection__: Direction of the camera (Camera.CAMERA_FRONT|Camera.CAMERA_BACK) (defaults to Camera.CAMERA_BACK) **This only works on iOS**

### Example

Expand All @@ -357,6 +358,7 @@ capturing a video clip, the `CaptureErrorCB` callback executes with a
- __limit__: The maximum number of video clips the device's user can capture in a single capture operation. The value must be greater than or equal to 1 (defaults to 1).

- __duration__: The maximum duration of a video clip, in seconds.
- __cameraDirection__: Direction of the camera (Camera.CAMERA_FRONT|Camera.CAMERA_BACK) (defaults to Camera.CAMERA_BACK) **This only works on iOS**

### Example

Expand Down
29 changes: 20 additions & 9 deletions src/ios/CDVCapture.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,17 @@ - (uint64_t)accessibilityTraits
- (BOOL)prefersStatusBarHidden {
return YES;
}

- (UIViewController*)childViewControllerForStatusBarHidden {
return nil;
}

- (void)viewWillAppear:(BOOL)animated {
SEL sel = NSSelectorFromString(@"setNeedsStatusBarAppearanceUpdate");
if ([self respondsToSelector:sel]) {
[self performSelector:sel withObject:nil afterDelay:0];
}

[super viewWillAppear:animated];
}

Expand Down Expand Up @@ -131,6 +131,7 @@ - (void)captureImage:(CDVInvokedUrlCommand*)command
// options could contain limit and mode neither of which are supported at this time
// taking more than one picture (limit) is only supported if provide own controls via cameraOverlayView property
// can support mode in OS
NSNumber* cameraDirection = [options objectForKey:@"cameraDirection"];

if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
NSLog(@"Capture.imageCapture: camera not available.");
Expand All @@ -144,6 +145,11 @@ - (void)captureImage:(CDVInvokedUrlCommand*)command
pickerController.delegate = self;
pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
pickerController.allowsEditing = NO;

if([cameraDirection intValue] == 1) {
pickerController.cameraDevice = UIImagePickerControllerCameraDeviceFront;
}

if ([pickerController respondsToSelector:@selector(mediaTypes)]) {
// iOS 3.0
pickerController.mediaTypes = [NSArray arrayWithObjects:(NSString*)kUTTypeImage, nil];
Expand Down Expand Up @@ -222,6 +228,7 @@ - (void)captureVideo:(CDVInvokedUrlCommand*)command
// options could contain limit, duration and mode
// taking more than one video (limit) is only supported if provide own controls via cameraOverlayView property
NSNumber* duration = [options objectForKey:@"duration"];
NSNumber* cameraDirection = [options objectForKey:@"cameraDirection"];
NSString* mediaType = nil;

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
Expand Down Expand Up @@ -253,6 +260,10 @@ - (void)captureVideo:(CDVInvokedUrlCommand*)command
// iOS 3.0
pickerController.mediaTypes = [NSArray arrayWithObjects:mediaType, nil];

if([cameraDirection intValue] == 1) {
pickerController.cameraDevice = UIImagePickerControllerCameraDeviceFront;
}

if ([mediaType isEqualToString:(NSString*)kUTTypeMovie]){
if (duration) {
pickerController.videoMaximumDuration = [duration doubleValue];
Expand Down Expand Up @@ -337,7 +348,7 @@ - (void)getMediaModes:(CDVInvokedUrlCommand*)command
movieArray ? (NSObject*) movieArray:[NSNull null], @"video",
audioArray ? (NSObject*) audioArray:[NSNull null], @"audio",
nil];

NSData* jsonData = [NSJSONSerialization dataWithJSONObject:modes options:0 error:nil];
NSString* jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

Expand Down Expand Up @@ -608,7 +619,7 @@ - (void)loadView
if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
self.edgesForExtendedLayout = UIRectEdgeNone;
}

// create view and display
CGRect viewRect = [[UIScreen mainScreen] applicationFrame];
UIView* tmp = [[UIView alloc] initWithFrame:viewRect];
Expand Down Expand Up @@ -737,7 +748,7 @@ - (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
UIInterfaceOrientationMask orientation = UIInterfaceOrientationMaskPortrait;
UIInterfaceOrientationMask supported = [captureCommand.viewController supportedInterfaceOrientations];

orientation = orientation | (supported & UIInterfaceOrientationMaskPortraitUpsideDown);
return orientation;
}
Expand All @@ -746,7 +757,7 @@ - (NSUInteger)supportedInterfaceOrientations
{
NSUInteger orientation = UIInterfaceOrientationMaskPortrait; // must support portrait
NSUInteger supported = [captureCommand.viewController supportedInterfaceOrientations];

orientation = orientation | (supported & UIInterfaceOrientationMaskPortraitUpsideDown);
return orientation;
}
Expand All @@ -773,7 +784,7 @@ - (void)processButton:(id)sender
__block NSError* error = nil;

__weak CDVAudioRecorderViewController* weakSelf = self;

void (^startRecording)(void) = ^{
[weakSelf.avSession setCategory:AVAudioSessionCategoryRecord error:&error];
[weakSelf.avSession setActive:YES error:&error];
Expand All @@ -794,7 +805,7 @@ - (void)processButton:(id)sender
}
UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, nil);
};

SEL rrpSel = NSSelectorFromString(@"requestRecordPermission:");
if ([self.avSession respondsToSelector:rrpSel])
{
Expand Down
6 changes: 6 additions & 0 deletions www/capture.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ function Capture() {
this.supportedVideoModes = [];
}

/**
* Camera direction constants
*/
Capture.CAMERA_BACK = 0;
Capture.CAMERA_FRONT = 1;

/**
* Launch audio recorder application for recording audio clip(s).
*
Expand Down