所以我需要为iphone应用程序编写QR阅读器。通常,像ZXING和ZBAr这样的框架允许您下载应用程序然后读取条形码。我想将它集成到应用程序本身中。所以基本上我希望能够点击按钮并让它读取QR码。这可能吗?如果是的话,有没有适当的文件?我尝试使用ZXing,但是当我链接二进制文件时,libZXingWidget.a不可用。此外,文档还不足以知道如何将其集成到应用程序中。所以让我知道。
答案 0 :(得分:4)
以下是设置ZBar的代码,请务必将SDK添加到项目中并链接您的库。
-
(void)viewDidLoad
{
[ZBarReaderView class];
readerView.readerDelegate = self;
readerView.tracksSymbols = NO;
//CHOOSE CAMERA
if (some setting isEqual to CameraRear) {
readerView.device = [self backFacingCameraIfAvailable];
}
else {
readerView.device = [self frontFacingCameraIfAvailable];
}
[self relocateReaderPopover:[self interfaceOrientation]];
[readerView start];
}
-(AVCaptureDevice *)frontFacingCameraIfAvailable
{
NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
AVCaptureDevice *captureDevice = nil;
for (AVCaptureDevice *device in videoDevices)
{
if (device.position == AVCaptureDevicePositionFront)
{
captureDevice = device;
break;
}
}
// couldn't find one on the front, so just get the default video device.
if ( ! captureDevice)
{
captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
}
return captureDevice;
}
-(AVCaptureDevice *)backFacingCameraIfAvailable
{
NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
AVCaptureDevice *captureDevice = nil;
for (AVCaptureDevice *device in videoDevices)
{
if (device.position == AVCaptureDevicePositionBack)
{
captureDevice = device;
break;
}
}
// couldn't find one on the front, so just get the default video device.
if ( ! captureDevice)
{
captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
}
return captureDevice;
}
-(void)relocateReaderPopover:(UIInterfaceOrientation)toInterfaceOrientation{
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
readerView.previewTransform = CGAffineTransformMakeRotation(M_PI_2);
} else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
readerView.previewTransform = CGAffineTransformMakeRotation(-M_PI_2);
} else if (toInterfaceOrientation== UIInterfaceOrientationPortraitUpsideDown) {
readerView.previewTransform = CGAffineTransformMakeRotation(M_PI);
} else {
readerView.previewTransform = CGAffineTransformIdentity;
}
}
答案 1 :(得分:3)
This is a step-by-step tutorial关于如何将集成的QR码阅读器添加到您的iPhone应用程序。如果您需要知道如何使用它,请查看此example ZXing project。
答案 2 :(得分:2)
我设法将ZXING集成到我的测试项目中。我不久前做过这个,所以我可能不记得我遇到和解决的所有问题。 在我的工作区文件夹中,我复制了ZXing-2.0文件夹。在里面我只留下了文件夹:cpp,docs和iphone。到我的工作区(包含测试项目)我已经从文件夹ZXing-2.0添加了ZXingWidget项目。这允许我添加到链接设置libZXingWidget.a。 当我构建我的测试项目时,XCode会检测依赖项并首先构建窗口小部件,然后构建测试项目并将其链接到libZXingWidget.a。
这是一个简单的视图控制器,我已经实现了在摄像机视图中显示能够检测QR码。
//
// MyVC.m
//
//
#import "MyVC.h"
#import "QRCodeReader.h"
@interface MyVC () {
ZXingWidgetController *_widController;
}
@end
@implementation MyVC
@synthesize labelResultString;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
_widController = [[ZXingWidgetController alloc] initWithDelegate:(id<ZXingDelegate>)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];
_widController.overlayView.displayedMessage = @"";
_widController.wantsFullScreenLayout = NO;
//[self presentModalViewController:_widController animated:NO];
_widController.view.frame = CGRectMake(10, 10, 300, 300);//self.view.frame;
_widController.view.autoresizingMask = UIViewAutoresizingNone;
_widController.overlayView.frame = CGRectMake(0, 0, 300, 300);
_widController.overlayView.cropRect = CGRectMake(20, 20, 260, 260);
[self.view addSubview:_widController.view];
//[_widController release];
}
- (void)viewDidUnload {
[super viewDidUnload];
self.labelResultString = nil;
}
- (void)dealloc {
self.labelResultString = nil;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[_widController viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[_widController viewDidAppear:animated];
[[UIApplication sharedApplication] setStatusBarHidden:NO];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[_widController viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[_widController viewDidDisappear:animated];
}
#pragma mark - ZXingDelegateMethods
- (void)zxingController:(ZXingWidgetController*)controller didScanResult:(NSString *)result {
self.labelResultString.text = result;
}
- (void)zxingControllerDidCancel:(ZXingWidgetController *)controller {
[self dismissModalViewControllerAnimated:YES];
}
@end
答案 3 :(得分:2)
一旦你正确设置ZXing,获得QR阅读器的代码就这么简单了:
确保将这两个人导入QR:
#import "ZXingWidgetController.h"
#import "QRCodeReader.h"
然后在您的控制器中,您将设置代码阅读器,如下所示:
ZXingWidgetController *widController = [[ZXingWidgetController alloc] initWithDelegate:self showCancel:YES OneDMode:NO];
QRCodeReader *qrcodeReader = [[QRCodeReader alloc] init];
NSSet *readers = [[NSSet alloc] initWithObjects:qrcodeReader,nil];
widController.readers = readers;
[self presentModalViewController:widController animated:YES];
你基本上设置了处理代码读取的控制器(ZXingWidgetController
),然后给它一组你想要的所有类型的代码阅读器(这里我只使用QRCodeReader
。)最后,你只是把它作为一个模态视图控制器。
然后,您需要实施<ZXingDelegate>
和以下delegate
函数:
- (void)zxingController:(ZXingWidgetController*)controller didScanResult:(NSString *)result
{
[self dismissModalViewControllerAnimated:YES];
NSLog(@"%@",result); //Simple NSString result.
}
- (void)zxingControllerDidCancel:(ZXingWidgetController*)controller
{
[self dismissModalViewControllerAnimated:YES];
NSLog(@"Cancelled");
}
最难的部分就是设置它。我发现本教程最有帮助(很抱歉,我使用的是链接):How to install ZXing in Xcode 4如果您有任何问题,评论也会有所帮助。