Last active
February 28, 2016 04:24
-
-
Save sdgandhi/2fe940fceb0ccadc830a to your computer and use it in GitHub Desktop.
Setup camera capture and view on iOS (and flip camera + flashlight)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// *** Setup camera *** // | |
- (void)setupCamera | |
{ | |
// setup session | |
self.session = [AVCaptureSession new]; | |
self.session.sessionPreset = AVCaptureSessionPresetPhoto; | |
// setup device | |
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; | |
if ([device lockForConfiguration:nil]) { | |
if (device.autoFocusRangeRestrictionSupported) { | |
device.autoFocusRangeRestriction = AVCaptureAutoFocusRangeRestrictionNear; | |
} | |
if (device.smoothAutoFocusSupported) { | |
device.smoothAutoFocusEnabled = YES; | |
} | |
if ([device isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus]){ | |
device.focusMode = AVCaptureFocusModeContinuousAutoFocus; | |
} | |
device.exposureMode = AVCaptureExposureModeContinuousAutoExposure; | |
[device unlockForConfiguration]; | |
} | |
// add device input to session | |
AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil]; | |
[self.session addInput:deviceInput]; | |
// add output to session | |
NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:AVVideoCodecJPEG, AVVideoCodecKey, nil]; | |
AVCaptureStillImageOutput *stillImage = [AVCaptureStillImageOutput new]; | |
stillImage.outputSettings = outputSettings; | |
[self.session addOutput:stillImage]; | |
AVCaptureVideoPreviewLayer *preview = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session]; | |
preview.videoGravity = AVLayerVideoGravityResizeAspectFill; | |
preview.frame = self.cameraPreview.layer.bounds; | |
[self.cameraPreview.layer addSublayer:preview]; | |
[self.session startRunning]; | |
} | |
// *** Flip camera *** // | |
//Change camera source | |
- (IBAction)flipTapped:(id)sender | |
{ | |
//Change camera source | |
if(self.session) | |
{ | |
//Indicate that some changes will be made to the session | |
[self.session beginConfiguration]; | |
//Remove existing input | |
AVCaptureInput* currentCameraInput = [self.session.inputs objectAtIndex:0]; | |
[self.session removeInput:currentCameraInput]; | |
//Get new input | |
AVCaptureDevice *newCamera = nil; | |
if(((AVCaptureDeviceInput*)currentCameraInput).device.position == AVCaptureDevicePositionBack) { | |
newCamera = [self cameraWithPosition:AVCaptureDevicePositionFront]; | |
} | |
else { | |
newCamera = [self cameraWithPosition:AVCaptureDevicePositionBack]; | |
} | |
//Add input to session | |
NSError *err = nil; | |
AVCaptureDeviceInput *newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:newCamera error:&err]; | |
if(!newVideoInput || err) { | |
NSLog(@"Error creating capture device input: %@", err.localizedDescription); | |
} | |
else { | |
[self.session addInput:newVideoInput]; | |
} | |
//Commit all the configuration changes at once | |
[self.session commitConfiguration]; | |
} | |
} | |
- (AVCaptureDevice *) cameraWithPosition:(AVCaptureDevicePosition) position | |
{ | |
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; | |
for (AVCaptureDevice *device in devices) { | |
if ([device position] == position) return device; | |
} | |
return nil; | |
} | |
- (IBAction)flashTapped:(id)sender | |
{ | |
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; | |
if (device.hasTorch) { | |
[device lockForConfiguration:nil]; | |
if (device.torchMode == AVCaptureTorchModeOn) { | |
device.torchMode = AVCaptureTorchModeOff; | |
} else { | |
[device setTorchModeOnWithLevel:AVCaptureMaxAvailableTorchLevel error:nil]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment