Ios Xcode AVCapturesession扫描特定帧中的条形码(rectOfInterest不工作)

Ios Xcode AVCapturesession扫描特定帧中的条形码(rectOfInterest不工作),ios,objective-c,xcode,barcode-scanner,avmetadataitem,Ios,Objective C,Xcode,Barcode Scanner,Avmetadataitem,我正在尝试为我目前正在开发的应用程序设计条形码扫描仪。我希望扫描器预览能填满设备的整个屏幕,并提供一个较小的框架来指向条形码。一切都在按我所希望的方式进行,但我无法让兴趣框架发挥作用 以下是条形码扫描仪的实现: #import "GEScannerViewController.h" @import AVFoundation; @interface GEScannerViewController () <AVCaptureMetadataOutputObjectsDelegate>

我正在尝试为我目前正在开发的应用程序设计条形码扫描仪。我希望扫描器预览能填满设备的整个屏幕,并提供一个较小的框架来指向条形码。一切都在按我所希望的方式进行,但我无法让兴趣框架发挥作用

以下是条形码扫描仪的实现:

#import "GEScannerViewController.h"
@import AVFoundation;

@interface GEScannerViewController () <AVCaptureMetadataOutputObjectsDelegate> {
    AVCaptureSession *_session;
    AVCaptureDevice *_device;
    AVCaptureDeviceInput *_input;
    AVCaptureMetadataOutput *_output;
    AVCaptureVideoPreviewLayer *_prevLayer;

    UIView *_greyView;
    UIView *_highlightView;
    UIView *_scopeView;
    UILabel *_label;
}
@end

@implementation GEScannerViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    _label = [[UILabel alloc] init];
    _label.frame = CGRectMake(0, self.view.bounds.size.height - 40, self.view.bounds.size.width, 40);
    _label.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    _label.backgroundColor = [UIColor colorWithWhite:0.15 alpha:0.65];
    _label.textColor = [UIColor whiteColor];
    _label.textAlignment = NSTextAlignmentCenter;
    _label.text = @"(none)";
    [self.view addSubview:_label];

    NSError *error = nil;

    _session = [[AVCaptureSession alloc] init];
    _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    [_device lockForConfiguration:&error];

    if (error) {
        NSLog(@"Error: %@", error);
    }

    _device.focusPointOfInterest = CGPointMake(self.view.frame.size.width / 2, (self.view.frame.size.height / 2) - 80);

    _input = [AVCaptureDeviceInput deviceInputWithDevice:_device error:&error];
    if (_input) {
        [_session addInput:_input];
    } else {
        NSLog(@"Error: %@", error);
    }

    _output = [[AVCaptureMetadataOutput alloc] init];
    [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    _output.rectOfInterest = CGRectMake((self.view.frame.size.width / 2) - 160, (self.view.frame.size.height / 2) - 160, 320, 160);
    [_session addOutput:_output];

    _output.metadataObjectTypes = [_output availableMetadataObjectTypes];

    _prevLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session];
    _prevLayer.frame = self.view.bounds;
    _prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [self.view.layer addSublayer:_prevLayer];

    _greyView = [[UIView alloc] initWithFrame:self.view.frame];
    _greyView.bounds = self.view.bounds;
    _greyView.backgroundColor = [UIColor colorWithWhite:0.15 alpha:0.65];
    [self.view.layer addSublayer:_greyView.layer];

    _scopeView = [[UIView alloc] initWithFrame:CGRectMake((self.view.frame.size.width / 2) - 160, (self.view.frame.size.height / 2) - 160, 320, 160)];
    _scopeView.backgroundColor = [UIColor clearColor];
    _scopeView.layer.borderColor = [UIColor greenColor].CGColor;
    _scopeView.layer.borderWidth = 1;
    _scopeView.clipsToBounds = YES;
    [self.view addSubview:_scopeView];

    _highlightView = [[UIView alloc] init];
    _highlightView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleBottomMargin;
    _highlightView.layer.borderColor = [UIColor greenColor].CGColor;
    _highlightView.layer.borderWidth = 3;
    [_scopeView addSubview:_highlightView];

    [_session startRunning];

    [self.view bringSubviewToFront:_highlightView];
    [self.view bringSubviewToFront:_label];
}
#导入“gescannervewcontroller.h”
@进口基金会;
@接口GEScannerViewController(){
AVCaptureSession*\u会话;
AVCaptureDevice*\u设备;
AVCaptureDeviceInput*\u输入;
AVCaptureMataOutput*\u输出;
AVCaptureVideoPreviewLayer*\u prevLayer;
UIView*\u greyView;
UIView*_highlightView;
UIView*\u范围视图;
UILabel*\u标签;
}
@结束
@Gescannervicewcontroller的实现
-(无效)viewDidLoad{
[超级视图下载];
_label=[[UILabel alloc]init];
_label.frame=CGRectMake(0,self.view.bounds.size.height-40,self.view.bounds.size.width,40);
_label.autoresizingMask=ui视图autoresizingflexibletopmargin;
_label.backgroundColor=[UIColor colorWithWhite:0.15 alpha:0.65];
_label.textColor=[UIColor-whiteColor];
_label.textAlignment=NSTextAlignmentCenter;
_label.text=@“(无)”;
[self.view addSubview:_label];
n错误*错误=nil;
_会话=[[AVCaptureSession alloc]init];
_device=[AvCaptureDeviceDefaultDeviceWithMediaType:AVMediaTypeVideo];
[\u设备锁定配置:&错误];
如果(错误){
NSLog(@“错误:%@”,错误);
}
_device.focusPointOfInterest=CGPointMake(self.view.frame.size.width/2,(self.view.frame.size.height/2)-80);
_输入=[AvCaptureDeviceInputDeviceInputWithDevice:_设备错误:&错误];
如果(_输入){
[_会话添加输入:_输入];
}否则{
NSLog(@“错误:%@”,错误);
}
_输出=[[AvCaptureMataOutput alloc]init];
[\u output setMetadataObjectsDelegate:self queue:dispatch\u get\u main\u queue()];
_output.rectOfInterest=CGRectMake((self.view.frame.size.width/2)-160,(self.view.frame.size.height/2)-160320160);
[_sessionaddoutput:_output];
_output.metadataObjectTypes=[\u output availableMetadataObjectTypes];
_prevLayer=[AvCaptureVideoPreviewLayerWithSession:_session];
_prevLayer.frame=self.view.bounds;
_prevLayer.videoGravity=AVLayerVideoGravityResizeAspectFill;
[self.view.layer addSublayer:_prevLayer];
_greyView=[[UIView alloc]initWithFrame:self.view.frame];
_greyView.bounds=self.view.bounds;
_greyView.backgroundColor=[UIColor颜色与白色:0.15 alpha:0.65];
[self.view.layer addSublayer:_greyView.layer];
_scopeView=[[UIView alloc]initWithFrame:CGRectMake((self.view.frame.size.width/2)-160,(self.view.frame.size.height/2)-160320160)];
_scopeView.backgroundColor=[UIColor clearColor];
_scopeView.layer.borderColor=[UIColor greenColor].CGColor;
_scopeView.layer.borderWidth=1;
_scopeView.clipsToBounds=是;
[self.view addSubview:_scopeView];
_highlightView=[[UIView alloc]init];
_highlightView.autoresizingMask=UIViewAutoresizingFlexibleTopMargin | uiviewautoresizingflexibleleleftmargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
_highlightView.layer.borderColor=[UIColor greenColor].CGColor;
_highlightView.layer.borderWidth=3;
[[u scopeView addSubview:[u highlightView];
[_会话开始固定];
[self.view将subview带到前面:_highlightView];
[self.view将子视图带到前面:_标签];
}

我正在使用_output.rectOfInterest指定与_scopeView的帧相同的帧。不幸的是,这不起作用。如果我这样做,就不会再识别条形码了。

我一拿到条形码,就非常清楚:

AVCaptureMataOutput是由像素定义的,因此为了将显示坐标映射到输出中的坐标,我必须使用metadataOutputRectOfInterestForRect:

From AVCaptureOutput.h:


/*!
@method metadataOutputRectOfInterestForRect:
@abstract
Converts a rectangle in the receiver's coordinate space to a rectangle of interest in the coordinate space of an AVCaptureMetadataOutput
whose capture device is providing input to the receiver.

@param rectInOutputCoordinates
A CGRect in the receiver's coordinates.

@result
A CGRect in the coordinate space of the metadata output whose capture device is providing input to the receiver.

@discussion
AVCaptureMetadataOutput rectOfInterest is expressed as a CGRect where {0,0} represents the top left of the picture area,
and {1,1} represents the bottom right on an unrotated picture.  This convenience method converts a rectangle in
the coordinate space of the receiver to a rectangle of interest in the coordinate space of an AVCaptureMetadataOutput
whose AVCaptureDevice is providing input to the receiver.  The conversion takes orientation, mirroring, and scaling into
consideration.  See -transformedMetadataObjectForMetadataObject:connection: for a full discussion of how orientation and mirroring
are applied to sample buffers passing through the output.
*/

- (CGRect)metadataOutputRectOfInterestForRect:(CGRect)rectInOutputCoordinates NS_AVAILABLE_IOS(7_0);

使用此选项设置rectOfInterest后,它起作用了。

这里的“接收器”代表什么?那么,如何实现仅扫描屏幕中心?