0

I am working on simple camera app which will take photo without any user interaction by using AV Foundation.. By using timer i have achieved that but now I want to enable auto flash for my back camera

i) Flash should Enable if i am in dark place

ii ) Flash should not enable if i am in sun light or under some light coverage area

- (void)StartTimer
{
    seconds = 1;
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(subtractTime) userInfo:nil repeats:YES];
}

- (void)subtractTime
{
    seconds--;
    // timerLabel.text = [NSString stringWithFormat:@"%02d",(int)seconds];
    if(seconds == 0)
    {
        [timer invalidate];
        session = [[AVCaptureSession alloc]init];
        [session setSessionPreset:AVCaptureSessionPresetPhoto];
        AVCaptureDevice *inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        NSError *error;
        AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:&error];
        if ([session canAddInput:deviceInput])
        {
            [session addInput:deviceInput];
        }
        AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc]initWithSession:session];
        [previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
        CALayer *rootLayer = [[self view]layer];
        [rootLayer setMasksToBounds:YES];
        CGRect frame = self.imageforcapture.frame;
        [previewLayer setFrame:frame];
        [rootLayer insertSublayer:previewLayer atIndex:0];
        StillImageOutput = [[AVCaptureStillImageOutput alloc]init];
        NSDictionary *outputSettings =[[NSDictionary alloc]initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey, nil];
        [StillImageOutput setOutputSettings:outputSettings];
        [session addOutput:StillImageOutput];
        [session startRunning];

        // Timer to take picture

        seconds = 1;
        timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(takePicture) userInfo:nil repeats:YES];

    }
}

-(void)takePicture
{
    AVCaptureConnection *videoConnection  = nil;
    for(AVCaptureConnection *connection in StillImageOutput.connections)
    {
        for(AVCaptureInputPort *port in  [connection inputPorts])
        {
            if ([[port mediaType] isEqual:AVMediaTypeVideo]){
                videoConnection =connection;
                break;

            }
        }
    }


    [StillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error)
    {
        if (imageDataSampleBuffer!=NULL)
        {

            NSData *imageData =[AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
             self.imageArray = [[NSMutableArray alloc]init];
            self.image = [ UIImage imageWithData:imageData];
             [self.imageArray insertObject:self.image  atIndex:0];
            self.image_view.image=[self.imageArray objectAtIndex:0];
        }
    }];
    [timer invalidate];
    seconds = 1;
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(toSwitchFrontCamera) userInfo:nil repeats:YES];
}
Meet Doshi
  • 4,241
  • 10
  • 40
  • 81
Nithya
  • 1,029
  • 2
  • 14
  • 27
  • http://stackoverflow.com/a/10053987/4601170 – Bhavin Bhadani Aug 16 '16 at 08:51
  • 1
    you can access camara flash [Link](http://stackoverflow.com/a/10053987/4003548) and rest functionality could be done by using some tricks. Have a look these links [detect light](http://stackoverflow.com/questions/22753165/detecting-if-iphone-is-in-a-dark-room) and [useful link](http://www.b2cloud.com.au/tutorial/obtaining-luminosity-from-an-ios-camera/).. by understand the whole environment you can achieve – vaibhav Aug 16 '16 at 10:12

1 Answers1

1

You can use this with your code to enable auto flash.

  - (void) setAutoFlash {

    Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
    if (captureDeviceClass != nil) {
        AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        if ([device hasFlash]){

            [device lockForConfiguration:nil];

            [device setTorchMode:AVCaptureFlashModeAuto];

            [device unlockForConfiguration];
        }
    }
}
Surjeet Singh
  • 11,691
  • 2
  • 37
  • 54
  • Thanks surjeet for your answer.. But i want to enable the flash depending upon the environment.. please have a look at my question one more time .. i have edited it .. – Nithya Aug 16 '16 at 09:18
  • Configure flash to Auto Mode will work as you want. If you are in a dark place, flash will work when captured image, and if in proper light, that will not work. – Surjeet Singh Aug 16 '16 at 10:19