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];