我使用 AVFoundation 创建了自己的扫描仪。我添加了一个感兴趣的矩形来限制扫描区域。它在屏幕上添加了一条红线,便于扫描。我在 IPhone 6s plus , Iphone 6 , Iphone 5 , Ipad 2 gen上尝试过,甚至在< em> Ipod 并且在那里工作正常。 但是如果我在 Ipad Mini 上尝试1代和2代,它就不会打开相机。我也没有在日志中收到任何错误消息。这就是我添加红线以限制scannig区域的方法。
_output.rectOfInterest = [_videoPreviewLayer metadataOutputRectOfInterestForRect:[self scanArea]];
- (CGRect) scanArea {
float scanRectHeight = 4.0f;
CGRect scanRect = CGRectMake(0, ((_screenHeight/2) - (scanRectHeight/2)), _screenWidth, scanRectHeight);
NSLog(@"screenHeight/2: %f scanRectHeight/2: %f", _screenHeight/2, scanRectHeight/2);
NSLog(@"screenHeight/2 - scanRectHeight/2: %f", ((_screenHeight/2)-(scanRectHeight/2)));
NSLog(@"%@", NSStringFromCGRect(scanRect));
UIView *scanAreaView = [[UIView alloc] initWithFrame:scanRect];
UIColor *borderColor = [UIColor colorWithRed:1.0f green:0.0f blue:0.0f alpha:0.5f];
scanAreaView.backgroundColor = borderColor;
scanAreaView.layer.zPosition = 1;
[self.view addSubview:scanAreaView];
return scanRect;
}
-(void) setScreenSize {
_screenHeight = [[UIScreen mainScreen] bounds].size.height;
_screenWidth = [[UIScreen mainScreen] bounds].size.width;
_screenScaleFactor = [UIScreen mainScreen].scale;
if (_screenWidth > _screenHeight) {
float tempHeight = _screenWidth;
_screenWidth = _screenHeight;
_screenHeight = tempHeight;
}
}
- (BOOL) startReading {
NSLog(@"startReading...");
_captureSession = [[AVCaptureSession alloc] init];
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if(input) {
[_captureSession addInput:input];
} else {
NSLog(@"error: %@", error);
return NO;
}
_output = [[AVCaptureMetadataOutput alloc] init];
if(_output)
[_captureSession addOutput:_output];
else
NSLog(@"Could not add output!");
[_output setMetadataObjectTypes:[self defaultMetaDataObjectTypes]];
[_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
_videoPreviewLayer = [AVCaptureVideoPreviewLayer layerWithSession:_captureSession];
_videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
if(UIInterfaceOrientationIsPortrait(orientation)) {
_videoPreviewLayer.bounds = CGRectMake(0, 0, self.view.layer.bounds.size.width, self.view.layer.bounds.size.height);
_videoPreviewLayer.position = CGPointMake(CGRectGetMidX(self.view.layer.bounds), CGRectGetMidY(self.view.layer.bounds));
}
else {
_videoPreviewLayer.bounds = CGRectMake(0, 0, self.view.layer.bounds.size.height, self.view.layer.bounds.size.width);
_videoPreviewLayer.position = CGPointMake(CGRectGetMidY(self.view.layer.bounds), CGRectGetMidX(self.view.layer.bounds));
}
[self initializeBoundingBox];
[_captureSession startRunning];
_output.rectOfInterest = [_videoPreviewLayer metadataOutputRectOfInterestForRect:[self scanArea]];
NSLog(@"_output.rectOfInterest: %@", NSStringFromCGRect(_output.rectOfInterest));
[self.view.layer addSublayer:_videoPreviewLayer];
return YES;
}
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
NSLog(@"captureOutput...");
for (AVMetadataObject *metadata in metadataObjects) {
AVMetadataMachineReadableCodeObject *transformed = (AVMetadataMachineReadableCodeObject *)[_videoPreviewLayer transformedMetadataObjectForMetadataObject:metadata];
_boundingBox.frame = transformed.bounds;
_boundingBox.hidden = NO;
NSArray *translatedCorners = [self translatePoints:transformed.corners fromView:self.view toView:_boundingBox];
_boundingBox.corners = translatedCorners;
_scannedString = transformed.stringValue;
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate );
[self stopReading:nil];
}
}
我记录了一些值以尝试查看差异。在我之前和之后给出 metadataOutputRectOfInterestForRect()的矩形
- Iphone 6s plus
赋予 metadataOutputRectOfInterestForRect: {{0, 366}, {414, 4}}
_output.rectOfInterest: {{0.49728260869565211, 0}, {0.0054347826086956763, 1}}
- Ipad 2 gen
赋予 metadataOutputRectOfInterestForRect: {{0, 510}, {768, 4}}
_output.rectOfInterest: {{0.49853516, -2.9802322e-08}, {0.0029296875, 1}}
- Ipad mini 1 gen
赋予 metadataOutputRectOfInterestForRect: {{0, 510}, {768, 4}}
_output.rectOfInterest: {{0.49853516, -2.9802322e-08}, {0.0029296875, 1}}
- iphone 6s
赋予 metadataOutputRectOfInterestForRect: {{0, 331.5}, {375, 4}}
_output.rectOfInterest: {{0.49700149925037473, 0}, {0.0059970014992504206, 1}}
这个负值在开始时对我很突出,但我试图添加 fabsf(),它根本没有改变这个值。此值也出现在 Ipad 上,并且工作正常。
以下是相机未打开时扫描仪的屏幕截图:
所以我的问题是为什么它适用于所有这些设备接受 Ipad mini ,那里有一些我不知道的差异吗?
编辑:添加了更多代码段