I am using AVFoundation to take a picture automatically, but the pictures are coming out very dark. The back camera is ok if you use the flash on an iPhone 4, but the front camera is basically a black square with only the faintest outline of the objects in the picture.
I have the following set for both cameras, but it does not seem to make a difference:
if (device.hasFlash) {
NSLog(#"device.hasFlash turning flash mode on");
[device lockForConfiguration:&deviceError];
device.flashMode = AVCaptureFlashModeOn;
[device unlockForConfiguration];
}
else {
NSLog(#"Device does not have Flash");
}
if ([device isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus]) {
NSLog(#"Enabling ContinuousAutoFocus");
[device lockForConfiguration:&deviceError];
device.focusMode = AVCaptureFocusModeContinuousAutoFocus;
[device unlockForConfiguration];
}
else {
NSLog(#"Device does not support ContinuousAutoFocus");
}
if ([device isExposureModeSupported:AVCaptureExposureModeContinuousAutoExposure]) {
NSLog(#"Enabling ContinuousAutoExposure");
[device lockForConfiguration:&deviceError];
device.exposureMode = AVCaptureExposureModeContinuousAutoExposure;
[device unlockForConfiguration];
}
else {
NSLog(#"Device does not support ContinuousAutoExposure");
}
if ([device isWhiteBalanceModeSupported:AVCaptureWhiteBalanceModeContinuousAutoWhiteBalance]) {
NSLog(#"Enabling ContinuousAutoWhiteBalance");
[device lockForConfiguration:&deviceError];
device.whiteBalanceMode = AVCaptureWhiteBalanceModeContinuousAutoWhiteBalance;
[device unlockForConfiguration];
}
else {
NSLog(#"Device does not support ContinuousAutoWhiteBalance");
}
Any Ideas?
The front-facing camera takes a while to adjust its white balance. You may need to KVO the isAdjustingWhiteBalance device property and only trigger the capture after the property has gone from NO to YES and back to NO for the first time.
I also had this issue, as I created AVCaptureStillImageOutput before every capture. But some time is required for auto setup.
So to fix it I create AVCaptureStillImageOutput once with AVCaptureSession creation
Related
how can i develop or is there any way to achieve the flashlight flash on the beat of the music?
i have search over google for the functionality but won't found anything that will draw me the exact way so i would like to have some expert advice that if anyone had done that before or have an idea how to do it?
looking for something like this video
i know how to turn on and off the flash light here is the code but don't know how to handle that on beat of music
-(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];
}
}
}
please help thanks in advance :)
Having no knowledge whatsoever regarding Audio-technology I have stumbled upon a tutorial that should help you out pretty easily. The tutorial is for a music visualizer however the foundation is alike.
http://www.raywenderlich.com/36475/how-to-make-a-music-visualizer-in-ios
I want to turn on torch mode AVCaptureTorchModeOn in my app while doing video recording.
I m using below code.
-(void)set_TorchMode:(BOOL)turnOn
{
AVCaptureDevice *theDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([theDevice hasTorch]) {
[theDevice lockForConfiguration: nil];
AVCaptureTorchMode currentMode = [theDevice torchMode];
BOOL isAlreadyTurnedOn = (AVCaptureTorchModeOn == currentMode);
if (isAlreadyTurnedOn != turnOn) {
[theDevice setTorchMode: turnOn? AVCaptureTorchModeOn: AVCaptureTorchModeOff];
}
[theDevice unlockForConfiguration];
}
}
I m calling this method while start recording to turn ON and while stop recording to turn it OFF.
Its working fine for me first time when i record, but while start recording second time, its turn on but immediately turns OFF.Its not keeping ON while recording is running.
Thanks for any help.
Following code is implemented for the turn on and off back light .
May this helping to you,
- (void) setTorchOn:(BOOL)isOn
{
AVCaptureDevice* device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
[device lockForConfiguration:nil]; //you must lock before setting torch mode
[device setTorchMode:isOn ? AVCaptureTorchModeOn : AVCaptureTorchModeOff];
[device unlockForConfiguration];
}
- (IBAction)changedState:(id)sender {
UISwitch *switchValue = (UISwitch*)sender;
[self setTorchOn:[switchValue isOn]];
}
please test this code into the devices.
I'm currently working on a small project, the meaning of the project/app is that if you were to shake the device the flash ignites. If you shake it again the light turns off!
The first 3/4 toggles between light on or off are working ok, but after 3/4 toggles it is imposible to turn of the light since the device doesnt detect a shake.
Another small problem (since iOS 5.0) is that if a motion is detected the flash will blink very short (1 sec) and than power on. This looks like a short flash.
What am i doing wrong
Detecting the shake:
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (event.type == UIEventSubtypeMotionShake) {
NSLog(#"shaken, not stirred.");
[self playSound];
[self toggleFlashlight];
}
}
Toggling the Flash ligt:
- (void)toggleFlashlight
{
AVCaptureDevice *device =
[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch] && [device hasFlash]){
if (device.torchMode == AVCaptureTorchModeOff) {
NSLog(#"It's currently off.. turning on now.");
AVCaptureDeviceInput *flashInput =
[AVCaptureDeviceInput deviceInputWithDevice:device error: nil];
AVCaptureVideoDataOutput *output =
[[AVCaptureVideoDataOutput alloc] init];
AVCaptureSession *session =
[[AVCaptureSession alloc] init];
[session beginConfiguration];
[device lockForConfiguration:nil];
[device setTorchMode:AVCaptureTorchModeOn];
[device setFlashMode:AVCaptureFlashModeOn];
[session addInput:flashInput];
[session addOutput:output];
[device unlockForConfiguration];
[output release];
[session commitConfiguration];
[session startRunning];
[self setAVSession:session];
[session release];
}
else {
NSLog(#"It's currently on.. turning off now.");
[AVSession stopRunning];
}
}
}
I would really appreciate your expertise in solving the problem for me!
Well, I don't know much about it, but it's possible you're starting/stopping the session too quickly (calling ToggleFlashLight too soon after last calling it). You might see this code here... It suggests that you do all that (except for the Torch/FlashModeOn) once to create the session, and then just do the lock/torchModeOn/flashModeOn/unlock to turn on and lock/torchModeOff/flashModeOff/unlock to turn off.
Calling your code in
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
instead of motionEnded might solve your problems.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Turn on torch/flash on iPhone 4
I just want to be able to turn on the led light. Is there a simple way to do this, or am I going to need to, say, set up the phone to take a video, simulate it videoing with the light on, but not save the video? Something like that? Thanks.
Try this, it worked fine for me.
#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];
}
}
}
I want to disable the auto focus feature in the iPhone 3G S.
How i can do it programmatically?
I used custom UIImagePickerController for my camera application. I don't need this feature in my application.
please help me.
The following code should work for what you want:
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];
}
}
Seen here!
Generally your application shouldn't be changing this setting, if your users don't want to use the auto-focus then they can disable it, but your app shouldn't make that choice for them.
Is there a good reason why you would need to control that? Does it interfere with your desired functionality in some way?