1,二维码扫面用ios7 自带控件
@property ( strong , nonatomic ) AVCaptureDevice * device;
@property ( strong , nonatomic ) AVCaptureDeviceInput * input;
@property ( strong , nonatomic ) AVCaptureMetadataOutput * output;
@property ( strong , nonatomic ) AVCaptureSession * session;
@property ( strong , nonatomic ) AVCaptureVideoPreviewLayer * preview;
if ([AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]==AVAuthorizationStatusDenied) {
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"未能调用相机,请检查“设置->隐私->相机”是否打开" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert show];
return;
}
self.device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
self.input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
self.output = [[AVCaptureMetadataOutput alloc]init];
[self.output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
//[self.output setRectOfInterest:CGRectMake((124)/ScreenHeight,((ScreenWidth-220)/2)/ScreenWidth,220/ScreenHeight,220/ScreenWidth)];
self.session = [[AVCaptureSession alloc]init];
if ([self.session canAddInput:self.input]) {
[self.session addInput:self.input];
}
if ([self.session canAddOutput:self.output]) {
[self.session addOutput:self.output];
}
self.output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode];
self.preview = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
self.preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
self.preview.frame = self.view.layer.bounds;
[self.view.layer insertSublayer:self.preview atIndex:0];
[self.session startRunning];
//回调函数
#pragma mark avcapturemetadataoutputobjectsdelegate
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
NSString * stringvalue;
if ([metadataObjects count]>0) {
[self.session stopRunning];
AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex:0];
stringvalue = metadataObject.stringValue;
NSLog(@"qrcode=%@",stringvalue);
}
NSLog(@"qrcodeOK");
[self.delegate KBErweimaFinishWithString:stringvalue];//对数据进行处理的 delegate
[self.navigationController popViewControllerAnimated:YES];
}
2相册图片的二维码解析 http://download.csdn.net/detail/zyq527758142/8428825 64位 zbar 下载地址 记得引入 lib
icon.dylib 库
#pragma mark-调用相册二维码
-(void)photoErwei{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
picker.allowsEditing = YES;
[self presentModalViewController:picker animated:YES];
}else{
NSLog(@"相册不能用。。。");
}
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
ZBarReaderController* read = [ZBarReaderController new];
read.readerDelegate = self;
CGImageRef cgImageRef = image.CGImage;
ZBarSymbol* symbol = nil;
for(symbol in [read scanImage:cgImageRef])
break;
// pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:symbol.data];
// NSLog(@"symbol.data:%@",symbol.data);
[self.delegate KBErweimaFinishWithString:symbol.data];
[picker dismissViewControllerAnimated:YES completion:nil];
NSLog(@"二维码棒棒的");
}
3二维码生成
#import "QRCodeGenerator.h" //http://download.csdn.net/download/sjx19871225/5065683 下载地址
self.qrid = [[dic valueForKey:@"data"] valueForKey:@"token"];
NSString * str = [NSString stringWithFormat:@"%@%@",DOWNLOADSERVER,self.qrid];
self.qrImage.image = [QRCodeGenerator qrImageForString:str imageSize:200];