ios - AVCaptureMetadataOutput, rectOfInterest 扫描外部
<p><p>所以我正在尝试使用 AVCaptureMetadataOutput 来扫描二维码。我遇到的问题是,即使我使用 rectOfInterest,扫描也可能发生在预览区域之外,请参见下图:</p>
<p> <img src="https://i.imgur.com/PZTFMZI.png" alt="Scans outside"/> </p>
<p>代码如下:</p>
<pre><code>- (void)capture
{
session = [ init];
device = ;
if ( == YES ) {
CGPoint point = CGPointMake(0.5,0.5);
;
;
;
}
NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device
error:&error];
if (!input)
{
NSLog(@"Error: %@", error);
return;
}
;
//Add the metadata output device
output = [ init];
;
;
output.metadataObjectTypes = @;
output.rectOfInterest = self.livevideo.bounds;
newCaptureVideoPreviewLayer = [ initWithSession:session];
newCaptureVideoPreviewLayer.frame = self.livevideo.bounds;
newCaptureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
;
highlightView = [ init];
highlightView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleBottomMargin;
highlightView.layer.borderColor = .CGColor;
highlightView.layer.borderWidth = 3;
;
;
}
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputMetadataObjects:(NSArray *)metadataObjects
fromConnection:(AVCaptureConnection *)connection
{
AVMetadataMachineReadableCodeObject *barCodeObject;
CGRect highlightViewRect = CGRectZero;
for (AVMetadataObject *metadata in metadataObjects)
{
for (NSString *type in output.metadataObjectTypes) {
if ()
{
barCodeObject = (AVMetadataMachineReadableCodeObject *);
highlightViewRect = barCodeObject.bounds;
@try {
NSString *code =;
NSLog(@"Readtype: %@", type);
self.barcode.text = code;
}
@catch (NSException *exception) {
NSLog(@"%@", exception.reason);
}
break;
}
}
}
highlightView.frame = highlightViewRect;
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>那是<a href="https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVCaptureMetadataOutput/Reference/Reference.html#//apple_ref/occ/instp/AVCaptureMetadataOutput/rectOfInterest" rel="noreferrer noopener nofollow">documented behavior</a> :</p>
<blockquote>
<p>Metadata objects whose bounds do not intersect with the rectOfInterest will not be returned.</p>
</blockquote>
<p>所以,如果二维码与矩形完全相交,就会被检测到。</p></p>
<p style="font-size: 20px;">关于ios - AVCaptureMetadataOutput, rectOfInterest 扫描外部,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/20195186/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/20195186/
</a>
</p>
页:
[1]