转载自:http://hi.baidu.com/osbornwh/item/d44eb9d9f34702e1795daadc
一直没有自己写过文章,在互联网上获得了很多,回报一下吧!
首先下载zxing的skd下载地址是:http://code.google.com/p/zxing/downloads/list,目前最新的是2.0
下好之后把cppg跟iphone这2个文件夹拷贝出来一定要如图所示目录
然后把“zxing/iphone/ZXingWidget/”下的ZXingWidget.xcodeproj拖到你的文件下
如图
接着加静态库什么的!按http://yannickloriot.com/2011/04/how-to-install-zxing-in-xcode-4/这个网址的来做就可以了。。然后要注意的就是那个头文件的读取要这样改
然后这个z xing有2种扫描方式,一种是直接扫描,一种是拍照扫描
直接扫描的话用如下代码
ZXingWidgetController*widController = [[ZXingWidgetControlleralloc] initWithDelegate:selfshowCancel:YESOneDMode:NO];
// QRCodeReader* qrcodeReader = [[QRCodeReader alloc] init]; (文章中是用下边那句话,但我的测试是用这句话才识别得到chenyong05314)
DataMatrixReader* DMReader = [[DataMatrixReaderalloc] init];
//MultiFormatUPCEANReader *fomReader=[[MultiFormatUPCEANReader alloc] init];
NSSet*readers = [[NSSetalloc] initWithObjects:DMReader,nil];
widController.readers= readers;
// [readers release];
NSBundle*mainBundle = [NSBundlemainBundle];
widController.soundToPlay=
[NSURLfileURLWithPath:[mainBundle pathForResource:@"beep-beep"ofType:@"aiff"] isDirectory:NO];
[selfpresentModalViewController:widController animated:YES];
[DMReader release];
//[fomReader release];
[readers release];
[widController release];
然后再头文件加入他的deleate,然后就可以实现
- (void)zxingController:(ZXingWidgetController*)controller didScanResult:(NSString*)result {
}跟
- (void)zxingControllerDidCancel:(ZXingWidgetController*)controller {
[selfdismissModalViewControllerAnimated:YES];
}
这2个函数!第一个result就是你要的二维码!
第二种方式是用如下代码
UIImagePickerController*pickCtr = [[UIImagePickerControlleralloc] init];
pickCtr.sourceType= UIImagePickerControllerSourceTypeCamera;
pickCtr.delegate= self;
pickCtr.allowsEditing=YES;
//pickCtr.videoQuality= UIImagePickerControllerQualityType640x480;
[selfpresentModalViewController:pickCtr animated:YES];
[pickCtr release];
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
//NSLog(@"info ==== %@", info);
UIImage*image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
//QRCodeReader* qrcodeReader = [[QRCodeReader alloc] init];
DataMatrixReader* DMReader = [[DataMatrixReaderalloc] init];
NSSet*readers = [[NSSetalloc] initWithObjects:DMReader,nil];
[DMReader release];
//CGRect cropRect = [UIScreen mainScreen].applicationFrame;
Decoder*d = [[Decoderalloc] init];
d.readers= readers;
d.delegate= self;
intres = [d decodeImage:image];
NSLog(@"res=%d",res);
[d release];
[selfdismissModalViewControllerAnimated:YES];
}
然后包含
UIImagePickerControllerDelegate,UINavigationControllerDelegate这2个deleate。
- (void)decoder:(Decoder*)decoder didDecodeImage:(UIImage*)image usingSubset:(UIImage*)subset withResult:(TwoDDecoderResult*)twoDResult {
}
- (void)decoder:(Decoder*)decoder failedToDecodeImage:(UIImage*)image usingSubset:(UIImage*)subset reason:(NSString*)reason {
}
然后[twoDResult text]就是你要的二唯码!