//二維碼生成需要導入libiconv.dylib
#import "ViewController.h"
#import "ZBarSDK.h"
#import "QRCodeGenerator.h"
#define Width self.view.frame.size.width
#define Height self.view.frame.size.height
@interface ViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate,ZBarReaderDelegate>
{
NSInteger num;
BOOL upOrdown;//判斷掃描線的狀态
NSTimer *timer;
UIImageView *image;//邊框
}
//掃描線
@property (nonatomic, strong) UIImageView *line;
@end
@implementation ViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//二維碼生成
UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(, , Width - , Height - )];
imageView.image = [QRCodeGenerator qrImageForString:[NSString stringWithFormat:@"生成二維碼的内容"] imageSize:imageView.bounds.size.width];
[self.view addSubview:imageView];
//二維碼掃描
UIButton * scanButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[scanButton setTitle:@"掃描" forState:UIControlStateNormal];
scanButton.frame = CGRectMake(, , , );
[scanButton addTarget:self action:@selector(setupCamera) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:scanButton];
}
-(void)setupCamera
{
[self scanBtnAction];
}
-(void)scanBtnAction
{
num = ;
upOrdown = NO;
//初始話ZBar
ZBarReaderViewController *reader = [ZBarReaderViewController new];
//隐藏底部控制按鈕
reader.showsZBarControls = NO;
/**
* 删除頁面上的控件
*/
for (UIView *temp in [reader.view subviews]){
for (UIButton* btn in [temp subviews]) {
if ([btn isKindOfClass:[UIButton class]]) {
[btn removeFromSuperview];
}
}
// 去掉 toolbar
for (UIToolbar* tool in [temp subviews]) {
if ([tool isKindOfClass:[UIToolbar class]]) {
[tool setHidden:YES];
[tool removeFromSuperview];
}
}
}
//設定代理
reader.readerDelegate = self;
//支援界面旋轉
reader.supportedOrientationsMask = ZBarOrientationMaskAll;
reader.showsHelpOnFail = NO;
reader.scanCrop = CGRectMake(, , , );//掃描的感應框
ZBarImageScanner * scanner = reader.scanner;
[scanner setSymbology:ZBAR_I25
config:ZBAR_CFG_ENABLE
to:];
UIView *OverlayView = [[UIView alloc] initWithFrame:CGRectMake(, ,Width, Height)];
OverlayView.backgroundColor = [UIColor clearColor];
reader.cameraOverlayView = OverlayView;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(, , self.view.frame.size.width - , )];
label.text = @"請将掃描的二維碼至于下面的框内\n謝謝!";
label.font = [UIFont systemFontOfSize:];
label.textColor = [UIColor whiteColor];
label.numberOfLines = ;
label.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:];
[OverlayView addSubview:label];
image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pick_bg.png"]];
image.frame = CGRectMake(, , Width - , );
_line = [[UIImageView alloc] initWithFrame:CGRectMake(, , image.frame.size.width - , )];
_line.image = [UIImage imageNamed:@"line.png"];
[image addSubview:_line];
[OverlayView addSubview:image];
//定時器,設定時間過1.5秒,
timer = [NSTimer scheduledTimerWithTimeInterval: target:self selector:@selector(animation) userInfo:nil repeats:YES];
// 用于取消操作的button
UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
[cancelButton setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:]];
[cancelButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
[cancelButton setFrame:CGRectMake(, Height - , Width - , )];
[cancelButton setTitle:@"取消" forState:UIControlStateNormal];
[cancelButton.titleLabel setFont:[UIFont boldSystemFontOfSize:]];
[cancelButton addTarget:self action:@selector(dismissOverlayView:)forControlEvents:UIControlEventTouchUpInside];
[OverlayView addSubview:cancelButton];
[self presentViewController:reader animated:YES completion:^{
}];
}
//取消
- (void)dismissOverlayView:(UIButton *)sender
{
[self dismissViewControllerAnimated:YES completion:^{
[timer invalidate];
}];
}
-(void)animation
{
if (upOrdown == NO) {
num ++;
_line.frame = CGRectMake(, + *num, image.frame.size.width - , );
if (*num == CGRectGetHeight(image.frame) - ) {
upOrdown = YES;
}
}
else {
num --;
_line.frame = CGRectMake(, + *num, image.frame.size.width - , );
if (num == ) {
upOrdown = NO;
}
}
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[timer invalidate];
_line.frame = CGRectMake(, , image.frame.size.width - , );
num = ;
upOrdown = NO;
[picker dismissViewControllerAnimated:YES completion:^{
[picker removeFromParentViewController];
}];
}
//二維碼掃描結果
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:^{
[picker removeFromParentViewController];
id <NSFastEnumeration> results = [info objectForKey:ZBarReaderControllerResults];
ZBarSymbol *symbol = nil;
for(symbol in results)
break;
if(symbol.data && [symbol.data rangeOfString:@"http:"].location != NSNotFound)
{
NSString *regex = @"http+:[^\\s]*";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
// 正規表達式判斷 是否包含 http:
if ([predicate evaluateWithObject:symbol.data])
{
// 判斷是不是我們自己的二維碼
if ([symbol.data rangeOfString:@"http://m.pinlehuo.com/api.php"].location != NSNotFound) {
NSLog(@"-------%@", symbol.data);
}else{
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:symbol.data]];
}
}
} else {
NSLog(@"symbol.data = %@", symbol.data);
}
}];
}
@end
[zbar下載下傳支援位](http://pan.baidu.com/s/1FFiIq)