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];
}
}
}
Related
I'm building this code for future learning and fun. It supposed to open the camera and enable the touch focus. It works in the rear-facing camera, but nothing happens when i flip it to the front camera. I have searched alot, but could not find any code fix to that problem. Could anyone please show me step by step with code how to make touch focus also work in the front camera view?
Down below is the focus function:
- (void) focus:(CGPoint) aPoint{
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if([device isFocusPointOfInterestSupported] &&
[device isFocusModeSupported:AVCaptureFocusModeAutoFocus]) {
CGRect screenRect = [[UIScreen mainScreen] bounds];
double screenWidth = screenRect.size.width;
double screenHeight = screenRect.size.height;
double focus_x = aPoint.x/screenWidth;
double focus_y = aPoint.y/screenHeight;
if([device lockForConfiguration:nil]) {
if([self.delegate respondsToSelector:#selector(scanViewController:didTapToFocusOnPoint:)]) {
[self.delegate scanViewController:self didTapToFocusOnPoint:aPoint];
}
[device setFocusPointOfInterest:CGPointMake(focus_x,focus_y)];
[device setFocusMode:AVCaptureFocusModeAutoFocus];
if ([device isExposureModeSupported:AVCaptureExposureModeAutoExpose]){
[device setExposureMode:AVCaptureExposureModeAutoExpose];
}
[device unlockForConfiguration];
}
}
}
The whole source can be found here:
https://gist.github.com/Alex04/6976945
UPDATE:
if ([device isExposurePointOfInterestSupported])
{
[device lockForConfiguration:&error];
[device setExposurePointOfInterest:aPoint];
if ([device isExposureModeSupported:AVCaptureExposureModeContinuousAutoExposure])
{
[device setExposureMode:AVCaptureExposureModeContinuousAutoExposure];
}
[device setWhiteBalanceMode:AVCaptureWhiteBalanceModeContinuousAutoWhiteBalance];
[device unlockForConfiguration];
}
I don't think the front-facing camera supports tap-to-focus on any iPhone. You can check for support by checking the focusPointOfInterestSupported property of the AVCaptureDevice.
If you want to implement tap-for-exposure, check the device's exposurePointOfInterestSupported property. If the device supports the feature, set the exposurePointOfInterest property to use it.
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];
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.
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];
}
}
}
Update : I know how to turn on/off the camera flash. What I want to know is if the camera flash is already lit or not.
I would like to know if camera flash is lit or not on iPhone, but I haven't found any method in UIImagePickerController which allows me to do this. I know we can get the cameraFlashMode. But I want to know if the camera flash is already lit or not.
For example, if the mode is UIImagePickerControllerCameraFlashModeAuto, the camera flash could be lit or not before I take the control, and I want to know the state of camera flash before doing some operations.
Surprising this is really unanswered the first person that answered didn't even answer the question...
func torchButtonPressed() {
//
let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
do {
try device.lockForConfiguration()
} catch {
return
}
if device.torchMode == AVCaptureTorchMode.Off {
do {
device.torchMode = AVCaptureTorchMode.On
try device.setTorchModeOnWithLevel(AVCaptureMaxAvailableTorchLevel)
} catch {
print("no torch")
return
}
} else {
device.torchMode = AVCaptureTorchMode.Off
}
device.unlockForConfiguration()
}
You can use following code to find that
#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) {
NSLog(#"Torch is ON");
} else {
NSLog(#"Torch is OFF");
}
[device unlockForConfiguration];
}
}
}
Happy Coding...!!!
I went through the same issue as yours.
iOS supports two modes - Flash Light & Torch. The code below checks if each is available & then if turns them on or off depending on which one you call. Also it checks if the light is already on/off.
Flash On -
-(void)flashOn {
Class captureDeviceClass = NSClassFromString(#"AVCaptureDevice");
if (captureDeviceClass != nil) {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
[device lockForConfiguration:nil];
if ([device hasFlash]) {
if ([device flashMode] == AVCaptureFlashModeOff) {
[device setFlashMode:AVCaptureFlashModeOn];
}
}
if ([device hasTorch]) {
if ([device torchMode] == AVCaptureTorchModeOff) {
[device setTorchMode:AVCaptureTorchModeOn];
}
}
[device unlockForConfiguration];
}
}
Flash Off-
-(void)flashOff {
Class captureDeviceClass = NSClassFromString(#"AVCaptureDevice");
if (captureDeviceClass != nil) {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
[device lockForConfiguration:nil];
if ([device hasFlash]) {
if ([device flashMode] == AVCaptureFlashModeOn) {
[device setFlashMode:AVCaptureFlashModeOff];
}
}
if ([device hasTorch]) {
if ([device torchMode] == AVCaptureTorchModeOn) {
[device setTorchMode:AVCaptureTorchModeOff];
}
}
[device unlockForConfiguration];
}
}