1.如果是自己设置摄像头:
// 1.获取硬件设备
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
// 2.创建输入流
AVCaptureDeviceInput *input = [[AVCaptureDeviceInput alloc]initWithDevice:device error:nil];
// 3.创建设备输出流
AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
[output setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
// AVCaptureSession属性
self.session = [[AVCaptureSession alloc]init];
// 设置为高质量采集率
[self.session setSessionPreset:AVCaptureSessionPresetHigh];
// 添加会话输入和输出
if ([self.session canAddInput:input]) {
[self.session addInput:input];
}
if ([self.session canAddOutput:output]) {
[self.session addOutput:output];
}
// 9.启动会话
[self.session startRunning];
. 在下边方法中解决:
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection{
CFDictionaryRef metadataDict = CMCopyDictionaryOfAttachments(NULL,sampleBuffer, kCMAttachmentMode_ShouldPropagate);
NSDictionary *metadata = [[NSMutableDictionary alloc] initWithDictionary:(__bridge NSDictionary*)metadataDict];
CFRelease(metadataDict);
NSDictionary *exifMetadata = [[metadata objectForKey:(NSString *)kCGImagePropertyExifDictionary] mutableCopy];
float brightnessValue = [[exifMetadata objectForKey:(NSString *)kCGImagePropertyExifBrightnessValue] floatValue];
// brightnessValue 值代表光线强度,值越小代表光线越暗
if (brightnessValue <= -2 && !_isAutoOpen) {
_isAutoOpen = YES;
[self.torchBtn setSelected:YES];
[self turnTorchOn:YES];
}
}
2.七牛云视频数据获取弱光校验:
///获取视频数据
func shortVideoRecorder(_ recorder: PLShortVideoRecorder, cameraSourceDidGet pixelBuffer: CVPixelBuffer) -> Unmanaged<CVPixelBuffer> {
if let metadataDict = CMCopyDictionaryOfAttachments(allocator: nil, target: pixelBuffer, attachmentMode: kCMAttachmentMode_ShouldPropagate){
let meta = NSMutableDictionary(dictionary: metadataDict)
self.autoLight = false
if let MetadataDictionary:NSDictionary = meta.object(forKey: "MetadataDictionary") as? NSDictionary{
if let normalizedSNR:CGFloat = MetadataDictionary["NormalizedSNR"] as? CGFloat{
// LZPLog(normalizedSNR)
if normalizedSNR <= 4{
self.autoLight = true
}
}
}
}
//MARK:...释放资源,如果不释放,则资源会爆掉...
let unmanaged = Unmanaged.passRetained(pixelBuffer)
unmanaged.autorelease()
return unmanaged
}
上述方法是视频输出数据,下边记得要释放数据,不然会卡爆;
NormalizedSNR字段会根据光线的强弱变化;