gpt4 book ai didi

ios - 为什么使用新的 iOS 7 API 扫描条形码真的很慢?

转载 作者:可可西里 更新时间:2023-11-01 05:54:33 25 4
gpt4 key购买 nike

我目前正在尝试使用 iOS 7 的最新 API 来扫描 Code 39 条形码,但这让我发疯了。我必须以特定方式握住手机大约 10 秒钟才能检测到它。我将它与 Red Laser、Zbar 等进行了比较,他们可以在 1 秒内分析它,即使它有点倾斜。我不确定是不是因为我加载捕获 session 的方式或其他原因。我很感激你的帮助。关于如何提高性能有什么建议吗?

以下是我在 viewDidLoad 方法中加载扫描仪的方式:

    //Initialize Laser View
laserView = [[UIView alloc] init];
laserView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleBottomMargin;
laserView.layer.borderColor = [UIColor redColor].CGColor;
laserView.layer.borderWidth = 8;
laserView.layer.cornerRadius = 10;
[self.view addSubview:laserView];

//Start Session
scannerSession = [[AVCaptureSession alloc] init];
scannerDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

//Define Error Messages
NSError *error = nil;

//Define Input
scannerInput = [AVCaptureDeviceInput deviceInputWithDevice:scannerDevice error:&error];

//Check if Device has a Camera
if (scannerInput) {
[scannerSession addInput:scannerInput];
} else {
NSLog(@"Error: %@", error);
}

// Locks the configuration
BOOL success = [scannerDevice lockForConfiguration:nil];
if (success) {
if ([scannerDevice isAutoFocusRangeRestrictionSupported]) {

// Restricts the autofocus to near range (new in iOS 7)
[scannerDevice setAutoFocusRangeRestriction:AVCaptureAutoFocusRangeRestrictionNear];
}
}
// unlocks the configuration
[scannerDevice unlockForConfiguration];

//Define Output & Metadata Object Types
scannerOutput = [[AVCaptureMetadataOutput alloc] init];
[scannerOutput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
[scannerSession addOutput:scannerOutput];
scannerOutput.metadataObjectTypes = [scannerOutput availableMetadataObjectTypes];

//Create Video Preview Layer
scannerPreviewLayer = [AVCaptureVideoPreviewLayer layerWithSession:scannerSession];
scannerPreviewLayer.frame = self.view.bounds;
scannerPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.view.layer addSublayer:scannerPreviewLayer];

//Start Session
[scannerSession startRunning];
[self.view bringSubviewToFront:cancelButton];
[self.view bringSubviewToFront:laserView];

和:

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

//Prepare Laser View
CGRect laser = CGRectZero;

//Format Date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"M/d"];

//Format Time
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
[timeFormatter setDateFormat:@"h:ma"];

//Define Barcode Types to Recognize
AVMetadataMachineReadableCodeObject *barCodeObject;
NSString *idNumber = nil;
NSArray *barCodeTypes = @[AVMetadataObjectTypeCode39Code];

if ([metadataObjects count] > 1) {

NSLog(@"%lu Barcodes Found.", (unsigned long)[metadataObjects count]);

}

//Get String Value For Every Barcode (That Matches The Type We're Looking For)
for (AVMetadataObject *metadata in metadataObjects) {


for (NSString *type in barCodeTypes) {


//If The Barcode Is The Type We Need Then Get Data
if ([metadata.type isEqualToString:type]) {

barCodeObject = (AVMetadataMachineReadableCodeObject *)[scannerPreviewLayer transformedMetadataObjectForMetadataObject:(AVMetadataMachineReadableCodeObject *)metadata];
laser = barCodeObject.bounds;
idNumber = [(AVMetadataMachineReadableCodeObject *)metadata stringValue];
break;
}
}

// If IDNumber Found
if (idNumber != nil) {

//Stop Session
[scannerSession stopRunning];
[self vibrate];

NSLog(@"ID: %@", idNumber);

break;
}

//If IDNumber Is Not Found
else {

NSLog(@"No ID Found.");
}
}

//Update Laser
laserView.frame = laser;
}

最佳答案

我在使用 AVCaptureSession 时遇到了类似的问题,捕获速度非常慢,有时需要很长时间才能完成。

不知道我的解决方案是否适合您,但肯定会对像我一样正在寻找这个问题的其他人有所帮助。

AVCaptureSession *captureSession = [AVCaptureSession new];
captureSession.sessionPreset = AVCaptureSessionPresetHigh;

使用此代码,您可以强制相机使用高质量预设。

希望这会对某人有所帮助。

关于ios - 为什么使用新的 iOS 7 API 扫描条形码真的很慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21695931/

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