1.å¦ä½å¨iOSä¸ä½¿ç¨ZXingåº
2.å¦ä½å¨ iOS å·¥ç¨ä¸ä½¿ç¨ OpenCV
3.å¨iosä¸å¦ä½å©ç¨ opencv è¿è¡å¾åå¤ç
å¦ä½å¨iOSä¸ä½¿ç¨ZXingåº
ZXing(Githubéåå°å)æ¯ä¸ä¸ªå¼æºçæ¡ç çæåæ«æåºï¼å¼æºå议为Apache2.0)ãå®ä¸ä½æ¯æä¼å¤çæ¡ç æ ¼å¼ï¼èä¸æåç§è¯è¨çå®ç°çæ¬ï¼å®æ¯æçè¯è¨å æ¬ï¼Javaã C++ã C#ã Objective-CãActionScript以åRubyã
æ以åå¨iOS项ç®å¼åä¸ä½¿ç¨äºZXingçæ«æäºç»´ç åè½ãå¨æ¤æ»ç»ä¸ä¸å¦ä½å°ZXingéæå°å·²æçiOSå·¥ç¨ä¸ï¼å享ç»å¤§å®¶ã
éææ¥éª¤
é¦å å»Google CodeæGithubå°ZXingç代ç ä¸è½½ä¸æ¥ï¼æ´ä¸ªå·¥ç¨æ¯è¾å¤§ï¼æ们åªéè¦å ¶ä¸æ¶åiOSçé¨åï¼æ以æ好åä¸äºè£åªãç®åæ¥è¯´ï¼æ们åªéè¦ä¿çcppåiphoneè¿2个æ件夹ï¼å ¶ä½çå ¨é¨å æã
æ¥çæ们继ç»è£åªï¼å¯¹äºcppè¿ä¸ªç®å½ï¼åªä¿çcpp/core/src/zxingä¸é¢çå 容ï¼å ¶ä½å 容ä¹å¯ä»¥å æäºãä½æ¯æ´ä¸ªç®å½ç»æå¿ é¡»ä¿æåæ ·ãè£åªå®åï¼æ´ä¸ªç®å½ç»æå¦ä¸æ示ï¼
æ¥ä¸æ¥ï¼æ们æè£åªåçzxingç®å½æ´ä¸ªç§»å¨å°æ们çiOS项ç®çç®å½ä¸ï¼å¹¶ä¸æä¸å¾ä¸å¯ä»¥çå°çZXingWidget.xcodeprojæ件æå¨å°æ们çiOSå·¥ç¨ä¸ã
ä¸ä¸æ¥ï¼æ们éè¦è®¾ç½®ZXing项ç®åæ们åæ¬çiOS项ç®ä¹é´çä¾èµå ³ç³»ãå¨æ们çiOS项ç®ç设置ä¸ï¼ç¹å»build phases tabï¼ç¶åå¢å Target Dependencies å Link binaryï¼å¹¶ä¸å¢å è¿äºframeworkä¾èµï¼
a. AVFoundation
b. AudioToolbox
c. CoreVideo
d. CoreMedia
e. libiconv
f. AddressBook
g. AddressBookUI
å®æä¹åå¦ä¸å¾æ示ï¼
æåä¸æ¥ï¼å¨è®¾ç½®ä¸å¢å å¦ä¸2个header search path:
./zxing/iphone/ZXingWidget/Classes
./zxing/cpp/core/src
éè¦æ³¨æçæ¯ï¼ç¬¬ä¸ä¸ªpathè¦è®¾ç½®æ循ç¯æ¥æ¾åç®å½ï¼è第äºä¸ªä¸å¾ªç¯æ¥æ¾ï¼å¦ä¸å¾æ示ï¼
æåä½ ï¼å®æè¿æ¥ä¹åï¼ä½ 就已ç»å®æZXingåºçéæäºãä¸é¢è°è°å¦ä½ä½¿ç¨ZXingåºæ¥åäºç»´ç è¯å«ã
äºç»´ç è¯å«
ZXingçiOSçæ¬æä¾2ç§æ¹æ³æ¥åäºç»´ç è¯å«åè½ï¼ç¬¬ä¸ç§æ¹æ³æ¯è¾ç®åï¼ç¬¬äºç§æ¹æ³æ¯è¾å¤æãæå¨åDemoæ¶ä½¿ç¨äºç¬¬ä¸ç§æ¹æ³ï¼åçæ£é¡¹ç®å¼åçæ¶å使ç¨äºç¬¬äºç§æ¹æ³ï¼æ以é½ç»å¤§å®¶ä»ç»ä¸ä¸ã
使ç¨æ¹æ³ä¸
ZXingç´æ¥æä¾äºä¸ä¸ªæ«æäºç»´ç çView Controllerï¼å³ZXingWidgetControllerãå¨éè¦ä½¿ç¨ççé¢ä»£ç ä¸ï¼å å ¥æ件ä¾èµï¼
1 #import <ZXingWidgetController.h>
2 #import <QRCodeReader.h>
ç¶åå¨éè¦æ«æçæ¶åï¼è°ç¨å¦ä¸ä»£ç å³å¯ï¼
1 - (IBAction)scanPressed:(id)sender {
2 ZXingWidgetController *widController = [[ZXingWidgetController alloc] initWithDelegate:self showCancel:YES OneDMode:NO];
3 NSMutableSet *readers = [[NSMutableSet alloc ] init];
4 QRCodeReader* qrcodeReader = [[QRCodeReader alloc] init];
5 [readers addObject:qrcodeReader];
6 [qrcodeReader release];
7 widController.readers = readers;
8 [readers release];
9 [self presentModalViewController:widController animated:YES];
[widController release];
}
å¨ZXingæ«ææç»ææ¶ï¼ä¼è°ç¨å¦ä¸åè°å½æ°ï¼
1 @protocol ZXingDelegate
2 - (void)zxingController:(ZXingWidgetController*)controller didScanResult:(NSString *)result;
3 - (void)zxingControllerDidCancel:(ZXingWidgetController*)controller;
4 @end
使ç¨æ¹æ³äº
æ¹æ³äºä¸æ¹æ³ä¸çåºå«å°±ç¸å½äºAVFoundationåUIImagePickerControllerçåºå«ä¸æ ·ãç®åæ¥è¯´ï¼å°±æ¯ä½¿ç¨æ¹æ³äºæ¯æ¹æ³ä¸æ´å 麻ç¦ï¼ä½æ¯è·å¾çå¯å®å¶æ§æ´é«ã
使ç¨æ¹æ³äºæ¶ï¼ä½ éè¦èªå·±ç¨AVFoundationè·å¾Cameraè¿åçå®æ¶å¾è±¡ï¼ç¶å转æUIImageï¼æåä¼ ç»ZXingçDecoderç±»å®æäºç»´ç çè¯å«ãç±äºä½¿ç¨AVFoundationæ¶åç代ç ç¥å¤ï¼æåç示æ代ç å¦ä¸ï¼
1 #import "Decoder.h"
2 #import "TwoDDecoderResult.h"
3 #import "QRCodeReader.h"
4
5 - (void)viewDidLoad {
6 // setup QR reader
7 self.qrReader = [[NSMutableSet alloc ] init];
8 QRCodeReader* qrcodeReader = [[QRCodeReader alloc] init];
9 [self.qrReader addObject:qrcodeReader];
self.scanningQR = NO;
self.step = STEP_QR;
}
// AVFoundationçåè°å½æ°
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
// 第ä¸æ¥ï¼å°sampleBuffer转æUIImage
UIImage *image= [self getCaptureImage:sampleBuffer];
// 第äºæ¥ï¼ç¨Decoderè¯å«å¾è±¡
Decoder *d = [[Decoder alloc] init];
d.readers = self.qrReader;
d.delegate = self;
self.scanningQR = [d decodeImage:image] == YES ?bonecp 源码 NO : YES;
}
ZXingçDecoderç±»æä¾äºä»¥ä¸åè°å½æ°è·å¾è¯å«ç»æï¼
1 @protocol DecoderDelegate<NSObject>
2 @optional
3 - (void)decoder:(Decoder *)decoder willDecodeImage:(UIImage *)image usingSubset:(UIImage *)subset;
4 - (void)decoder:(Decoder *)decoder didDecodeImage:(UIImage *)image usingSubset:(UIImage *)subset withResult:(TwoDDecoderResult *)result {
5 NSLog(@"result = %@", [result text]);
6 }
7 - (void)decoder:(Decoder *)decoder failedToDecodeImage:(UIImage *)image usingSubset:(UIImage *)subset reason:(NSString *)reason;
8 - (void)decoder:(Decoder *)decoder foundPossibleResultPoint:(CGPoint)point;
9
@end
Trouble Shoot & Tips
æå¨ä½¿ç¨ä¸éå°äºä¸äºé®é¢ï¼ä¸»è¦æ¯ç¼è¯çé®é¢ã
ä¸ä¸ªæ¯æ¾ä¸å° 头æ件ã解å³æ¹æ³ï¼æç¨å°ZXingçæºæ件æ©å±åç±.mæ¹æ.mmã
æ¥éï¼Undefined symbols for architecture armv7sï¼è§£å³æ¹æ³ï¼æZXingWidgetçä¸ä¸ªbuild targetåæ°ï¼âBuild Active Architecture Onlyâ ä¿®æ¹æ âNOâ.
æ¥éï¼No such file or directoryï¼åºç°è¯¥é误å¯è½æ¯ä½ çHeader Search Pathåéäºï¼æè å°±æ¯ä½ çzxingåºçç®å½ç»æä¸æ¯æä¸é¢å¼ºè°çï¼å¥½å¥½æ£æ¥ä¸ä¸å§ã
å¦æä½ éè¦çæäºç»´ç åæµè¯ï¼æ¨èä¸ä¸ªä¸éçå¨çº¿çæäºç»´ç çç½ç«ï¼http://cli.im/
ZXingåOpenCVçå ¼å®¹é®é¢
ZXing 2.1 åOpenCV 2.4.3çiOSåºæä¸äºå ¼å®¹é®é¢ï¼ä»ä»¬å¯¹C++æ ååºççæ¬åç¼è¯å¨çæ¬é½æä¸äºéæ±ï¼é æ满足ä¸æ¹äºï¼å¦ä¸æ¹å°±ç¼è¯ä¸éè¿äºãStackoverflowä¸æ人ç»äºæ¾å°äºä¸ä¸ªè®©å®ä»¬åå¹³å ±å¤çæ¹æ³ï¼ä½æ¯åªéç¨äºiOS5.0以ä¸çæ¬ãæ£å¥½æ们çAppåªæ¯æiOS5.0+ï¼æ以就æå®äºã
å¦ä½å¨ iOS å·¥ç¨ä¸ä½¿ç¨ OpenCV
é¦å 说æï¼ä¸ºä½å¨ PCH æ件ä¸å¼å ¥ OpenCV ç头æ件æ们éè¦å¤å #ifdef __cpluseplusè¿ä¸é¨åå¢ï¼è¿æ¯å 为 PCH æ件æ¯ä¸ä¸ªä¼è¢«ææçæ件å¼å ¥ç头æ件ï¼èæ们åå¸æ #import <opencv2/opencv.hpp>è¿é¨ååªä¼è¢«ä¸äº C++å®ç°æ件ç¼è¯ï¼æ以æ们å ä¸#ifdef __cpluseplusæ¥è¡¨ç¤ºè¿æ¯ C++ æ件æä¼ç¼è¯çï¼é¤äº#ifdef __cpluseplusï¼è¿æ#ifdef __OBJC__è¿æ ·çå®æ¥è¯´æç¼è¯è§åï¼æç § OC æ件ç¼è¯ï¼ï¼è¿æ ·çå®å¤åºç°äºä¸äºä¼è¢«å¤ç§ç±»åçå®ç°æ件å¼ç¨ç头æ件ä¸ãå¦å¤æ³¨æå¦ä¸ä¸ªé®é¢ï¼å¦æä¸ä¸ªå¤´æ件æ¯C++ç±»åç头æ件ï¼é£ä¹ä¸å®è¦ä¿è¯ææç´æ¥æè é´æ¥å¼ç¨è¿ä¸ªå¤´æ件çå®ç°æ件é½è¦ä¸º.mmæè .cppï¼å¦å Xcode å°±ä¸ä¼æè¿ä¸ªå¤´æ件å½å C++头æ件æ¥ç¼è¯ï¼å°±ä¼åºç°æåºæ¬ç#include <iostream>è¿ç§å¼ç¨é½ä¼æ¥åºfile not found è¿æ ·çç¼è¯é误çé®é¢ãæå¨ç¼è¯çè¿ç¨ä¸ï¼æ个C++头æ件 A.h 被 B.h å¼ç¨ï¼ç¶å B.h å被 C.m å¼ç¨ï¼è½ç¶ B çå®ç°æä»¶æ¯ B.mm ï¼ä½æ¯ä»ç¶æ¥åºäºä¹å说çé£ä¸ªéè¯¯ï¼ æè°¢ StackOberflow 让ææ¾å°äºé®é¢åççåå ãæä»¥å¯¹äº C++ 头æ件çå¼ç¨ä¸å®è¦æ³¨æï¼ä½å¡æ¯å¼ç¨äº A.h çå®ç°é¨åï¼é½å¿ é¡»æ¯.mmæè .cppåç¼åãï¼åæ¶æ们ä¹å¯ä»¥ç¥éï¼Xcode æ¯æ ¹æ®å¤´æ件被å¼ç¨çæ åµæ¥å¤å®å¤´æ件çç¼è¯ ç±»åçï¼ã
å¨iosä¸å¦ä½å©ç¨ opencv è¿è¡å¾åå¤ç
å¨iosä¸å¦ä½å©ç¨ opencv è¿è¡å¾åå¤ççæ¹æ³ï¼SDKçæ¬
ä½è æä¾çå·¥ç¨SDKçæ¬å¯è½åä½ å¼åç¯å¢çä¸åï¼æä¸è½½å°çæ¯åºäº4.0çï¼ï¼éè¦è®¾ç½®æä½ ä½¿ç¨çSDKçæ¬ï¼å¨Groups&Filesçªå£çå·¥ç¨æä»¶æ ¹ç®å½å³é®-Get Infoï¼å¨buildæ çBase SDKä¸é¡¹ä¸ï¼æ¹ä¸ºä½ çSDKçæ¬ï¼
2. é¾æ¥æ è®° Other linker flags
æ¬å°çOpenCVåºè¦é¾æ¥è¿Xcodeå·¥ç¨ä¸ï¼å¨Get Infoçbuild项ä¸ï¼æç´¢ Other Linker Flagsï¼æ·»å ç¸åºçé¾æ¥æ è®°ï¼
iPhoneçæºè°è¯ï¼
-lstdc++
-lz
â$(SRCROOT)/OpenCV/lib/iPhoneOS/lib/libcv.aâ
â$(SRCROOT)/OpenCV/lib/iPhoneOS/lib/libcxcore.aâ
模æå¨è°è¯ï¼
-lstdc++
-lz
â$(SRCROOT)/OpenCV/lib/iPhoneSimulator/lib/libcv.aâ
â$(SRCROOT)/OpenCV/lib/iPhoneSimulator/lib/libcxcore.aâ
3. 头æ件æç´¢è·¯å¾ Header search paths
为æ¹ä¾¿çè°ç¨OpenCV头æ件ï¼è¦è®¾ç½®å¤´æ件æ索路å¾ï¼åæ ·å¨build项ä¸æç´¢Header search pathsï¼æ·»å ç¸åºçå 容ï¼
iPhoneçæºè°è¯ï¼
â$(SRCROOT)/OpenCV/lib/iPhoneOS/include/opencv/â
â$(SRCROOT)/OpenCV/lib/iPhoneOS/include/â
模æå¨è°è¯ï¼
â$(SRCROOT)/OpenCV/lib/iPhoneSimulator/include/opencv/â
â$(SRCROOT)/OpenCV/lib/iPhoneSimulator/include/â
4. Building OpenCV
æ两ç§æ¹å¼æ建OpenCVï¼ä¸ç§æ¯è½¬æ¢å°OpenCV targetåç¬æ建ï¼ä¸ç§æ¯å°OpenCVæ·»å 为主åºç¨çä¾èµï¼æ建主åºç¨æ¶ä¸åæ建ãç±äºOpenCVç代ç åºæ¬ä¸éè¦ä¿®æ¹ï¼ä¸æ建è¿ç¨å¾æ ¢ï¼æ以建议对OpenCVè¿è¡åç¬æ建ã
å¨Groups & Filesçªå£ä¸çTargetsæ ä¸ï¼åå»OpenCVï¼ç¹å»Build and Runè¿è¡ç¼è¯ãè¿ç¨æ¯è¾æ ¢ï¼ä¸è¬ä¼æ¥å 个erroråwarningï¼ä¸è¿åºæ¬å¯ä»¥æ è§ã
è¿è¡äºè¿äºè®¾ç½®ä¹åï¼ä¸»åºç¨åºæ¬å°±å¯ä»¥æ£å¸¸ç¼è¯è¿è¡äºï¼