导入ZBarSDK文件并引入一下框架
AVFoundation.framework
CoreMedia.framework
CoreVideo.framework
QuartzCore.framework
libiconv.dylib
引入头文件#import “ZBarSDK.h”即可使用
当找到条形码时,会执行代理方法
废话不多说,直接上代码
//
// LQXZBarViewController.m
// erweim
//
// Created by 刘祺旭 on 15/9/9.
// Copyright (c) 2015年 刘祺旭. All rights reserved.
//
#import "LQXZBarViewController.h"
@interface LQXZBarViewController ()
{
BOOL is_Anmotion;
ZBarReaderView*lqxreadview;
UIImageView*lqxscanZomeBack;
UIImageView*lqxreadLineView;
}
@end
@implementation LQXZBarViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self InitScan];
[self loopDrawLine];
}
#pragma mark 初始化扫描
- (void)InitScan
{
lqxreadview = [ZBarReaderView new];
lqxreadview.backgroundColor = [UIColor clearColor];
lqxreadview.frame = CGRectMake(187.5 - 150, 333.5 - 150, 300, 300);
lqxreadview.readerDelegate = self;
lqxreadview.allowsPinchZoom = YES;//使用手势变焦
lqxreadview.trackingColor = [UIColor redColor];
lqxreadview.showsFPS = NO;// 显示帧率 YES 显示 NO 不显示
lqxreadview.scanCrop = CGRectMake(50, 50, 50, 50);//将被扫描的图像的区域
UIImage *hbImage=[UIImage imageNamed:@"框.png"];
lqxscanZomeBack=[[UIImageView alloc] initWithImage:hbImage];
//添加一个背景图片
CGRect mImagerect=CGRectMake((lqxreadview.frame.size.width-200)/2.0, (lqxreadview.frame.size.height-200)/2.0, 200, 200);
[lqxscanZomeBack setFrame:mImagerect];
lqxreadview.scanCrop = [self getScanCrop:mImagerect readerViewBounds:lqxreadview.bounds];//将被扫描的图像的区域
[lqxreadview addSubview:lqxscanZomeBack];
[lqxreadview addSubview:lqxreadLineView];
[self.view addSubview:lqxreadview];
[lqxreadview start];
}
#pragma mark 获取扫描区域
-(CGRect)getScanCrop:(CGRect)rect readerViewBounds:(CGRect)readerViewBounds
{
CGFloat x,y,width,height;
x = rect.origin.x / readerViewBounds.size.width;
y = rect.origin.y / readerViewBounds.size.height;
width = rect.size.width / readerViewBounds.size.width;
height = rect.size.height / readerViewBounds.size.height;
return CGRectMake(x, y, width, height);
}
#pragma mark 扫描动画
-(void)loopDrawLine
{
CGRect rect = CGRectMake(lqxscanZomeBack.frame.origin.x, lqxscanZomeBack.frame.origin.y, lqxscanZomeBack.frame.size.width, 2);
if (lqxreadLineView) {
[lqxreadLineView removeFromSuperview];
}
lqxreadLineView = [[UIImageView alloc] initWithFrame:rect];
lqxreadLineView.backgroundColor = [UIColor redColor];
[lqxreadLineView setImage:[UIImage imageNamed:@"线.jpg"]];
[UIView animateWithDuration:3.0
delay: 0.0
options: UIViewAnimationOptionCurveEaseIn
animations:^{
//修改fream的代码写在这里
lqxreadLineView.frame =CGRectMake(lqxscanZomeBack.frame.origin.x, lqxscanZomeBack.frame.origin.y+lqxscanZomeBack.frame.size.height, lqxscanZomeBack.frame.size.width, 2);
[lqxreadLineView setAnimationRepeatCount:0];
}
completion:^(BOOL finished){
if (!is_Anmotion) {
[self loopDrawLine];
}
}];
[lqxreadview addSubview:lqxreadLineView];
}
#pragma mark 获取扫描结果
- (void)readerView:(ZBarReaderView *)readerView didReadSymbols:(ZBarSymbolSet *)symbols fromImage:(UIImage *)image
{
// 得到扫描的条码内容
const zbar_symbol_t *symbol = zbar_symbol_set_first_symbol(symbols.zbarSymbolSet);
NSString *symbolStr = [NSString stringWithUTF8String: zbar_symbol_get_data(symbol)];
if (zbar_symbol_get_type(symbol) == ZBAR_QRCODE) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:symbolStr]];
}
for (ZBarSymbol *symbol in symbols) {
break;
}
[readerView stop];
is_Anmotion = YES;
[readerView removeFromSuperview];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end