QR码扫描在iOS应用程序
我需要将QR码阅读器集成到应用程序中,并find了一个教程 。
我从这个链接下载了Z-bar sdk。
这是我所做的。
在QRscannerViewController.m中
-(IBAction)StartScan:(id) sender { ZBarReaderViewController *reader = [ZBarReaderViewController new]; reader.readerDelegate = self; reader.readerView.torchMode = 0; ZBarImageScanner *scanner = reader.scanner; // TODO: (optional) additional reader configuration here // EXAMPLE: disable rarely used I2/5 to improve performance [scanner setSymbology: ZBAR_I25 config: ZBAR_CFG_ENABLE to: 0]; // present and release the controller [self presentModalViewController: reader animated: YES]; [reader release]; resultTextView.hidden=NO; } - (void) readerControllerDidFailToRead: (ZBarReaderController*) reader withRetry: (BOOL) retry{ NSLog(@"the image picker failing to read"); } - (void) imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo: (NSDictionary*) info { NSLog(@"the image picker is calling successfully %@",info); // ADD: get the decode results id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults]; ZBarSymbol *symbol = nil; NSString *hiddenData; for(symbol in results) hiddenData=[NSString stringWithString:symbol.data]; NSLog(@"the symbols is the following %@",symbol.data); // EXAMPLE: just grab the first barcode // break; // EXAMPLE: do something useful with the barcode data //resultText.text = symbol.data; resultTextView.text=symbol.data; NSLog(@"BARCODE= %@",symbol.data); NSUserDefaults *storeData=[NSUserDefaults standardUserDefaults]; [storeData setObject:hiddenData forKey:@"CONSUMERID"]; NSLog(@"SYMBOL : %@",hiddenData); resultTextView.text=hiddenData; [reader dismissModalViewControllerAnimated: NO]; }
所有需要的框架都被添加了,所以没有referenced from
错误。
当我点击扫描button,ZBarReaderViewController显示正常,我按住Alt键并左键单击鼠标打开模拟器的照片库,一切正常。
问题是什么,
- QR图像不被扫描,即
imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo
函数不被调用。 - QR图像显示比原始大小大。
如何解决这个问题?
为什么图像没有被扫描?
在我们的iPhone应用程序中使用ZBar SDK进行BR和QR码扫描。
你可以find一步一步的文章,以及如何处理示例代码
如何在iPhone教程中使用条形码扫描器(BR和QR)(使用ZBar)
看看它怎么运作
-
从这里下载ZBar SDK
-
在你的项目中添加下面的框架
- AVFoundation.framework
- CoreGraphics.framework
- CoreMedia.framework
- CoreAudio.framework
- CoreVideo.framework
- QuartzCore.framework
- libiconv.dylib
-
在框架中添加下载的zip库libzbar.a
-
在你的class级中导入标题并确认它是委托
#import“ZBarSDK.h”
和
@interface ViewController : UIViewController <ZBarReaderDelegate>
5.扫描图像
- (IBAction)startScanning:(id)sender { NSLog(@"Scanning.."); resultTextView.text = @"Scanning.."; ZBarReaderViewController *codeReader = [ZBarReaderViewController new]; codeReader.readerDelegate=self; codeReader.supportedOrientationsMask = ZBarOrientationMaskAll; ZBarImageScanner *scanner = codeReader.scanner; [scanner setSymbology: ZBAR_I25 config: ZBAR_CFG_ENABLE to: 0]; [self presentViewController:codeReader animated:YES completion:nil]; }
6.获得结果
- (void) imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo: (NSDictionary*) info { // get the decode results id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults]; ZBarSymbol *symbol = nil; for(symbol in results) // just grab the first barcode break; // showing the result on textview resultTextView.text = symbol.data; resultImageView.image = [info objectForKey: UIImagePickerControllerOriginalImage]; // dismiss the controller [reader dismissViewControllerAnimated:YES completion:nil]; }
希望这会帮助你,如果你在这个例子中发现任何麻烦,也请让我知道,乐意帮忙
官方文件
与iOS7
的发布一样,您不再需要使用外部框架或库。 具有AVFoundation的iOS生态系统现在完全支持从EAN到UPC的几乎所有的QR码扫描 。
只要看看技术说明和AVFoundation编程指南 。 AVMetadataObjectTypeQRCode
是你的朋友。
这是一个很好的教程 ,它一步一步显示: iPhone QR码扫描库iOS7
只是一个例子,如何设置它:
#pragma mark - #pragma mark AVFoundationScanSetup - (void) setupScanner { self.device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; self.input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil]; self.session = [[AVCaptureSession alloc] init]; self.output = [[AVCaptureMetadataOutput alloc] init]; [self.session addOutput:self.output]; [self.session addInput:self.input]; [self.output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; self.output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode]; self.preview = [AVCaptureVideoPreviewLayer layerWithSession:self.session]; self.preview.videoGravity = AVLayerVideoGravityResizeAspectFill; self.preview.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); AVCaptureConnection *con = self.preview.connection; con.videoOrientation = AVCaptureVideoOrientationLandscapeLeft; [self.view.layer insertSublayer:self.preview atIndex:0]; }
尝试在iOS 7和更新。
要抓取QR码:
- (IBAction)Capture:(id)sender { isFirst=true; _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; _prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; [self.view.layer addSublayer:_prevLayer]; [_session startRunning]; }
要阅读,使用它的委托方法:
- (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]; highlightViewRect = barCodeObject.bounds; detectionString = [(AVMetadataMachineReadableCodeObject *)metadata stringValue]; break; } } if (detectionString != nil) { if (isFirst) { isFirst=false; _label.text = detectionString; break; } } else _label.text = @"(none)"; } _highlightView.frame = highlightViewRect; }
首先从这里 导入ZXingWidget库 。
尝试这个 ,
- (IBAction)btnScanClicked:(id)sender { ZXingWidgetController *widController = [[ZXingWidgetController alloc] initWithDelegate:self showCancel:YES OneDMode:NO]; QRCodeReader* qrcodeReader = [[QRCodeReader alloc] init]; NSSet *readers = [[NSSet alloc ] initWithObjects:qrcodeReader,nil]; [qrcodeReader release]; widController.readers = readers; [readers release]; NSBundle *mainBundle = [NSBundle mainBundle]; widController.soundToPlay = [NSURL fileURLWithPath:[mainBundle pathForResource:@"beep-beep" ofType:@"aiff"] isDirectory:NO]; [self presentModalViewController:widController animated:YES]; [widController release]; }
和代表
- (void)zxingController:(ZXingWidgetController*)controller didScanResult:(NSString *)result { }
您可以使用我自己的QRCodeReader框架。
https://www.cocoacontrols.com/controls/qrcodereader
如何使用
- embedded式二进制文件
- 在您的视图控制器中拖放UIView。
- 改变UIVIEW的类别。
- 绑定你的UIView。
在视图控制器中粘贴“M1,M2”方法(即“ViewController.m”)
“M1”viewDidLoad
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. self.title = @"QR Code Reader"; [qrCodeView setDelegate:self]; [qrCodeView startReading]; }
在这里委托方法: “M2”QRCodeReaderDelegate
#pragma mark - QRCodeReaderDelegate - (void)getQRCodeData:(id)qRCodeData { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"QR Code" message:qRCodeData preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Close" style:UIAlertActionStyleDefault handler:nil]; [alertController addAction:cancel]; UIAlertAction *reScan = [UIAlertAction actionWithTitle:@"Rescan" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [qrCodeView startReading]; }]; [alertController addAction:reScan]; [self presentViewController:alertController animated:YES completion:nil]; }
谢谢。
- cocoa:命令/ usr / bin / ditto失败,退出代码为1
- Swift iOS -UIImagePicker的照片库在模拟器上呈现,但崩溃(不会出现)在运行Xcode的实际设备上
- 如何使用UISearchBar过滤类型]的数组并显示为String
- Xcode:“这个应用程序目前无法安装。”
- 由于Google SignIn引起的iOS拒绝。 最新的Google SignIn(4.0.0)去safari
- Swift的第七周-UI视图控制器–林信华–中
- 导航控制器循环
- 在使用UIImagePickerControllerOriginalImage从UIImagePickerController获取图像时获取内存警告
- 在Pinterest上开发快速可靠的iOS版本(第一部分)