1.公司做一个二维码条形码扫描的项目,遇到二维码扫描很好识别,条形码扫描有点慢
解决办法:device放大焦距(完美解决)
//获取摄像设备
AVCaptureDevice * device = [AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeVideo];
//创建输入流
AVCaptureDeviceInput * input = [AVCaptureDeviceInputdeviceInputWithDevice:device error:nil];
//创建输出流
AVCaptureMetadataOutput * output = [[AVCaptureMetadataOutputalloc]init];
//设置代理在主线程里刷新
[output setMetadataObjectsDelegate:selfqueue:dispatch_get_main_queue()];
output.rectOfInterest = scanCrop;
// [ output setRectOfInterest : CGRectMake (( 124 )/ ScreenHEI ,(( ScreenWHD - 220 )/ 2 )/ ScreenWHD , 220 / ScreenHEI , 220 / ScreenWHD )];
//初始化链接对象
session = [[AVCaptureSessionalloc]init];
//高质量采集率
[sessionsetSessionPreset:AVCaptureSessionPresetHigh];
if (input) {
[sessionaddInput:input];
}
if (output) {
[sessionaddOutput:output];
//设置扫码支持的编码格式(如下设置条形码和二维码兼容)
NSMutableArray *a = [[NSMutableArrayalloc] init];
if ([output.availableMetadataObjectTypescontainsObject:AVMetadataObjectTypeQRCode]) {
[a addObject:AVMetadataObjectTypeQRCode];
}
if ([output.availableMetadataObjectTypescontainsObject:AVMetadataObjectTypeEAN13Code]) {
[a addObject:AVMetadataObjectTypeEAN13Code];
}
if ([output.availableMetadataObjectTypescontainsObject:AVMetadataObjectTypeEAN8Code]) {
[a addObject:AVMetadataObjectTypeEAN8Code];
}
if ([output.availableMetadataObjectTypescontainsObject:AVMetadataObjectTypeCode128Code]) {
[a addObject:AVMetadataObjectTypeCode128Code];
}
output.metadataObjectTypes=a;
}
/*
[ _output setRectOfInterest : CGRectMake (( 124 )/ ScreenHigh ,(( ScreenWidth - 220 )/ 2 )/ ScreenWidth , 220 / ScreenHigh , 220 / ScreenWidth )];
*/
AVCaptureVideoPreviewLayer * layer = [AVCaptureVideoPreviewLayerlayerWithSession:session];
layer.videoGravity=AVLayerVideoGravityResizeAspectFill;
layer.frame = CGRectMake(0, 0, [UIScreenmainScreen].bounds.size.width, self.ScanReadView.frame.size.height);
[self.ScanReadView.layerinsertSublayer:layer atIndex:0];
[selfsetOverlayPickerView:self.ScanReadView];
//放大焦距
NSError *error = nil;
[device lockForConfiguration:&error];
if (device.activeFormat.videoMaxZoomFactor > 2) {
device.videoZoomFactor = 2;
}else{
device.videoZoomFactor = device.activeFormat.videoMaxZoomFactor;
}
[device unlockForConfiguration];
//开始捕获
[sessionstartRunning];