Detect LED Flash for Alerts setting status in code - iphone

How can I detect "LED Flash for Alerts" (from Settings) status in code? I have tried:
AVCaptureDevice *torched=[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
BOOL test = torched.torchMode;
BOOL test1 = torched.flashMode;
But it seams that it is not what I need.

Do like this,
BOOL test=NO;
BOOL test1=NO;
Class captureDeviceClass = NSClassFromString(#"AVCaptureDevice");
if (captureDeviceClass != nil) {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch] && [device hasFlash]){
if(device.flashMode==AVCaptureFlashModeOn)
NSLog(#"ON");
else
NSLog(#"OFF");
}
}
Hope it will helps you....

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 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

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

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.