iOS开发利用zbar实现二维码扫描(支持64位) 及二维码生成

//二维码生成需要导入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(50, 130, Width - 100, Height - 350)];
    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(100, 100, 120, 40);
    [scanButton addTarget:self action:@selector(setupCamera) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:scanButton];

}
-(void)setupCamera
{
    [self scanBtnAction];
}
-(void)scanBtnAction
{
    num = 0;
    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(0.1, 0.2, 0.8, 0.8);//扫描的感应框
    ZBarImageScanner * scanner = reader.scanner;
    [scanner setSymbology:ZBAR_I25
                   config:ZBAR_CFG_ENABLE
                       to:0];
    UIView *OverlayView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,Width, Height)];
    OverlayView.backgroundColor = [UIColor clearColor];
    reader.cameraOverlayView = OverlayView;


    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(30, 30, self.view.frame.size.width - 60, 40)];
    label.text = @"请将扫描的二维码至于下面的框内\n谢谢!";
    label.font = [UIFont systemFontOfSize:14.0];

    label.textColor = [UIColor whiteColor];
    label.numberOfLines = 0;
    label.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];
    [OverlayView addSubview:label];

    image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pick_bg.png"]];
    image.frame = CGRectMake(30, 100, Width - 60, 280);



    _line = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, image.frame.size.width - 20, 2)];
    _line.image = [UIImage imageNamed:@"line.png"];
    [image addSubview:_line];
    [OverlayView addSubview:image];
    //定时器,设定时间过1.5秒,
    timer = [NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:@selector(animation) userInfo:nil repeats:YES];

    // 用于取消操作的button
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelButton setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.5]];
    [cancelButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [cancelButton setFrame:CGRectMake(30, Height - 80, Width - 60, 40)];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton.titleLabel setFont:[UIFont boldSystemFontOfSize:17.0]];
    [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(10, 10 + 2*num, image.frame.size.width - 20, 2);
        if (2*num == CGRectGetHeight(image.frame) - 20) {
            upOrdown = YES;
        }
    }
    else {
        num --;
        _line.frame = CGRectMake(10, 10 + 2*num, image.frame.size.width - 20, 2);
        if (num == 0) {
            upOrdown = NO;
        }
    }

}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [timer invalidate];
    _line.frame = CGRectMake(10, 10, image.frame.size.width - 20, 2);
    num = 0;
    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下载支持64位](http://pan.baidu.com/s/1FFiIq)

你可能感兴趣的:(64位,ios开发,二维码生成,二维码扫描)