Iphone ios应用中的二维码扫描
我需要在应用程序中集成二维码阅读器,并为其找到一个解决方案 我从这里下载了Z-bar sdk 这就是我所做的 在QRscannerViewController.m中Iphone ios应用中的二维码扫描,iphone,ios,objective-c,qr-code,Iphone,Ios,Objective C,Qr Code,我需要在应用程序中集成二维码阅读器,并为其找到一个解决方案 我从这里下载了Z-bar sdk 这就是我所做的 在QRscannerViewController.m中 -(IBAction)StartScan:(id) sender { ZBarReaderViewController *reader = [ZBarReaderViewController new]; reader.readerDelegate = self; reader.readerView.to
-(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];
}
-(iAction)开始扫描:(id)发送方
{
ZBarReaderViewController*读卡器=[ZBarReaderViewController新建];
reader.readerDelegate=self;
reader.readerView.torchMode=0;
ZBarImageScanner*scanner=reader.scanner;
//TODO:(可选)此处的附加读卡器配置
//示例:禁用很少使用的I2/5以提高性能
[扫描仪设置符号:ZBAR_I25]
配置:ZBAR_CFG_启用
至:0];
//显示并释放控制器
[自我呈现模式控制器:读卡器
动画:是];
[读者发布];
resultTextView.hidden=否;
}
-(无效)读卡器控制器IDFailToRead:(ZBarReaderController*)读卡器
withRetry:(BOOL)重试{
NSLog(@“图像选择器无法读取”);
}
-(void)imagePickerController:(UIImagePickerController*)读卡器已使用信息完成PickingMediaWithInfo:(NSDictionary*)信息
{
NSLog(@“图像选择器正在成功调用%@”,info);
//添加:获取解码结果
id结果=[info objectForKey:ZBarReaderControllerResults];
ZBarSymbol*符号=nil;
NSString*隐藏数据;
用于(结果中的符号)
hiddenData=[NSString stringWithString:symbol.data];
NSLog(@“符号如下%@”,symbol.data);
//示例:只需抓取第一个条形码
//中断;
//示例:对条形码数据执行一些有用的操作
//resultText.text=symbol.data;
resultTextView.text=symbol.data;
NSLog(@“条形码=%@”,符号.数据);
NSUserDefaults*storeData=[NSUserDefaults standardUserDefaults];
[storeData setObject:hiddenData forKey:@“CONSUMERID”];
NSLog(@“符号:%@”,隐藏数据);
resultTextView.text=hiddenData;
[reader dismissModalViewControllerAnimated:否];
}
添加了所有需要的框架,因此没有从错误中引用的
当我点击扫描按钮时,ZBarReaderViewController显示良好,我按住alt键并左键单击鼠标打开模拟器的照片库,所有工作正常
问题是,
未扫描QR图像,即imagePickerController:
(UIImagePickerController*)读卡器使用信息完成了PickingMediaWithInfo
函数未被调用
QR图像看起来比其原始大小大
如何解决这个问题
为什么不扫描图像?首先从导入Widget库
试试这个
- (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 {
}
在我们的iPhone应用程序中使用ZBar SDK进行BR和QR码扫描
您可以找到关于此的逐步文章,以及如何使用示例代码
看看它是如何工作的
从下载ZBar SDK
在项目中添加以下框架
- AVFoundation.framework
- CoreGraphics.framework
- CoreMedia.framework
- CoreAudio.framework
- CoreVideo.framework
- QuartzCore.framework
- libiconv.dylib
在框架中添加下载的zip库libzbar.a
在类中导入标题并确认其为委托
导入“ZBarSDK.h”
及
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];
}
-(void)imagePickerController:(UIImagePickerController*)读卡器已使用信息完成PickingMediaWithInfo:(NSDictionary*)信息
{
//获取解码结果
id结果=[info objectForKey:ZBarReaderControllerResults];
ZBarSymbol*符号=nil;
用于(结果中的符号)
//只要抓取第一个条形码
打破
//在textview上显示结果
resultTextView.text=symbol.data;
resultImageView.image=[info-objectForKey:UIImagePickerControllerOriginalImage];
//解雇控制员
[reader DismissViewControllerInitiated:YES completion:nil];
}
希望这将帮助你,也让我知道,如果你在这个例子中发现任何麻烦,很乐意帮助
与发布的iOS7
一样,您不再需要使用外部框架或库iOS生态系统与AVFoundation现在完全支持扫描从EAN上的QR到UPC的几乎所有代码
只需看一下和。他是你的朋友
下面是一个很好的教程,它一步一步地展示:
只是一个关于如何设置的小示例:
#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及更高版本上尝试此功能
要捕获二维码:
- (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;
}
您可以将我自己的框架用于QRCodeReader
如何使用
嵌入二进制文件
在视图控制器中拖放UIView
更改UIVIew的类
绑定您的UIView
在视图控制器(即“ViewController.m”)中粘贴“M1,M2”方法
“M1”视图加载
- (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];
}
谢谢。qr应完全可见,便于阅读。由于二维码图像较大,图书馆无法扫描。。图像中的所有四个角都应该在那里。@SharonNathaniel,如何缩小这个尺寸?我建议在一个指向纸张或屏幕上二维码的设备上试试。由于您是从模拟器中的照片库中获取图像,我不确定您能否在将其提供给读者之前对其进行操作。我建议在设备上试用。zxing,必须使用哪个sdk?在哪里下载?+1,很好的教程,它能在模拟器上工作还是我必须在上面测试device@NAZIK我的荣幸,亲爱的,顺便说一句,我只在苹果设备上测试过它,它工作得很好,但对模拟器不确定,如果你有任何问题,你可以给我留言,我可以解释我知道的一切。@NAZIK对k真的很好
- (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];
}
#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];
}