我正在开发简单的相机应用程序,它将通过使用AV基础而无需任何用户交互拍照。通过使用计时器我已经实现了但现在我想为我的后置相机启用自动闪光
i)如果我在黑暗的地方,应该启用Flash
ii)如果我处于阳光下或某些光线覆盖范围内,则闪光灯不应启用
- (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];
}
答案 0 :(得分:1)
您可以将其与代码一起使用以启用自动闪光。
- (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];
}
}
}