ios ios应用程序中的二维码扫描
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16166646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
QR Code Scanning in ios application
提问by Azik Abdullah
I need to integrate QR-code reader in app and found a tutorialfor it.
我需要在应用程序中集成二维码阅读器并找到了一个教程。
I downloaded Z-bar sdk from this link.
我从这个链接下载了 Z-bar sdk 。
Here is what I had done.
这是我所做的。
In the QRscannerViewController.m
在 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];
}
All needed frameworks were added, so there is no referenced from
errors.
添加了所有需要的框架,因此没有referenced from
错误。
When I click the scan button, the ZBarReaderViewController appears well and I hold the alt key and left click the mouse to open the photo library of simulator and all works fine.
当我单击扫描按钮时,ZBarReaderViewController 出现得很好,我按住 alt 键并单击鼠标左键打开模拟器的照片库,一切正常。
What the problem is,
问题是什么,
- The QR image is not get scanned, ie
imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo
function is not get called. - The QR image appears larger than its original size.
- 不会扫描 QR 图像,即
imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo
不会调用函数。 - QR 图像看起来比其原始尺寸大。
How to solve this?
如何解决这个问题?
Why the image not get scanned?
为什么图像没有被扫描?
回答by Alexander
As with the release of iOS7
you no longer need to use an external framework or library. The iOS ecosystem with AVFoundation now fully supports scanningalmost every code from QR over EAN to UPC.
随着发布iOS7
你不再需要使用外部框架或库。带有 AVFoundation 的 iOS 生态系统现在完全支持扫描几乎所有代码,从 EAN 上的 QR 到 UPC。
Just have a look at the Tech Noteand the AVFoundation programming guide. AVMetadataObjectTypeQRCode
is your friend.
只需查看技术说明和AVFoundation 编程指南。AVMetadataObjectTypeQRCode
是你的朋友。
Here is a nice tutorialwhich shows it step by step: iPhone QR code scan library iOS7
这是一个很好的教程,一步一步地展示它: iPhone QR code scan library iOS7
Just a little example on how to set it up:
只是一个关于如何设置它的小例子:
#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];
}
回答by swiftBoy
use ZBar SDK for BR and QR code scanning in our iPhone application.
在我们的 iPhone 应用程序中使用 ZBar SDK 进行 BR 和 QR 码扫描。
you can find step by step article for this, how to do with sample code as well
您可以找到有关此的分步文章,以及如何处理示例代码
How to use Barcode Scanner (BR and QR) in iPhone Tutorial (using ZBar)
see how it works
看看它怎么运作
download ZBar SDK from here
add below frameworks in your project
- AVFoundation.framework
- CoreGraphics.framework
- CoreMedia.framework
- CoreAudio.framework
- CoreVideo.framework
- QuartzCore.framework
- libiconv.dylib
Add the library downloaded libzbar.aof zip in the frameworks
import header in your class and confirm it's delegate
#import "ZBarSDK.h"
从这里下载 ZBar SDK
在您的项目中添加以下框架
- AVFoundation.framework
- 核心图形框架
- 核心媒体框架
- 核心音频框架
- 核心视频框架
- QuartzCore.framework
- libiconv.dylib
在frameworks中添加库下载的zip的libzbar.a
在您的班级中导入标题并确认它是委托
#import "ZBarSDK.h"
and
和
@interface ViewController : UIViewController <ZBarReaderDelegate>
5.scan image
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.get the result in
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];
}
Hope this will help you, also let me know if you find any trouble in this example, Happy to help
希望这会对您有所帮助,如果您在此示例中发现任何问题,也请告诉我,很高兴为您提供帮助
回答by SM18
Try this on iOS 7 and newer.
在 iOS 7 和更新版本上试试这个。
To capture QR code:
捕捉二维码:
- (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];
}
To read, use its delegate method:
要阅读,请使用其委托方法:
- (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;
}
回答by pratik bhiyani
FIrst import ZXingWidget libraryfrom here.
首先从这里导入 ZXingWidget 库。
Try this ,
尝试这个 ,
- (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];
}
and Delegate
和委托
- (void)zxingController:(ZXingWidgetController*)controller didScanResult:(NSString *)result {
}
回答by Vivek
You can use my own framework for QRCodeReader.
您可以使用我自己的QRCodeReader框架。
https://www.cocoacontrols.com/controls/qrcodereader
https://www.cocoacontrols.com/controls/qrcodereader
How to use
如何使用
- Embeded Binaries
- Drag and drop UIView in your view controller.
- Change Class of UIVIew.
- Bind your UIView.
- 嵌入式二进制文件
- 将 UIView 拖放到您的视图控制器中。
- 更改 UIView 的类。
- 绑定你的 UIView。
Paste "M1, M2" methods in your view controller (i.e. "ViewController.m")
在您的视图控制器中粘贴“M1, M2”方法(即“ViewController.m”)
"M1" viewDidLoad
“M1”视图DidLoad
- (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];
}
And here the delegate methods: "M2" QRCodeReaderDelegate
这里是委托方法: “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];
}
Thanks.
谢谢。