所以我需要为 iphone 应用程序编写一个二维码阅读器。通常,ZXING 和 ZBAr 等框架允许您下载应用程序,然后读取条形码。我想将它集成到应用程序本身中。所以基本上我希望能够点击一个按钮并让它读取二维码。这可能吗?如果是这样,是否有任何适当的文件?我尝试使用 ZXing,但是当我链接二进制文件时,libZXingWidget.a 不可用。此外,文档不足以知道如何将其集成到应用程序中。所以让我知道。
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;
}
}
这是一个关于如何将集成的二维码阅读器添加到您的 iPhone 应用程序的分步教程。如果您需要了解如何使用它,请查看此示例 ZXing 项目。
我已经设法将 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
正确设置 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");
}
最困难的部分只是设置它。我发现这个教程是最有帮助的(对不起,我使用了一个链接):如何在 Xcode 4 中安装 ZXing 如果您有任何问题,这些评论也很有帮助。