Skip to content

Commit

Permalink
switch between camera
Browse files Browse the repository at this point in the history
  • Loading branch information
dinitri committed Mar 11, 2024
1 parent edfbc79 commit 30bbf55
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/ios/CameraSessionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- (void) setupSession:(NSString *)defaultCamera completion:(void(^)(BOOL started))completion options:(NSDictionary *)options photoSettings:(AVCapturePhotoSettings *)photoSettings;
- (void) setFlashMode:(NSInteger)flashMode photoSettings:(AVCapturePhotoSettings *)photoSettings;
- (void) torchSwitch:(NSInteger)torchState;
- (BOOL) switchToUltraWideCamera;
- (void) updateOrientation:(AVCaptureVideoOrientation)orientation;
- (AVCaptureVideoOrientation) getCurrentOrientation:(UIInterfaceOrientation)toInterfaceOrientation;
+ (AVCaptureSessionPreset) calculateResolution:(NSInteger)targetSize;
Expand Down
65 changes: 60 additions & 5 deletions src/ios/CameraSessionManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,16 @@ - (void) setupSession:(NSString *)defaultCamera completion:(void(^)(BOOL started
} else {
self.defaultCamera = AVCaptureDevicePositionBack;
}

AVCaptureDevice *videoDevice;
videoDevice = [self cameraWithPosition: self.defaultCamera captureDeviceType: AVCaptureDeviceTypeBuiltInWideAngleCamera];
if ([options[@"captureDevice"] isEqual: @"wide-angle"]) {
// need to add check for ios version first
videoDevice = [self cameraWithPosition: self.defaultCamera captureDeviceType: AVCaptureDeviceTypeBuiltInUltraWideCamera];
}
else {
videoDevice = [self cameraWithPosition: self.defaultCamera captureDeviceType: AVCaptureDeviceTypeBuiltInWideAngleCamera];
if ([self deviceHasUltraWideCamera]) {
if (@available(iOS 13.0, *)) {
videoDevice = [self cameraWithPosition: self.defaultCamera captureDeviceType: AVCaptureDeviceTypeBuiltInUltraWideCamera];
}
}

}

if ([videoDevice hasFlash]) {
Expand Down Expand Up @@ -173,6 +176,58 @@ - (void) torchSwitch:(NSInteger)torchState{
}
}

- (BOOL)switchToUltraWideCamera {
if (![self deviceHasUltraWideCamera]) return FALSE;

dispatch_async(self.sessionQueue, ^{
if (@available(iOS 13.0, *)) {
AVCaptureDevice *ultraWideCamera = [self cameraWithPosition: self.defaultCamera captureDeviceType:AVCaptureDeviceTypeBuiltInUltraWideCamera];
if (ultraWideCamera) {
// Remove the current input
[self.session removeInput:self.videoDeviceInput];

// Create a new input with the ultra-wide camera
NSError *error = nil;
AVCaptureDeviceInput *ultraWideVideoDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:ultraWideCamera error:&error];

if (!error) {
// Add the new input to the session
if ([self.session canAddInput:ultraWideVideoDeviceInput]) {
[self.session addInput:ultraWideVideoDeviceInput];
self.videoDeviceInput = ultraWideVideoDeviceInput;
__block AVCaptureVideoOrientation orientation;
dispatch_sync(dispatch_get_main_queue(), ^{
orientation=[self getCurrentOrientation];
});
[self updateOrientation:orientation];
} else {
NSLog(@"Failed to add ultra-wide input to session");
}
} else {
NSLog(@"Error creating ultra-wide device input: %@", error.localizedDescription);
}
} else {
NSLog(@"Ultra-wide camera not found");
}
} else {
// Fallback on earlier versions
}
});
return TRUE;
}

- (BOOL)deviceHasUltraWideCamera {
NSArray *devices;
if (@available(iOS 13.0, *)) {
AVCaptureDeviceDiscoverySession *discoverySession = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInUltraWideCamera] mediaType:AVMediaTypeVideo position:AVCaptureDevicePositionUnspecified];
devices = discoverySession.devices;
} else {
// Fallback on earlier versions
}

return devices.count > 0;
}

- (void)setFlashMode:(NSInteger)flashMode photoSettings:(AVCapturePhotoSettings *)photoSettings {
NSError *error = nil;
// Let's save the setting even if we can't set it up on this camera.
Expand Down
1 change: 1 addition & 0 deletions src/ios/SimpleCameraPreview.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- (void) capture:(CDVInvokedUrlCommand*)command;
- (void) setSize:(CDVInvokedUrlCommand*)command;
- (void) torchSwitch: (CDVInvokedUrlCommand*)command;
- (void) switchToUltraWideCamera: (CDVInvokedUrlCommand*) command;
- (void) deviceHasFlash: (CDVInvokedUrlCommand*)command;
@property (nonatomic) CameraSessionManager *sessionManager;
@property (nonatomic) CameraRenderController *cameraRenderController;
Expand Down
9 changes: 8 additions & 1 deletion src/ios/SimpleCameraPreview.m
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ - (void) torchSwitch:(CDVInvokedUrlCommand*)command{
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

- (void) switchToUltraWideCamera:(CDVInvokedUrlCommand*)command{
if (self.sessionManager != nil) {
[self.sessionManager switchToUltraWideCamera];
}
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

- (void) deviceHasFlash:(CDVInvokedUrlCommand*)command{
AVCaptureDeviceDiscoverySession *captureDeviceDiscoverySession = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInWideAngleCamera]
mediaType:AVMediaTypeVideo
Expand Down Expand Up @@ -192,7 +200,6 @@ - (void) capture:(CDVInvokedUrlCommand*)command {
}
}


- (NSDictionary *)getGPSDictionaryForLocation {
if (!currentLocation)
return nil;
Expand Down
4 changes: 4 additions & 0 deletions www/SimpleCameraPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ SimpleCameraPreview.torchSwitch = function (options, onSuccess, onError) {
exec(onSuccess, onError, PLUGIN_NAME, "torchSwitch", [options]);
};

SimpleCameraPreview.switchToUltraWideCamera = function (onSuccess, onError) {
exec(onSuccess, onError, PLUGIN_NAME, "switchToUltraWideCamera", []);
};

SimpleCameraPreview.deviceHasFlash = function (onSuccess, onError) {
exec(onSuccess, onError, PLUGIN_NAME, "deviceHasFlash", []);
};
Expand Down

0 comments on commit 30bbf55

Please sign in to comment.