这里对于扫描功能不会过多的说明,毕竟只是一个小笔记而已。
简单的记录扫描所需部件:
/**
- 会话对象
*/
-(AVCaptureSession *)session
{
if (!_session) {
_session = [AVCaptureSession new];
[_session setSessionPreset: AVCaptureSessionPresetHigh]; //高质量采集
[self setupIODevice];
}
return _session;
}
/**
- 视频输入设备
*/
- (AVCaptureDeviceInput *)input
{
if (!_input) {
AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType: AVMediaTypeVideo];
_input = [AVCaptureDeviceInput deviceInputWithDevice: device error: nil];
}
return _input;
}
/**
- 数据输出对象
*/
-(AVCaptureMetadataOutput *)output
{
if (!_output) {
_output = [AVCaptureMetadataOutput new];
[_output setMetadataObjectsDelegate: self queue: dispatch_get_main_queue()];
}
return _output;
}
/**
- 扫描视图
*/
-(AVCaptureVideoPreviewLayer *)scanView
{
if (!_scanView) {
_scanView = [AVCaptureVideoPreviewLayer layerWithSession: self.session];
_scanView.videoGravity = AVLayerVideoGravityResizeAspectFill;
_scanView.frame = self.bounds;
}
return _scanView;
}
/**
- 扫描范围
*/
-(CGRect)scanRect
{
if (CGRectEqualToRect(_scanRect, CGRectZero)) {
CGRect rectOfInterest = self.output.rectOfInterest;
CGFloat yOffset = rectOfInterest.size.width - rectOfInterest.origin.x;
CGFloat xOffset = 1 - 2 * SCANSPACEOFFSET;
_scanRect = CGRectMake(rectOfInterest.origin.y * SCREENWIDTH, rectOfInterest.origin.x * (SCREENHEIGHT-NAV_BAR_HEIGHT), xOffset * SCREENWIDTH, yOffset * (SCREENHEIGHT-NAV_BAR_HEIGHT));
}
return _scanRect;
}
主要的代理方法AVCaptureMetadataOutputObjectsDelegate
/**
- 二维码扫描数据返回
*/
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
if (metadataObjects.count > 0) {
[self stop];
AVMetadataMachineReadableCodeObject * metadataObject = metadataObjects[0];
这里你将metadataObject.stringValue获取的字符串返回即可
}}
/**
- 二维码扫描时获取光度来判断是否需要打开闪关灯
*/
-(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];
NSLog(@"%f",brightnessValue);//这里获取的就是外部环境的光度,你可以根据的光度的大小来进行判断是否需要打开闪关灯,值越大越亮
// 根据brightnessValue的值来打开和关闭闪光灯
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
BOOL result = [device hasTorch];// 判断设备是否有闪光灯
if ((brightnessValue < 0) && result) {// 打开闪光灯
[device lockForConfiguration:nil];
[device setTorchMode: AVCaptureTorchModeOn];//
[device unlockForConfiguration];
}else if((brightnessValue > 0) && result) {// 关闭闪光灯
[device lockForConfiguration:nil];
[device setTorchMode: AVCaptureTorchModeOff];//关
[device unlockForConfiguration];
}
}