Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set front-facing camera in Xcode

Tags:

Set front-facing camera in Xcode. How can I do that? Set front-facing camera in Xcode.

AVCaptureSession *session = [AVCaptureSession new];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
    [session setSessionPreset:AVCaptureSessionPreset640x480];
else
    [session setSessionPreset:AVCaptureSessionPresetPhoto];

AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
require( error == nil, bail );

isUsingFrontFacingCamera = NO;
if ( [session canAddInput:deviceInput] )
    [session addInput:deviceInput];

stillImageOutput = [AVCaptureStillImageOutput new];
[stillImageOutput addObserver:self forKeyPath:@"capturingStillImage" options:NSKeyValueObservingOptionNew context:AVCaptureStillImageIsCapturingStillImageContext];
if ( [session canAddOutput:stillImageOutput] )
    [session addOutput:stillImageOutput];
like image 824
Damian Avatar asked May 29 '12 08:05

Damian


3 Answers

Since iOS 10 devicesWithMediaType: is deprecated. You should use AVCAptureDeviceDiscoverySession.

- (AVCaptureDevice *)frontCamera {    
    return [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[ AVCaptureDeviceTypeBuiltInWideAngleCamera ]
                                                                  mediaType:AVMediaTypeVideo
                                                                   position:AVCaptureDevicePositionFront].devices.firstObject;
}
like image 39
vdugnist Avatar answered Oct 01 '22 04:10

vdugnist


Add this method to iterate over existing devices checking its position:

- (AVCaptureDevice *)frontCamera {
    NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    for (AVCaptureDevice *device in devices) {
        if ([device position] == AVCaptureDevicePositionFront) {
            return device;
        }
    }
    return nil;
}

Then, replace:

AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

with:

AVCaptureDevice *device = [self frontCamera];
like image 188
djromero Avatar answered Oct 01 '22 02:10

djromero


**.h file
@interface ViewController : UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
.m File
-(IBAction)Btn_Ok:(id)sender
{
 if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
            {
                UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                                      message:@"Device has no camera"
                                                                     delegate:nil
                                                            cancelButtonTitle:@"OK"
                                                            otherButtonTitles: nil];
                [myAlertView show];
            }
            else
            {
                UIImagePickerController *picker = [[UIImagePickerController alloc] init];
                picker.delegate = self;
                picker.allowsEditing = YES;
                picker.sourceType = UIImagePickerControllerSourceTypeCamera;
                picker.cameraDevice=UIImagePickerControllerCameraDeviceFront;
                [self presentViewController:picker animated:YES completion:NULL];
            }
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    self.yourimg.image = chosenImage;
    [picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:NULL];
}
like image 34
Yogesh Tarsariya Avatar answered Oct 01 '22 03:10

Yogesh Tarsariya