I'm using Apple's AVCam source code to create a custom camera, I'm trying to toggle flash on/off but its not working. Here is my code, not sure what's wrong. I'm new to AVCam.
- (void) toggleFlash:(id)sender {
dispatch_async([self sessionQueue], ^{
AVCaptureDevice *currentVideoDevice = [[self videoDeviceInput] device];
AVCaptureDevicePosition currentPosition = [currentVideoDevice position];
if(currentPosition == AVCaptureDevicePositionUnspecified || currentPosition == AVCaptureDevicePositionBack) {
if([currentVideoDevice hasFlash]) {
[currentVideoDevice lockForConfiguration:nil];
[currentVideoDevice setFlashMode:AVCaptureFlashModeOn];
[currentVideoDevice unlockForConfiguration];
}
}
});
}
Its go through each line in code, and not logs any errors from this but still no luck.
- (void) toggleFlash {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch] && [device hasFlash]){
[device lockForConfiguration:nil];
[device setTorchMode:!device.torchActive];
[device setFlashMode:!device.torchActive];
[device unlockForConfiguration];
}
}
P.S. In my case, torch/flash is off initially.
Related
I am using video recording from AVCam. Auto focus works fine before video recording is started. But it keeps on focusing after the video has started recording which is an annoying behavior. I have disabled video stabilization in AVCaptureConnection but still no use
AVCaptureConnection *connection = [movieFileOutput connectionWithMediaType:AVMediaTypeVideo];
if ([connection isVideoStabilizationSupported]) {
if (SYSTEM_VERSION_LESS_THAN(#"8.0")) {
// For iOS 7
[connection setEnablesVideoStabilizationWhenAvailable:NO];
} else {
connection.preferredVideoStabilizationMode = AVCaptureVideoStabilizationModeOff;
}
}
Please guide me how can I disable auto focus during video recording?
I have called following method from viewDidLoad to achieve required functionality
- (void)disableAutoFocus {
AVCaptureDevice *device = [[self videoDeviceInput] device];
[device lockForConfiguration:nil];
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];
}
}
}
To turn on torch and flashlight I'm using this code:
if ([device hasTorch]) {
[device lockForConfiguration:nil];
[device setTorchMode: on ? AVCaptureTorchModeOn : AVCaptureTorchModeOff];
[device setFlashMode: on ? AVCaptureFlashModeOn : AVCaptureFlashModeOff];
[device setFocusMode:AVCaptureFocusModeAutoFocus];
[device unlockForConfiguration];
}
But if you compare it with the native camera app you will see that the native flashlight is more powerful when capturing photo
Is there a way to make it powerful when capturing a photo similar to native camera flash?
you can update your code with this code:
- (void)setTorchToLevel:(float)torchLevel
{
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch]) {
[device lockForConfiguration:nil];
if (torchLevel <= 0.0) {
[device setTorchMode:AVCaptureTorchModeOff];
}
else {
if (torchLevel >= 1.0)
torchLevel = AVCaptureMaxAvailableTorchLevel;
BOOL success = [device setTorchModeOnWithLevel:torchLevel error:nil];
}
[device unlockForConfiguration];
}
}
hope this will help you.
Try adding the following code.
[device setTorchModeOnWithLevel:AVCaptureMaxAvailableTorchLevel error:nil];
used below code to disable torch was working fine in ios7 ,
but in ios8 torch gets on when dark
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch] || [device hasFlash]){
[device lockForConfiguration:nil];
[device setTorchMode:AVCaptureTorchModeOff];
[device setFlashMode:AVCaptureFlashModeOff];
[device unlockForConfiguration];
}
Try this code it works...
- (IBAction)flashOnClicked:(id)sender
{
AVCaptureDevice *flashLight = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([flashLight isTorchAvailable] && [flashLight isTorchModeSupported:AVCaptureTorchModeOn])
{
BOOL success = [flashLight lockForConfiguration:nil];
if (success)
{
if ([flashLight isTorchActive])
{
//TURN ON
[flashLight setTorchMode:AVCaptureTorchModeOff];
}
else
{
//TURN OFF
[flashLight setTorchMode:AVCaptureTorchModeOn];
}
[flashLight unlockForConfiguration];
}
}
}
I am trying to make an extension for marmalade, which turns on and off the camera flash in iPhone. I am referring to this answer on SO about using camera flash. I've put the exact code what he has mentioned and [device setTorchMode:AVCaptureTorchModeOn]; gets called too. But the flash doesn't respond, as if nothing has happened. Is there anything I need to do, to make it work as a static library, so that I can use it in my extension?
Update:-
I am using iOS-SDK 6.1 to compile the extension and was testing on iPhone 4 (iOS version 4.3.1). But now I am testing on iPhone 5 (iOS version 6.0.0), and now the flash is turning on, but not turning off. I guess this might help.
That code you copied may have an error in it. Try the following:
-(void)turnOnFlash
{
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch]) {
[device lockForConfiguration:nil];
[device setTorchMode:AVCaptureTorchModeOff];
[device unlockForConfiguration];
}
}
I don't know why, but the below code worked for me.
void TurnFlashOn_platform(bool turnOn) {
AVCaptureDevice* device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch]){
[device lockForConfiguration:nil]; //you must lock before setting torch mode
[device setTorchMode:turnOn ? AVCaptureTorchModeOn : AVCaptureTorchModeOff];
[device unlockForConfiguration];
}
}
I guess must be some typos. Thanks anyways.
Use this function to turn on and off the flash light...
#import <AVFoundation/AVFoundation.h>
- (void) turnTorchOn: (bool) on {
Class captureDeviceClass = NSClassFromString(#"AVCaptureDevice");
if (captureDeviceClass != nil) {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch] && [device hasFlash]){
[device lockForConfiguration:nil];
if (on) {
[device setTorchMode:AVCaptureTorchModeOn];
[device setFlashMode:AVCaptureFlashModeOn];
torchIsOn = YES;
} else {
[device setTorchMode:AVCaptureTorchModeOff];
[device setFlashMode:AVCaptureFlashModeOff];
torchIsOn = NO;
}
[device unlockForConfiguration];
}
}
}
How can I turn the iPhone's LED camera flash on/off programatically?
#import <AVFoundation/AVFoundation.h>
...
- (void) turnTorchOn: (bool) on {
// check if flashlight available
Class captureDeviceClass = NSClassFromString(#"AVCaptureDevice");
if (captureDeviceClass != nil) {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch] && [device hasFlash]){
[device lockForConfiguration:nil];
if (on) {
[device setTorchMode:AVCaptureTorchModeOn];
[device setFlashMode:AVCaptureFlashModeOn];
//torchIsOn = YES; //define as a variable/property if you need to know status
} else {
[device setTorchMode:AVCaptureTorchModeOff];
[device setFlashMode:AVCaptureFlashModeOff];
//torchIsOn = NO;
}
[device unlockForConfiguration];
}
} }
I combined the timer with the above code.it worked for me...
- (void)viewDidLoad
{
[super viewDidLoad];
myTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:#selector(toggleFlashlight) userInfo:nil repeats:YES];
// Do any additional setup after loading the view from its nib.
}
- (void) toggleFlashlight
{
// check if flashlight available
Class captureDeviceClass = NSClassFromString(#"AVCaptureDevice");
if (captureDeviceClass != nil) {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch] && [device hasFlash]){
[device lockForConfiguration:nil];
if (device.torchMode == AVCaptureTorchModeOff)
{
[device setTorchMode:AVCaptureTorchModeOn];
[device setFlashMode:AVCaptureFlashModeOn];
//torchIsOn = YES;
}
else
{
[device setTorchMode:AVCaptureTorchModeOff];
[device setFlashMode:AVCaptureFlashModeOff];
// torchIsOn = NO;
}
[device unlockForConfiguration];
}
} }