////YVANQRCODEVIEWCONTROLLER.M//Zbar////Created by City--online on 15/6/8.//Copyright (c) 2015 CYW. All rights reserved.//#import "YvanQRCodeViewController.h"#import "ZBarSDK.h"#defineScanview_edgetop 40.0#defineScanview_edgeleft 50.0#defineTintcolor_alpha 0.2//Light Transparency#defineDarkcolor_alpha 0.5//Dark Transparency#defineView_width Self.view.bounds.size.width#defineView_height Self.view.bounds.size.height@interfaceYvanqrcodeviewcontroller () <ZBarReaderViewDelegate>{UIView*_qrcodeline; Nstimer*_timer; //Set Scan screenUIView *_scanview; Zbarreaderview*_readerview;}@end@implementationYvanqrcodeviewcontroller- (void) viewdidload {[Super viewdidload]; Self.title=@"Scan Two-dimensional code" ; //initializing the Scan interface[self setscanview]; _readerview=[[Zbarreaderview alloc] init]; _readerview.frame= CGRectMake (0,0, View_width, view_height); _readerview.trackssymbols=NO; _readerview.readerdelegate=Self ; [_readerview addsubview: _scanview]; //turn off the flash_readerview.torchmode =0 ; [Self.view addsubview: _readerview]; //Scan Area//Readerview.scancrop =[_readerview start]; [Self createtimer]; }#pragmaMark--zbarreaderviewdelegate-(void) Readerview: ( Zbarreaderview *) Readerview Didreadsymbols: ( Zbarsymbolset *) symbols FromImage: ( UIImage *) image{Constzbar_symbol_t *symbol =zbar_symbol_set_first_symbol (symbols. Zbarsymbolset); NSString*symbolstr =[NSString stringwithutf8string:zbar_symbol_get_data (symbol)]; //determine if the header ' http: ' is includedNSString *regex =@"http+:[^//s]*" ; Nspredicate*predicate = [Nspredicate predicatewithformat:@"Self MATCHES%@", regex]; Uialertview*alertview=[[Uialertview Alloc] Initwithtitle:@""Message:symbolstrDelegate: Nil Cancelbuttontitle:@"Cancel"Otherbuttontitles:nil]; [Alertview show]; //determine if the header ' SSID ' is included: 'NSString *ssid =@"ssid+:[^//s]*" ; Nspredicate*ssidpre = [Nspredicate predicatewithformat:@"Self MATCHES%@", SSID]; if([predicate evaluatewithobject:symbolstr]) {}Else if([Ssidpre evaluatewithobject:symbolstr]) {Nsarray*arr = [Symbolstr componentsseparatedbystring:@";" ]; Nsarray* Arrinfohead = [arr objectatindex:0] Componentsseparatedbystring:@":" ]; Nsarray* Arrinfofoot = [arr objectatindex:1] Componentsseparatedbystring:@":" ]; Symbolstr= [NSString stringWithFormat:@"SSID:%@/n password:%@", [arrinfohead objectatindex:1],[arrinfofoot Objectatindex:1 ]]; Uipasteboard*pasteboard=[Uipasteboard Generalpasteboard]; //you can then use the following code to place a string on the Clipboard:Pasteboard.string= [Arrinfofoot Objectatindex:1 ]; }}- (void) setscanview{_scanview=[[UIView Alloc] Initwithframe:cgrectmake (0,0, View_width, View_height)]; _scanview. BackgroundColor=[Uicolor Clearcolor]; //Top ViewUIView * Upview = [[UIView alloc] Initwithframe:cgrectmake (0,0, View_width, Scanview_edgetop)]; Upview. Alpha=Tintcolor_alpha; Upview. BackgroundColor=[Uicolor Blackcolor]; [_scanview Addsubview:upview]; //view on the leftUIView *leftview = [[UIView alloc] Initwithframe:cgrectmake (0, Scanview_edgetop, Scanview_edgeleft, View_width-2*Scanview_edgeleft)]; Leftview. Alpha=Tintcolor_alpha; Leftview. BackgroundColor=[Uicolor Blackcolor]; [_scanview Addsubview:leftview]; /****************** mid-scan area ****************************/Uiimageview*scancropview=[[Uiimageview alloc] Initwithframe:cgrectmake (Scanview_edgeleft, Scanview_edgetop, VIEW_WIDTH-2* Scanview_edgeleft, View_width-2*Scanview_edgeleft)]; //scancropview.image=[uiimage imagenamed:@ ""];Scancropview. Layer. BorderColor =[Uicolor Yellowcolor]. Cgcolor; Scancropview. Layer. BorderWidth=2.0 ; Scancropview. BackgroundColor=[Uicolor Clearcolor]; [_scanview Addsubview:scancropview]; //View on rightUIView *rightview = [[UIView alloc] Initwithframe:cgrectmake (View_width-scanview_edgeleft, Scanview_edgetop, SCA Nview_edgeleft, View_width-2*Scanview_edgeleft)]; Rightview. Alpha=Tintcolor_alpha; Rightview. BackgroundColor=[Uicolor Blackcolor]; [_scanview Addsubview:rightview]; //Bottom ViewUIView *downview = [[UIView alloc] Initwithframe:cgrectmake (0, View_width-2* Scanview_edgeleft + scanview_edgetop, View_width, View_height-(View_width-2* Scanview_edgeleft + scanview_edgetop)- - )]; //downview.alpha = Tintcolor_alpha;Downview. BackgroundColor =[[Uicolor Blackcolor] colorwithalphacomponent:tintcolor_alpha]; [_scanview addsubview:d Ownview]; //label for the descriptionUILabel *labintroudction=[[UILabel alloc] init]; Labintroudction. BackgroundColor=[Uicolor Clearcolor]; Labintroudction. Frame= CGRectMake (0,5, View_width, - ); Labintroudction. NumberOfLines=1 ; Labintroudction. Font=[Uifont systemfontofsize:15.0 ]; Labintroudction. TextAlignment=Nstextalignmentcenter; Labintroudction. TextColor=[Uicolor Whitecolor]; Labintroudction. Text=@"the QR Code is aligned to the box to automatically scan" ; [Downview addsubview:labintroudction]; UIView*darkview = [[UIView alloc] Initwithframe:cgrectmake (0, Downview. Frame. Size. Height-100.0, View_width,100.0 )]; Darkview. BackgroundColor=[[Uicolor Blackcolor] colorwithalphacomponent:darkcolor_alpha]; [Downview addsubview:d Arkview]; //button for switching light operationUIButton *openbutton=[[UIButton alloc] Initwithframe:cgrectmake (Ten, -,300.0,40.0 )]; [Openbutton settitle:@"turn on the flash"Forstate:uicontrolstatenormal]; [Openbutton settitlecolor: [Uicolor Whitecolor] forstate:uicontrolstatenormal]; Openbutton. Titlelabel. TextAlignment=Nstextalignmentcenter; Openbutton. BackgroundColor=[Uicolor Greencolor]; Openbutton. Titlelabel. Font=[Uifont systemfontofsize:22.0 ]; [Openbutton addtarget:self Action: @selector (Openlight) forcontrolevents:uicontroleventtouchupinside]; [Darkview Addsubview:openbutton]; //draw the middle of the baseline_qrcodeline = [[UIView alloc] Initwithframe:cgrectmake (Scanview_edgeleft, Scanview_edgetop, View_width-2* Scanview_edgeleft,2 )]; _qrcodeline. BackgroundColor=[Uicolor Yellowcolor]; [_scanview addsubview: _qrcodeline];}- (void) openlight{if(_readerview.torchmode = =0) {_readerview.torchmode=1 ; } Else{_readerview.torchmode=0 ; }}- (void) Viewwilldisappear: ( BOOL) animated{[Super viewwilldisappear:animated]; if(_readerview. Torchmode = =1) {_readerview. Torchmode=0 ; } [self Stoptimer]; [_readerview stop];}//horizontal line movement of two-dimensional code- (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==x) {[UIView beginanimations:@"ASA"Context:nil]; [UIView setanimationduration:1 ]; _qrcodeline.frame=cgrectmake (Scanview_edgeleft, Scanview_edgetop, view_width-2*scanview_edgeleft,1 ); [UIView commitanimations]; } Else if(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{//Create a time count_timer=[nstimer Scheduledtimerwithtimeinterval:1.0target:self selector: @selector (moveupanddownline) Userinfo:nil repeats:yes];}- (void) stoptimer{if([_timer isValid] = =YES) {[_timer invalidate]; _timer=Nil; }}//whether to support the turn screen-(BOOL) shouldautorotate{returnYES;}//supported screen orientations, where you can return directly to the Uiinterfaceorientationmask type//You can also return multiple uiinterfaceorientationmask values after the fetch or operation-(Nsuinteger) supportedinterfaceorientations{returnuiinterfaceorientationmaskportrait;}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}/*#pragma mark-navigation//in a storyboard-based application, you'll often want to do a little preparation before n avigation-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) Sender {//Get the new view controller using [s] Egue Destinationviewcontroller]. Pass the selected object to the new view controller.}*/@end
Zbar's custom QR code scan