上次是根据系统的属性自己封装的一个二维码扫描,这次给大家介绍一下基于ZBar集成的类似于QQ二维码扫描界面的二维码扫描的效果。

                                                               最下方配有效果图哦!

首先,需要引入ZBar第三方库文件:

ZBarSDK

libqrencode

其次,利用ZBar集成二维码扫描需要引入的类库有:

libiconv.tbd

QuartzCore.framework

CoreVideo.framework

CoreMedia.framework

AVFoundation.framework

代码实现:

-(void)createView{

//扫描页的背景图片

UIImageView*bgImageView;

if (self.view.frame.size.height<500) {

UIImage*image= [UIImage imageNamed:@"qrcode_scan_bg_Green.png"];

bgImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64-100)];

bgImageView.contentMode=UIViewContentModeTop;

bgImageView.clipsToBounds=YES;

bgImageView.image=image;

bgImageView.userInteractionEnabled=YES;

}else if(self.view.frame.size.height<600){

UIImage*image= [UIImage imageNamed:@"qrcode_scan_bg_Green_iphone5"];

bgImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64-100)];

bgImageView.contentMode=UIViewContentModeTop;

bgImageView.clipsToBounds=YES;

bgImageView.image=image;

bgImageView.userInteractionEnabled=YES;

}

else if(self.view.frame.size.height<680){

UIImage*image= [UIImage imageNamed:@"qrcode_scan_bg_Green_iphone6"];

bgImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64-100)];

bgImageView.contentMode=UIViewContentModeTop;

bgImageView.clipsToBounds=YES;

bgImageView.image=image;

bgImageView.userInteractionEnabled=YES;

}

else{

UIImage*image= [UIImage imageNamed:@"qrcode_scan_bg_Green_iphone6p"];

bgImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64-100)];

bgImageView.contentMode=UIViewContentModeTop;

bgImageView.clipsToBounds=YES;

bgImageView.image=image;

bgImageView.userInteractionEnabled=YES;

}

[self.view addSubview:bgImageView];

//扫描框下面的提示语

if (self.view.frame.size.height<600) {

UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-200, self.view.frame.size.width, 40)];

label.text = @"将取景框对准二维码,即可自动扫描。";

label.font=[UIFont systemFontOfSize:12];

label.textColor = [UIColor whiteColor];

label.textAlignment = NSTextAlignmentCenter;

label.lineBreakMode = NSLineBreakByWordWrapping;

label.numberOfLines = 2;

label.backgroundColor = [UIColor clearColor];

[bgImageView addSubview:label];

}else{

UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-300, self.view.frame.size.width, 40)];

label.text = @"将取景框对准二维码,即可自动扫描。";

label.font=[UIFont systemFontOfSize:17];

label.textColor = [UIColor whiteColor];

label.textAlignment = NSTextAlignmentCenter;

label.lineBreakMode = NSLineBreakByWordWrapping;

label.numberOfLines = 2;

label.backgroundColor = [UIColor clearColor];

[bgImageView addSubview:label];

}

//初始化扫描线

//4s/5/5s

if (self.view.frame.size.height<600) {

_line = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/5.8, 50, 220, 2)];

}

else{

//6/6s/6p

_line = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/4.6, 50, 220, 2)];

}

_line.image = [UIImage imageNamed:@"qrcode_scan_light_green.png"];

[bgImageView addSubview:_line];

//下方相册

UIImageView*scanImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, bgImageView.frame.size.height+64, self.view.frame.size.width, 100)];

scanImageView.image=[UIImage imageNamed:@"qrcode_scan_bar.png"];

scanImageView.userInteractionEnabled=YES;

[self.view addSubview:scanImageView];

NSArray*unSelectImageNames=@[@"qrcode_scan_btn_photo_nor.png",@"qrcode_scan_btn_flash_nor.png",@"qrcode_scan_btn_myqrcode_nor.png"];

NSArray*selectImageNames=@[@"qrcode_scan_btn_photo_down.png",@"qrcode_scan_btn_flash_down.png",@"qrcode_scan_btn_myqrcode_down.png"];

for (int i=0; i<unSelectImageNames.count; i++) {

UIButton*button=[UIButton buttonWithType:UIButtonTypeCustom];

[button setImage:[UIImage imageNamed:unSelectImageNames[i]] forState:UIControlStateNormal];

[button setImage:[UIImage imageNamed:selectImageNames[i]] forState:UIControlStateHighlighted];

button.frame=CGRectMake(self.view.frame.size.width/3*i, 0, self.view.frame.size.width/3, 100);

[scanImageView addSubview:button];

if (i==0) {

[button addTarget:self action:@selector(pressPhotoLibraryButton:) forControlEvents:UIControlEventTouchUpInside];

}

if (i==1) {

[button addTarget:self action:@selector(flashLightClick) forControlEvents:UIControlEventTouchUpInside];

}

if (i==2) {

button.hidden=YES;

}

}

//假导航

UIImageView*navImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];

navImageView.image=[UIImage imageNamed:@"qrcode_scan_bar.png"];

navImageView.userInteractionEnabled=YES;

[self.view addSubview:navImageView];

UILabel*titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2-32,20 , 64, 44)];

titleLabel.textColor=[UIColor whiteColor];

titleLabel.text=@"扫一扫";

[navImageView addSubview:titleLabel];

//返回按钮

UIButton*button = [UIButton buttonWithType:UIButtonTypeCustom];

[button setImage:[UIImage imageNamed:@"qrcode_scan_titlebar_back_pressed@2x.png"] forState:UIControlStateHighlighted];

[button setImage:[UIImage imageNamed:@"qrcode_scan_titlebar_back_nor.png"] forState:UIControlStateNormal];

[button setFrame:CGRectMake(10,15, 48, 48)];

[button addTarget:self action:@selector(pressCancelButton:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:button];

timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(animation1) userInfo:nil repeats:YES];

}

//扫描线的动画效果

-(void)animation1

{

[UIView animateWithDuration:2 animations:^{

if (self.view.frame.size.height<600) {

_line.frame = CGRectMake(self.view.frame.size.width/5.8, self.view.frame.size.height/43*22, 220, 2);

}else{

_line.frame = CGRectMake(self.view.frame.size.width/4.6, self.view.frame.size.height/43*22, 220, 2);

}

}completion:^(BOOL finished) {

[UIView animateWithDuration:2 animations:^{

if (self.view.frame.size.height<600) {

_line.frame = CGRectMake(self.view.frame.size.width/5.8, 50, 220, 2);

}

else{

_line.frame = CGRectMake(self.view.frame.size.width/5.8, 50, 220, 2);

}

}];

}];

}

//开启关闭闪光灯

-(void)flashLightClick{

AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

if (device.torchMode==AVCaptureTorchModeOff) {

//闪光灯开启

[device lockForConfiguration:nil];

[device setTorchMode:AVCaptureTorchModeOn];

}else {

//闪光灯关闭

[device setTorchMode:AVCaptureTorchModeOff];

}

}

- (void)viewDidLoad

{

//相机界面的定制在self.view上加载即可

BOOL Custom= [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];//判断摄像头是否能用

if (Custom) {

[self initCapture];//启动摄像头

}

[self createView];

[super viewDidLoad];

}

#pragma mark 选择相册

- (void)pressPhotoLibraryButton:(UIButton *)button

{  if (timer) {

[timer invalidate];

timer=nil;

}

_line.frame = CGRectMake(50, 50, 220, 2);

num = 0;

upOrdown = NO;

UIImagePickerController *picker = [[UIImagePickerController alloc] init];

picker.allowsEditing = YES;

picker.delegate = self;

picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

[self presentViewController:picker animated:YES completion:^{

self.isScanning = NO;

[self.captureSession stopRunning];

}];

}

#pragma mark 点击取消

- (void)pressCancelButton:(UIButton *)button

{

self.isScanning = NO;

[self.captureSession stopRunning];

self.ScanResult(nil,NO);

if (timer) {

[timer invalidate];

timer=nil;

}

_line.frame = CGRectMake(50, 50, 220, 2);

num = 0;

upOrdown = NO;

[self dismissViewControllerAnimated:YES completion:nil];

}

#pragma mark 开启相机

- (void)initCapture

{

self.captureSession = [[AVCaptureSession alloc] init];

AVCaptureDevice* inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

AVCaptureDeviceInput *captureInput = [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:nil];

[self.captureSession addInput:captureInput];

AVCaptureVideoDataOutput *captureOutput = [[AVCaptureVideoDataOutput alloc] init];

captureOutput.alwaysDiscardsLateVideoFrames = YES;

if (IOS7) {

AVCaptureMetadataOutput*_output=[[AVCaptureMetadataOutput alloc]init];

[_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];

[self.captureSession setSessionPreset:AVCaptureSessionPresetHigh];

[self.captureSession addOutput:_output];

_output.metadataObjectTypes =@[AVMetadataObjectTypeQRCode];

if (!self.captureVideoPreviewLayer) {

self.captureVideoPreviewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];

}

// NSLog(@"prev %p %@", self.prevLayer, self.prevLayer);

self.captureVideoPreviewLayer.frame = self.view.bounds;

self.captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

[self.view.layer addSublayer: self.captureVideoPreviewLayer];

self.isScanning = YES;

[self.captureSession startRunning];

}else{

[captureOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];

NSString* key = (NSString *)kCVPixelBufferPixelFormatTypeKey;

NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA];

NSDictionary *videoSettings = [NSDictionary dictionaryWithObject:value forKey:key];

[captureOutput setVideoSettings:videoSettings];

[self.captureSession addOutput:captureOutput];

NSString* preset = 0;

if (NSClassFromString(@"NSOrderedSet") && // Proxy for "is this iOS 5" ...

[UIScreen mainScreen].scale > 1 &&

[inputDevice

supportsAVCaptureSessionPreset:AVCaptureSessionPresetiFrame960x540]) {

// NSLog(@"960");

preset = AVCaptureSessionPresetiFrame960x540;

}

if (!preset) {

// NSLog(@"MED");

preset = AVCaptureSessionPresetMedium;

}

self.captureSession.sessionPreset = preset;

if (!self.captureVideoPreviewLayer) {

self.captureVideoPreviewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];

}

// NSLog(@"prev %p %@", self.prevLayer, self.prevLayer);

self.captureVideoPreviewLayer.frame = self.view.bounds;

self.captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

[self.view.layer addSublayer: self.captureVideoPreviewLayer];

self.isScanning = YES;

[self.captureSession startRunning];

}

}

- (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer

{

CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);

// Lock the base address of the pixel buffer

CVPixelBufferLockBaseAddress(imageBuffer,0);

// Get the number of bytes per row for the pixel buffer

size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);

// Get the pixel buffer width and height

size_t width = CVPixelBufferGetWidth(imageBuffer);

size_t height = CVPixelBufferGetHeight(imageBuffer);

// Create a device-dependent RGB color space

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

if (!colorSpace)

{

NSLog(@"CGColorSpaceCreateDeviceRGB failure");

return nil;

}

// Get the base address of the pixel buffer

void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);

// Get the data size for contiguous planes of the pixel buffer.

size_t bufferSize = CVPixelBufferGetDataSize(imageBuffer);

// Create a Quartz direct-access data provider that uses data we supply

CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, baseAddress, bufferSize,NULL);

// Create a bitmap image from data supplied by our data provider

CGImageRef cgImage =

CGImageCreate(width,height,8,32,bytesPerRow,colorSpace,kCGImageAlphaNoneSkipFirst |kCGBitmapByteOrder32Little,provider,NULL,true,kCGRenderingIntentDefault);

CGDataProviderRelease(provider);

CGColorSpaceRelease(colorSpace);

// Create and return an image object representing the specified Quartz image

UIImage *image = [UIImage imageWithCGImage:cgImage];

 return image;

}

#pragma mark 对图像进行解码

- (void)decodeImage:(UIImage *)image

{

self.isScanning = NO;

ZBarSymbol *symbol = nil;

ZBarReaderController* read = [ZBarReaderController new];

read.readerDelegate = self;

CGImageRef cgImageRef = image.CGImage;

for(symbol in [read scanImage:cgImageRef])break;

if (symbol!=nil) {

if (timer) {

[timer invalidate];

timer=nil;

}

_line.frame = CGRectMake(50, 50, 220, 2);

num = 0;

upOrdown = NO;

self.ScanResult(symbol.data,YES);

[self.captureSession stopRunning];

[self dismissViewControllerAnimated:YES completion:nil];

}else{

timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(animation1) userInfo:nil repeats:YES];

num = 0;

upOrdown = NO;

self.isScanning = YES;

[self.captureSession startRunning];

}

}

#pragma mark - AVCaptureVideoDataOutputSampleBufferDelegate

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection

{

UIImage *image = [self imageFromSampleBuffer:sampleBuffer];

[self decodeImage:image];

}

#pragma mark AVCaptureMetadataOutputObjectsDelegate//IOS7下触发

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection

{

  if (metadataObjects.count>0)

{

AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex:0];

self.ScanResult(metadataObject.stringValue,YES);

}

[self.captureSession stopRunning];

_line.frame = CGRectMake(50, 50, 220, 2);

num = 0;

upOrdown = NO;

[self dismissViewControllerAnimated:YES completion:nil];

}

#pragma mark - UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{

if (timer) {

[timer invalidate];

timer=nil;

}

_line.frame = CGRectMake(50, 50, 220, 2);

num = 0;

upOrdown = NO;

UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"];

[self dismissViewControllerAnimated:YES completion:^{[self decodeImage:image];}];

}

//相册取消

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

{

if (timer) {

[timer invalidate];

timer=nil;

}

_line.frame = CGRectMake(50, 50, 220, 2);

num = 0;

upOrdown = NO;

timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(animation1) userInfo:nil repeats:YES];

[self dismissViewControllerAnimated:YES completion:^{

self.isScanning = YES;

[self.captureSession startRunning];

}];

}

#pragma mark - DecoderDelegate

+(NSString*)zhengze:(NSString*)str

{

NSError *error;

//http+:[^\\s]* 这是检测网址的正则表达式

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"http+:[^\\s]*" options:0 error:&error];//筛选

if (regex != nil) {

NSTextCheckingResult *firstMatch = [regex firstMatchInString:str options:0 range:NSMakeRange(0, [str length])];

if (firstMatch) {

NSRange resultRange = [firstMatch rangeAtIndex:0];

//从urlString中截取数据

NSString *result1 = [str substringWithRange:resultRange];

NSLog(@"正则表达后的结果%@",result1);

return result1;

}

}

return nil;

}

效果图:

开灯可以打开系统的手电筒,相册可以进入系统相册,如果选择的图片中包含有二维码,既可以自动扫描!

如有问题可以评论提问,有评必回!!!

2015最后一天,祝大家新年快乐!记得给个赞哦^_^

二维码扫描利用ZBar实现的更多相关文章

  1. iOS 二维码扫描 通过ZBar ZXing等第三方库

    扫描二维码的开源库有很多如 ZBar.ZXing等 ZBar的使用方法: 下载ZBar SDK 地址https://github.com/bmorton/ZBarSDK ZBarSDK是一个开源的SD ...

  2. 二维码开源库zbar、zxing使用心得

    首先说明我的测试场景是“识别打印在纸上的二维码”,在扫描结果中寻找二维码并进行识别,而不是直接让摄像头对着二维码扫描. zbar和zxing用的都是自己从github上clone的c++源码/接口编译 ...

  3. android利用zbar二维码扫描-(解决中文乱码及扫描区域定义)

    写在最前(这是对上一篇博文的问题做的更新[android利用zbar二维码扫描]) project下载   zbarLib编译project  project下载0积分 bug 在2.3的系统中Hol ...

  4. Android快速实现二维码扫描--Zbar

    Android中二维码扫描的最常用库是zxing和zbar,上一篇<Android快速实现二维码扫描–Zxing>介绍了Zxing.这次说Zbar,Zbar速度极快,我就比较常用,项目地址 ...

  5. ZBar 是款桌面电脑用条形码/二维码扫描工具

    ZBar 是款桌面电脑用条形码/二维码扫描工具 windows平台python 2.7环境编译安装zbar   最近一个项目需要识别二维码,找来找去找到了zbar和zxing,中间越过无数坑,总算基本 ...

  6. iOS使用AVFoundation实现二维码扫描(ios7以上)——转载

    关于二维码扫描有不少优秀第三方库: ZBar SDK 里面有详细的文档,相应介绍也非常多,如:http://rdcworld-iphone.blogspot.in/2013/03/how-to-use ...

  7. iOS使用AVFoundation实现二维码扫描

    原文:http://strivingboy.github.io/blog/2014/11/08/scan-qrcode/ 关于二维码扫描有不少优秀第三方库如: ZBar SDK 里面有详细的文档,相应 ...

  8. 【转】 iOS使用AVFoundation实现二维码扫描

    原文:http://strivingboy.github.io/blog/2014/11/08/scan-qrcode/ 关于二维码扫描有不少优秀第三方库如: ZBar SDK 里面有详细的文档,相应 ...

  9. Ios二维码扫描(系统自带的二维码扫描)

    Ios二维码扫描 这里给大家介绍的时如何使用系统自带的二维码扫描方法和一些简单的动画! 操作步骤: 1).首先你需要搭建UI界面如图:下图我用了俩个imageview和一个label 2).你需要在你 ...

随机推荐

  1. python BDD 框架之lettuce

    http://pythonhosted.org/lettuce/ 介绍                                                                 ...

  2. 使用WinDbg调试SQL Server——入门

    这篇文章我想探究下SQL Server里完全不同的领域:如果使用WinDbg(来自针对Windows的调试工具)调试SQL Server.在我们进入枯涩细节之前,我想详细解释下为什么选择这样晦涩的话题 ...

  3. 叨叨PS那些活

    临睡前记得今天技术小结没写...就起来叨叨些使用Photoshop做网站的活吧. 一般网站的建站流程和人员配置是: 1 美工,创建页面的psd图 2 前端工程师,根据psd图,切出html页面 3 后 ...

  4. springMVC注解@initbinder日期类型的属性自动转换

    在实际操作中经常会碰到表单中的日期 字符串和Javabean中的日期类型的属性自动转换, 而springMVC默认不支持这个格式的转换,所以必须要手动配置, 自定义数据类型的绑定才能实现这个功能. 一 ...

  5. .net开发微信公众平台

    一.说明:公众平台信息接口为开发者提供了一种新的消息处理方式,只有申请成为开发者后,你才能使用公众平台的开发功能,在这里你需要填写一个URL和一个Token,这两项信息也需要你拥有自己的服务器(外网服 ...

  6. Python入门笔记(16):对文件的操作(2)

    一.文件系统 从系统角度来看,文件系统是对文件存储器空间进行组织和分配,负责文件存储并对存入的文件进行保护和检索的系统.具体地说,它负责为用户建立文件,存入.读出.修改.转储文件,控制文件的存取,当用 ...

  7. MVC5 + EF6 + Bootstrap3 (11) 排序、搜索、分页

    系列教程:MVC5 + EF6 + Bootstrap3 上一节:MVC5 + EF6 + Bootstrap3 (10) 数据查询页面 源码下载:点我下载 我工作的源码:http://www.jin ...

  8. 002_Razor简介

    关于 Razor: Razor 语句以 @ 字符开始.在使用 Razor 声明视图模型对象的类型时要使用小写字母,如在本例文件 Index.cshtml 文件中 @model 以小写的 m 开头,但要 ...

  9. 与众不同 windows phone (51) - 8.1 新增控件: DatePickerFlyout, TimePickerFlyout

    [源码下载] 与众不同 windows phone (51) - 8.1 新增控件: DatePickerFlyout, TimePickerFlyout 作者:webabcd 介绍与众不同 wind ...

  10. ActiveReports 报表应用教程 (5)---解密电子商务领域首张电子发票的诞生(套打报表)

    6月27日京东商城发布了中国电子商务领域首张电子发票,同时宣布相关系统正式上线,这标志着中国电子商务的步伐又向前迈出了重要的一步.目前“电子发票”覆盖的服务范围是在北京地区购买图书.音像商品的个人消费 ...