使用 Xzing Library 寫一個簡單的 QRcode 掃瞄器 先至 Xzing 下載最新版的 Library https://code.google.com/p/zxing/ 寫本篇文章時 Xcode Version : 4.6.1 Zxing Version : 2.1 先建立一個Single View Application Project 本專案只實作iPhone Device 專案建立完成後將下載下來的 Zxing-2.1 解壓縮後找到裡面的 iphone 跟 cpp 資料夾 複製到剛剛建的專案目錄下 接著將專案目錄下的 iphone/ZXingWidget/ZXingWidget.xcodeproj 拖拉至剛剛建的 Project 專案下面 接著是專案內 Build 的設定 點選 ZXingWidget.xcodeproj 至 TARGETS Build Settings 下的 Other Warning Flags 加入一條 -Wno-unused-private-field (解決錯誤訊息 private field 'cached_y_' is not used ...等的問題) 點選專案至 Targets Build Settings 下的 Header Search Paths 加入 iphone/ZXingWidget/Classes recursive 跟 cpp/core/src non-recursive 至 Targets Build Settings 下的 C++ Standard Library 改為 Compiler Default (解決錯誤訊息 apple mach o linker error ...等的問題) 至 Targets Build Phases 下加入一個 Target Dependencies ZxingWidget (ZxingWidget) 至 Targets Build Phases 下加入六個 Link Binary Libraries libZxingWidget.a AudioToolbox.framework AVFoundation.framework CoreMedia.framework CoreVideo.framework libiconv.dylib 最後將 ViewController.m 檔名改為 ViewController.mm (解決錯誤訊息 iostream file not found ...等的問題) 最後為 ViewController.h 跟 ViewController.mm 內的 Code
ViewController.h
#import <UIKit/UIKit.h> #import "ZXingWidgetController.h" @interface ViewController : UIViewController<zxingdelegate> -(IBAction)scanButton:(id)sender; @end
ViewController.mm
#import "ViewController.h" #ifndef ZXQR #define ZXQR 1 #endif #if ZXQR #import "QRCodeReader.h" #endif #ifndef ZXAZ #define ZXAZ 0 #endif #if ZXAZ #import "AztecReader.h" #endif @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UIButton *scan_button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [scan_button setFrame:CGRectMake(0, 0, 320, 50)]; [scan_button setTitle:@"scan" forState:UIControlStateNormal]; [scan_button addTarget:self action:@selector(scanButton:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:scan_button]; } -(IBAction)scanButton:(id)sender{ ZXingWidgetController *widController = [[ZXingWidgetController alloc] initWithDelegate:self showCancel:YES OneDMode:NO]; NSMutableSet *readers = [[NSMutableSet alloc ] init]; #if ZXQR QRCodeReader* qrcodeReader = [[QRCodeReader alloc] init]; [readers addObject:qrcodeReader]; [qrcodeReader release]; #endif #if ZXAZ AztecReader *aztecReader = [[AztecReader alloc] init]; [readers addObject:aztecReader]; [aztecReader release]; #endif widController.readers = readers; [readers release]; [self presentModalViewController:widController animated:YES]; [widController release]; } #pragma mark - #pragma mark ZXingDelegateMethods - (void)zxingController:(ZXingWidgetController*)controller didScanResult:(NSString *)result { [self dismissModalViewControllerAnimated:NO]; NSLog(@"%@",result); } - (void)zxingControllerDidCancel:(ZXingWidgetController*)controller { [self dismissModalViewControllerAnimated:YES]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
在設備上的結果 按下 Button 後會開啟掃描畫面 掃描 QRcode 後會執行 NSLog(@"%@",result); 沒意外的話 All Output 會將結果顯示出來 END.
沒有留言:
張貼留言