我使用AV Foundation框架進行條碼掃描功能。iOS條碼掃描器
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)";
}
}
注:其工作。但是,主要原因,我沒有得到條碼編號。請你幫我解決這個問題。
什麼是你面對的確切問題?你無法看到結果或沒有得到結果? –
供參考:這是一個關於QR碼掃描的好教程:http://www.ama-dev.com/iphone-qr-code-library-ios-7/對於通過任何類型的代碼掃描,這幾乎是相同的程序AVFoundation – Alexander