iphone camera exposure/focus lock: it doesn't stay locked - iphone

I am try ing to take multiple pictures with iphone camera from the same scene programmatically and I don't want the exposure or focus to change after the user sets it for the first picture.
Here is my code that gets executed when the use initiates the picture taking from within my app:
NSArray *devices = [AVCaptureDevice devices];
NSError *error;
for (AVCaptureDevice *device in devices) {
if (([device hasMediaType:AVMediaTypeVideo]) &&
([device position] == AVCaptureDevicePositionBack) ) {
[device lockForConfiguration:&error];
if ([device isWhiteBalanceModeSupported:AVCaptureWhiteBalanceModeLocked]) {
device.whiteBalanceMode = AVCaptureWhiteBalanceModeLocked;
NSLog(#"Whitebalanced locked");
}
if ([device isExposureModeSupported:AVCaptureExposureModeLocked]) {
device.exposureMode = AVCaptureExposureModeLocked;
NSLog(#"Exposure locked");
}
if ([device isFocusModeSupported:AVCaptureFocusModeLocked]) {
device.focusMode = AVCaptureFocusModeLocked;
NSLog(#"Focus locked");
}
[device unlockForConfiguration];
}
}
The problem: Camera does auto-focus and auto-exposure after the first picture is taken. How can I force exposure and focus to stay locked?
Any help is appreciated.

I think you're missing to commit the configuration:
Before your code put:
captureSession = [[AVCaptureSession alloc] init];
[captureSession beginConfiguration];
After:
[captureSession commitConfiguration];
Hope this helped.

Related

Programmatically determine which camera ie front or back is being used in application iphone

In My application I need to determine Which camera is being used currently and toggle the camera ie back camera to front camera I have Implemented following code from Check which camera is currently in use in iOS Application
AVCaptureDevicePosition currentCameraPosition = [[VideoInputDevice device] position];
if (currentCameraPosition == AVCaptureDevicePositionBack)
{
currentCameraPosition = AVCaptureDevicePositionFront;
}
else
{
currentCameraPosition = AVCaptureDevicePositionBack;
}
AVCaptureDevice *backFacingCamera = nil;
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices)
{
if ([device position] == currentCameraPosition)
{
backFacingCamera = device;
}
}
NSLog(backFacingCamera ? #"Yes":#"No");
But Getting wrong answer
Your code looks fine. Assign values to variables after getting result like
- (void)frontCamera {
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices) {
if ([device position] == AVCaptureDevicePositionFront) {
//Front camera is used
}
}
}

How to disable the Auto focus on UIImagePicker and works same as front Camera?

i want to Disable the Auto focus on a Camera Application Using UIImagePicker,and Works same as Front Camera without Focusing.Can anyone Help me in this?
i have used this code but only the Square box is hidden, but still focusing...
-(void)disableAutoFocus
{
NSArray *devices = [AVCaptureDevice devices];
NSError *error;
for (AVCaptureDevice *device in devices) {
if (([device hasMediaType:AVMediaTypeVideo]) &&
([device position] == AVCaptureDevicePositionBack) ) {
[device lockForConfiguration:&error];
if ([device isFocusModeSupported:AVCaptureFocusModeLocked]) {
device.focusMode = AVCaptureFocusModeLocked;
NSLog(#"Focus locked");
}
[device unlockForConfiguration];
}
}
}

iPhone front camera mirror

My app is working with iPhone's front camera. When Iam trying to capture my photo there is an issue.
When I am turn left In the camera it is showing like turn right. Just like a mirror. How can I avoid this?!
AVCaptureDeviceInput *newVideoInput;
AVCaptureDevicePosition currentCameraPosition = [[videoInput device] position];
if (currentCameraPosition == AVCaptureDevicePositionBack)
{
currentCameraPosition = AVCaptureDevicePositionFront;
}
else
{
currentCameraPosition = AVCaptureDevicePositionBack;
}
AVCaptureDevice *backFacingCamera = nil;
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices)
{
if ([device position] == currentCameraPosition)
{
backFacingCamera = device;
}
}
newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:backFacingCamera error:&error];

How to on Torch light without using camera/video mode in iPhone?

I am using this below code for switch on the Torch light in iphone app. It working fine. The issue is, when we press the button the torch mode will be changed to 'On' but, the torch light only appear when the user entering into the Camera screen. I want to switch on the torch light without using the Camera screen. Can anyone please guide me? Please suggest me where i am wrong. Here my code,
captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (captureDevice.torchMode == AVCaptureTorchModeOff)
{
AVCaptureSession *session = [[AVCaptureSession alloc] init];
[session beginConfiguration];
[captureDevice lockForConfiguration:nil];
[captureDevice setTorchMode:AVCaptureTorchModeOn];
[captureDevice unlockForConfiguration];
[session commitConfiguration];
[session startRunning];
[self setTorchSession:session];
[session release];
}
else
{
[torchSession stopRunning];
[captureDevice setTorchMode:AVCaptureTorchModeOff];
}
Is this correct code for Torch Light in iPhone? Please help me. Thanks in advance.
This code works for me
- (void) internal_setFlashOn: (BOOL) turnOn {
AVCaptureDevice *theDevice = self.captureDevice;
if ([theDevice hasTorch]) {
[theDevice lockForConfiguration: nil];
AVCaptureTorchMode currentMode = [theDevice torchMode];
BOOL isAlreadyTurnedOn = (AVCaptureTorchModeOn == currentMode);
if (isAlreadyTurnedOn != turnOn) {
[theDevice setTorchMode: turnOn? AVCaptureTorchModeOn: AVCaptureTorchModeOff];
}
[theDevice unlockForConfiguration];
}
}
- (AVCaptureDevice *) captureDevice {
if (nil == internal_captureDevice) {
internal_captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
[internal_captureDevice retain];
}
return internal_captureDevice;
}
This works on iPhone4 and above.

How can I determine whether my iOS device has a torch light?

In my application I have the option for a torch light. Howevver, only iPhone 4 and iPhone 4S have torch lights. Other devices do not have the torch light. How can I find the current device model? Please help me. Thanks in advance.
You should not use the device model as an indicator of whether a feature is present. Instead, use the API that tells you exactly if the feature is present.
In your case, you want to use AVCaptureDevice's -hasTorch property:
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
NSMutableArray *torchDevices = [[NSMutableArray alloc] init];
BOOL hasTorch = NO;
for (AVCaptureDevice *device in devices) {
if ([device hasTorch]) {
[torchDevices addObject:device];
}
}
hasTorch = ([torchDevices count] > 0);
More information is available in the AV Foundation Programming Guide and the AVCaptureDevice Class Reference
You can have less code and use less memory than the code above:
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
BOOL hasTorch = NO;
for (AVCaptureDevice *device in devices) {
if ([device hasTorch]) {
hasTorch = YES;
break;
}
}
hasTorch will now contains the correct value
Swift 4
var deviceHasTorch: Bool {
return AVCaptureDevice.default(for: AVMediaType.video)?.hasTorch == true
}
This code will give your device the ability to turn on the flashlight. But it will also detect if the flashlight is on or off and do the opposite.
- (void)torchOnOff: (BOOL) onOff {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch]) {
[device lockForConfiguration:nil];
if (device.torchMode == AVCaptureTorchModeOff) {
device.torchMode = AVCaptureTorchModeOn;
NSLog(#"Torch mode is on.");
} else {
device.torchMode = AVCaptureTorchModeOff;
NSLog(#"Torch mode is off.");
}
[device unlockForConfiguration];
}
}
Swift 4
if let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo) {
if (device.hasTorch) {
// Device has torch
} else {
// Device does not have torch
}
} else {
// Device does not support video type (and so, no torch)
}
devicesWithMediaType: is now deprecated.
Swift 4:
let discoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: .video, position: .back)
for device in discoverySession.devices {
if device.hasTorch {
return true
}
}
return false