gpt4 book ai didi

ios7 - 使 AVCaptureSession 只扫描一次

转载 作者:行者123 更新时间:2023-12-04 16:49:59 24 4
gpt4 key购买 nike

如何使 AVCaptureSession 仅扫描 AVCaptureMetadataOutput 一次。我在扫描一个条形码超过 30 次时遇到问题,扫描声音延迟了大约 2-3 秒,然后它发出疯狂的哔哔声(字面意思)并显示 ~30 UIAlertViews!!

下面的代码是我尝试只扫描一次...

    - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{

CGRect highlightViewRect = CGRectZero;
AVMetadataMachineReadableCodeObject *barCodeObject;
NSString *detectionString = nil;
NSArray *barCodeTypes = @[AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeCode39Mod43Code];

for (AVMetadataObject *metadata in metadataObjects) {
for (NSString *type in barCodeTypes) {
if ([metadata.type isEqualToString:type])
{
barCodeObject = (AVMetadataMachineReadableCodeObject *)[_prevLayer transformedMetadataObjectForMetadataObject:(AVMetadataMachineReadableCodeObject *)metadata];
highlightViewRect = barCodeObject.bounds;
detectionString = [(AVMetadataMachineReadableCodeObject *)metadata stringValue];
break;
}
}
if (detectionString != nil)
{
[_session removeOutput:_output];
[_session stopRunning];
_session = nil;
_output = nil;
[_prevLayer removeFromSuperlayer];

NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/barcodeBeep.wav", [[NSBundle mainBundle] resourcePath]]];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[audioPlayer play];

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
}

感谢任何帮助。

最佳答案

iOS 中的条形码扫描仪是作为 AV 管道的一部分实现的。扫描仪将查看每个捕获的图像,如果它识别出图像中的条形码,则调用代表。因此,如果它在 30 个连续图像中识别出条形码,它将连续调用委托(delegate) 30 次。

如何处理这种情况取决于您的应用。某些应用程序可能希望不断收到有关已识别条形码的信息。您显然只对单个识别事件感兴趣。为此,您有多种选择:

  1. 完全停止视频捕获。如果您的应用在捕获条形码后切换到不同的场景,这通常是合适的。

  2. 从 AV 管道中删除条形码扫描仪 (AVCaptureMetadataOutput)。

  3. 记住上次捕获的条形码和上次捕获的时间,如果在上次捕获后的 2 到 3 秒内再次捕获相同的条形码,则不要采取任何行动。

您发布的代码不完整。可能是您已经实现了类似于选项 2(和/或 1)的东西。这些选项可能还不够,因为 AV 管道可能积压了几帧。停止捕获后,它会继续处理已捕获但未处理条形码的帧。

我预计大约有五个帧正在处理中。如果您真的遇到多达 30 帧,则表明您的主线程太忙,无法跟上捕获过程。

因此最好的方法可能是实现选项 3(除了您已经拥有的)并确保您的主线程不会太忙。

if (detectionString != nil)
{
if ([detectionString isEqualToString:_lastCapturedBarcode]
&& [_lastCaptureTime timeIntervalSinceNow] < -3.0)
return; // do nothing; the barcode was already captured

_lastCapturedBarcode = detectionString;
_lastCapturedBarcode = [NSDate date];

[_session removeOutput:_output];
[_session stopRunning];
_session = nil;
_output = nil;
[_prevLayer removeFromSuperlayer];

关于ios7 - 使 AVCaptureSession 只扫描一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21361747/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com