gpt4 book ai didi

ios - iOS 中的条码扫描器

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

我使用 AV Foundation 框架实现条码扫描功能。

session = [[AVCaptureSession alloc] init];
device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;

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()];
[session addOutput:output];

output.metadataObjectTypes = [output availableMetadataObjectTypes];

prevLayer = [AVCaptureVideoPreviewLayer layerWithSession:session];
prevLayer.frame = self.view.bounds;

使用 delegete 方法我将得到条形码结果。即条形码编号

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
CGRect highlightViewRect = CGRectZero;
AVMetadataMachineReadableCodeObject *barCodeObject;
NSString *detectionString = nil;
NSArray *barCodeTypes = @[AVMetadataObjectTypeUPCECode, AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeCode39Mod43Code,
AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode93Code, AVMetadataObjectTypeCode128Code,
AVMetadataObjectTypePDF417Code, AVMetadataObjectTypeQRCode, AVMetadataObjectTypeAztecCode];

for (AVMetadataObject *metadata in metadataObjects) {
for (NSString *type in barCodeTypes) {
if ([metadata.type isEqualToString:type])
{
barCodeObject = (AVMetadataMachineReadableCodeObject *)[_prevLayer transformedMetadataObjectForMetadataObject:(AVMetadataMachineReadableCodeObject *)metadata];
barcodeString = [(AVMetadataMachineReadableCodeObject *)metadata stringValue];
break;
}
}

if (barcodeString != nil)
{
NSLog(@"Barcode String: %@",barcodeString);
}
else
label.text = @"(none)";
}
}

注意:它的工作。但是,主要原因是,我总是无法获得条形码编号。你能帮我解决这个问题吗?

最佳答案

您的委托(delegate)方法看起来不错,但您应该添加:videoGravity 到您的 AVCaptureVideoPreviewLayer (prevLayer) 并启动 session 。尝试在第一个方法的末尾添加这些代码行:

prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.view.layer addSublayer:prevLayer];
[session startRunning];

关于ios - iOS 中的条码扫描器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23219047/

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