我正在使用下面的代码扫描QRCode和BarCode,当我从笔记本电脑的屏幕上扫描代码以及纸上的任何打印代码都能快速工作时,它工作得很好。但是当我从其他任何移动设备(如android手机)进行扫描时,它不会快速扫描。
scanRect = CGRectMake(scanRect.origin.y/windowSize.height, scanRect.origin.x/windowSize.width, scanRect.size.height/windowSize.height,scanRect.size.width/windowSize.width);
self.device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
self.input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
self.output = [[AVCaptureMetadataOutput alloc]init];
[self.output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
self.session = [[AVCaptureSession alloc]init];
[self.session setSessionPreset:([UIScreen mainScreen].bounds.size.height<500)?AVCaptureSessionPreset640x480:AVCaptureSessionPresetHigh];
AVCaptureDevice *frontDevice;
AVCaptureDeviceInput *frontDeviceInput;
for (AVCaptureDevice *device in [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) {
if (device.position == AVCaptureDevicePositionFront) {
frontDevice = device;
}
}
if (frontDevice) {
frontDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:frontDevice error:nil];
}
[_session addInput:frontDeviceInput];
[self.session addOutput:self.output];
self.output.metadataObjectTypes= self.output.availableMetadataObjectTypes;//@[AVMetadataObjectTypeQRCode,AVMetaObject];
self.output.rectOfInterest = scanRect;
self.preview = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
self.preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
self.preview.frame = CGRectMake(0, 0, _scanView.frame.size.width, _scanView.frame.size.height); //[UIScreen mainScreen].bounds;
self.previewLayerConnection=self.preview.connection;
if ([self.previewLayerConnection isVideoOrientationSupported])
{
self.previewLayerConnection.videoOrientation = [self videoOrientationFromCurrentDeviceOrientation];
}
[self.scanView.layer insertSublayer:self.preview atIndex:0];
[self.session startRunning];