二維碼框架ZBarSDK的使用和自定義二維碼掃描界面方法

如果你不知道ZBarSDK怎麼用,請下載demo

http://download.csdn.net/detail/u013686641/7858917


如果你已經配置好ZBarSDK ,那麼下面這個類可以直接用

下面是效果圖



//

//  頭文件

//  TestProject

//


#import <UIKit/UIKit.h>

#import "ZBarSDK.h"


@interface yxpQrCode :UIViewController

@end

//

//  實現文件

//  TestProject

//

#import "yxpQrCode.h"


#define SCANVIEW_EdgeTop 40.0

#define SCANVIEW_EdgeLeft 50.0


#define TINTCOLOR_ALPHA 0.2  //淺色透明度

#define DARKCOLOR_ALPHA 0.5  //深色透明度


@interfaceyxpQrCode ()<ZBarReaderViewDelegate>

{

   UIView *_QrCodeline;

   NSTimer *_timer;

    

    //設置掃描畫面

   UIView *_scanView;

   ZBarReaderView *_readerView;

}


@end


@implementation yxpQrCode


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

   self = [superinitWithNibName:nibNameOrNil bundle:nibBundleOrNil];

   if (self) {

        // Custom initialization

    }

    returnself;

}


- (void)viewDidLoad

{

    [superviewDidLoad];

    

    self.title=@"掃描二維碼";

    //初始化掃描界面

    [selfsetScanView];

    

     _readerView= [[ZBarReaderViewalloc]init];

    _readerView.frame =CGRectMake(0,64, VIEW_WIDTH, VIEW_HEIGHT -64);

    _readerView.tracksSymbols=NO;

    _readerView.readerDelegate =self;

    [_readerViewaddSubview:_scanView];

    //關閉閃光燈

    _readerView.torchMode =0;

    

    [self.viewaddSubview:_readerView];

    

    //掃描區域

    //readerView.scanCrop =

    

    [_readerViewstart];

    

    [selfcreateTimer];

    

}

#pragma mark -- ZBarReaderViewDelegate

-(void)readerView:(ZBarReaderView *)readerView didReadSymbols:(ZBarSymbolSet *)symbols fromImage:(UIImage *)image

{

    constzbar_symbol_t *symbol =zbar_symbol_set_first_symbol(symbols.zbarSymbolSet);

   NSString *symbolStr = [NSStringstringWithUTF8String: zbar_symbol_get_data(symbol)];

    

    //判斷是否包含 頭'http:'

    NSString *regex =@"http+:[^\\s]*";

   NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",regex];

    

    

    UIAlertView *alertView=[[UIAlertViewalloc] initWithTitle:@""message:symbolStr delegate:nilcancelButtonTitle:@"取消"otherButtonTitles:nil];

    [alertViewshow];

    

    //判斷是否包含 頭'ssid:'

    NSString *ssid =@"ssid+:[^\\s]*";;

   NSPredicate *ssidPre = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",ssid];

    

   if ([predicate evaluateWithObject:symbolStr]) {

        

    }

   else if([ssidPreevaluateWithObject:symbolStr]){

        

       NSArray *arr = [symbolStr componentsSeparatedByString:@";"];

        

        NSArray * arrInfoHead = [[arrobjectAtIndex:0]componentsSeparatedByString:@":"];

        

        NSArray * arrInfoFoot = [[arrobjectAtIndex:1]componentsSeparatedByString:@":"];

        

        

        symbolStr = [NSStringstringWithFormat:@"ssid: %@ \n password:%@",

                     [arrInfoHeadobjectAtIndex:1],[arrInfoFootobjectAtIndex:1]];

        

       UIPasteboard *pasteboard=[UIPasteboardgeneralPasteboard];

        //然後,可以使用如下代碼來把一個字符串放置到剪貼板上:

        pasteboard.string = [arrInfoFootobjectAtIndex:1];

    }

    

}



//二維碼的掃描區域

- (void)setScanView

{

    _scanView=[[UIViewalloc] initWithFrame:CGRectMake(0,0, VIEW_WIDTH,VIEW_HEIGHT-64)];

    _scanView.backgroundColor=[UIColorclearColor];

    

    //最上部view

   UIView* upView = [[UIViewalloc] initWithFrame:CGRectMake(0,0, VIEW_WIDTH,SCANVIEW_EdgeTop)];

    upView.alpha =TINTCOLOR_ALPHA;

    upView.backgroundColor = [UIColorblackColor];

    [_scanViewaddSubview:upView];

    

    //左側的view

    UIView *leftView = [[UIViewalloc] initWithFrame:CGRectMake(0,SCANVIEW_EdgeTop, SCANVIEW_EdgeLeft,VIEW_WIDTH-2*SCANVIEW_EdgeLeft)];

    leftView.alpha =TINTCOLOR_ALPHA;

    leftView.backgroundColor = [UIColorblackColor];

    [_scanViewaddSubview:leftView];

    

    /******************中間掃描區域****************************/

    UIImageView *scanCropView=[[UIImageViewalloc] initWithFrame:CGRectMake(SCANVIEW_EdgeLeft,SCANVIEW_EdgeTop, VIEW_WIDTH-2*SCANVIEW_EdgeLeft,VIEW_WIDTH-2*SCANVIEW_EdgeLeft)];

    //scanCropView.image=[UIImage imageNamed:@""];

    

    scanCropView.layer.borderColor=[UIColorgetThemeColor].CGColor;

    scanCropView.layer.borderWidth=2.0;

    

    scanCropView.backgroundColor=[UIColorclearColor];

    [_scanViewaddSubview:scanCropView];

    

    

    //右側的view

    UIView *rightView = [[UIViewalloc] initWithFrame:CGRectMake(VIEW_WIDTH-SCANVIEW_EdgeLeft,SCANVIEW_EdgeTop, SCANVIEW_EdgeLeft,VIEW_WIDTH-2*SCANVIEW_EdgeLeft)];

    rightView.alpha =TINTCOLOR_ALPHA;

    rightView.backgroundColor = [UIColorblackColor];

    [_scanViewaddSubview:rightView];

    

    

    //底部view

    UIView *downView = [[UIViewalloc] initWithFrame:CGRectMake(0,VIEW_WIDTH-2*SCANVIEW_EdgeLeft+SCANVIEW_EdgeTop,VIEW_WIDTH, VIEW_HEIGHT-(VIEW_WIDTH-2*SCANVIEW_EdgeLeft+SCANVIEW_EdgeTop)-64)];

    //downView.alpha = TINTCOLOR_ALPHA;

    downView.backgroundColor = [[UIColorblackColor] colorWithAlphaComponent:TINTCOLOR_ALPHA];

    [_scanViewaddSubview:downView];

    

    //用於說明的label

   UILabel *labIntroudction= [[UILabelalloc] init];

    labIntroudction.backgroundColor = [UIColorclearColor];

    labIntroudction.frame=CGRectMake(0,5, VIEW_WIDTH,20);

    labIntroudction.numberOfLines=1;

    labIntroudction.font=[UIFontsystemFontOfSize:15.0];

    labIntroudction.textAlignment=NSTextAlignmentCenter;

    labIntroudction.textColor=[UIColorwhiteColor];

    labIntroudction.text=@"將二維碼對準方框,即可自動掃描";

    [downViewaddSubview:labIntroudction];

    

   UIView *darkView = [[UIViewalloc] initWithFrame:CGRectMake(0, downView.frame.size.height-100.0,VIEW_WIDTH, 100.0)];

    darkView.backgroundColor = [[UIColorblackColorcolorWithAlphaComponent:DARKCOLOR_ALPHA];

    [downViewaddSubview:darkView];

    

    //用於開關燈操作的button

   UIButton *openButton=[[UIButtonalloc] initWithFrame:CGRectMake(10,20, 300.0, 40.0)];

    [openButtonsetTitle:@"開啓閃光燈" forState:UIControlStateNormal];

    [openButton setTitleColor:[UIColorwhiteColor] forState:UIControlStateNormal];

    openButton.titleLabel.textAlignment=NSTextAlignmentCenter;

    openButton.backgroundColor=[UIColorgetThemeColor];

    openButton.titleLabel.font=[UIFontsystemFontOfSize:22.0];

    [openButton addTarget:selfaction:@selector(openLight)forControlEvents:UIControlEventTouchUpInside];

    [darkViewaddSubview:openButton];

    

    //畫中間的基準線

    _QrCodeline = [[UIViewalloc] initWithFrame:CGRectMake(SCANVIEW_EdgeLeft,SCANVIEW_EdgeTop, VIEW_WIDTH-2*SCANVIEW_EdgeLeft,2)];

    _QrCodeline.backgroundColor = [UIColorgetThemeColor];

    [_scanViewaddSubview:_QrCodeline];

}

- (void)openLight

{

    if (_readerView.torchMode ==0) {

        _readerView.torchMode =1;

    }else

    {

        _readerView.torchMode =0;

    }

}

- (void)viewWillDisappear:(BOOL)animated

{

    [superviewWillDisappear:animated];

    

    if (_readerView.torchMode ==1) {

        _readerView.torchMode =0;

    }

    [selfstopTimer];

    

     [_readerViewstop];

    

}

//二維碼的橫線移動

- (void)moveUpAndDownLine

{

    CGFloat Y=_QrCodeline.frame.origin.y;

    //CGRectMake(SCANVIEW_EdgeLeft, SCANVIEW_EdgeTop, VIEW_WIDTH-2*SCANVIEW_EdgeLeft, 1)]

   if (VIEW_WIDTH-2*SCANVIEW_EdgeLeft+SCANVIEW_EdgeTop==Y){

        

        [UIView beginAnimations:@"asa" context:nil];

        [UIView setAnimationDuration:1];

        _QrCodeline.frame=CGRectMake(SCANVIEW_EdgeLeft, SCANVIEW_EdgeTop, VIEW_WIDTH-2*SCANVIEW_EdgeLeft,1);

        [UIView commitAnimations];

    }elseif(SCANVIEW_EdgeTop==Y){

        [UIView beginAnimations:@"asa" context:nil];

        [UIView setAnimationDuration:1];

        _QrCodeline.frame=CGRectMake(SCANVIEW_EdgeLeft, VIEW_WIDTH-2*SCANVIEW_EdgeLeft+SCANVIEW_EdgeTop, VIEW_WIDTH-2*SCANVIEW_EdgeLeft,1);

        [UIView commitAnimations];

    }

    

}


- (void)createTimer

{

    //創建一個時間計數

    _timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(moveUpAndDownLine) userInfo:nil repeats:YES];

}


- (void)stopTimer

{

   if ([_timer isValid] == YES) {

        [_timer invalidate];

        _timer =nil;

    }

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章