IOS two-dimensional code generation and sweep code detailed and example code _ios

Source: Internet
Author: User

iOS two-dimensional code generation and sweep code

Now more and more applications to join the two-dimensional code-related business, in the iOS development market, many developers are using a third party sweep code and generate a two-dimensional code of the control, the personal view of such third-party control is not high degree of recognition. Recently, just finishing up the new framework of things, studied. The specific code is as follows

Generate two-dimensional Code code

/** * @author half full, 15-12-18 * * @brief generate two-dimensional code picture * * @param code generated two-dimensional code picture content * @param width Two-dimensional code picture breadth * @param height two-dimensional code picture Height * * @return return UIImage object/-(UIImage *) Generateqrcode: (NSString *) c 
  Ode width: (cgfloat) Width height: (cgfloat) Height {ciimage *qrcodeimage; 
  NSData *data = [code datausingencoding:nsisolatin1stringencoding Allowlossyconversion:false]; 
   
  Cifilter *filter = [Cifilter filterwithname:@ "Ciqrcodegenerator"]; 
  [Filter Setvalue:data forkey:@ "InputMessage"]; 
  [Filter setvalue:@ "H" forkey:@ "Inputcorrectionlevel"]; 
   
  Qrcodeimage = [Filter outputimage]; 
  CGFloat ScaleX = width/qrcodeimage.extent.size.width; 
  CGFloat ScaleY = height/qrcodeimage.extent.size.height; Ciimage *transformedimage = [Qrcodeimage Imagebyapplyingtransform:cgaffinetransformscale (CGAffineTransformIdentity 
   
  , ScaleX, ScaleY)]; 
return [UIImage Imagewithciimage:transformedimage]; } 

  Scan two-dimensional code code

#import <AVFoundation/AVFoundation.h> static const float lightwidth = 240.F; 
static const FLOAT lightheight = 240.F; 
static const FLOAT crosslinewidth = 2.F; 
 
static const FLOAT crosslineheight = 15.F; 
  @interface Bbscancodeviewcontroller () <AVCaptureMetadataOutputObjectsDelegate> {float leftwith; 
float Topheight; 
} @property (Strong, nonatomic) Avcapturedevice *capturedevice; 
@property (Strong, nonatomic) Avcapturedeviceinput *captureinput; 
@property (Strong, nonatomic) Avcapturemetadataoutput *captureoutput; 
@property (Strong, nonatomic) avcapturesession *capturesession; 
 
@property (Strong, nonatomic) Avcapturevideopreviewlayer *capturepreview; 
@property (strong,nonatomic) UIButton *flashlightbtn; 
 
@property (strong,nonatomic) Uiimageview *lineimageview; 
@end @implementation bbscancodeviewcontroller @synthesize capturedevice = _capturedevice; 
@synthesize captureinput = _captureinput; 
@synthesize captureoutput = _captureoutput; @sYnthesize Capturepreview = _capturepreview; 
@synthesize capturesession = _capturesession; 
@synthesize delegate = _delegate; 
@synthesize Isrectscan = _isrectscan; 
@synthesize Lineimageview = _lineimageview; 
@synthesize flashlightbtn = _flashlightbtn; 
  -(void) viewdidload {[Super viewdidload]; 
  Self.isshownavigationitem = YES; 
  CGRect screenrect = [UIScreen mainscreen].bounds; 
  Leftwith = (screenrect.size.width-lightwidth)/2; 
   
Topheight = (screenrect.size.height-lightheight)/2; #if! 
Target_iphone_simulator [self initscancode]; 
  #endif [self initlayer]; 
   
   
 
  [Self Initviewcontrol]; [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (willresignactivenotification) Name: Uiapplicationwillresignactivenotification Object:nil]; 
  Listens to trigger the home key suspend program. [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (didbecomeactivenotification) Name: Uiapplicationdidbecomeactivenotification Object:nil]; Monitor whether to re-enter the program.
}-(void) Viewwilldisappear: (BOOL) animated {[Self stopscancode]; 
[Super viewwilldisappear:animated]; 
}-(void) willresignactivenotification {_flashlightbtn.selected = NO; 
    }-(void) Didbecomeactivenotification {}//loading the controls on the interface, such as: plus flash buttons, etc.-(void) Initviewcontrol {@autoreleasepool { 
    _FLASHLIGHTBTN = [UIButton buttonwithtype:uibuttontypecustom]; 
    [_flashlightbtn setimage:[uiimage imagenamed:@ "Openflashlight.png"] forstate:uicontrolstatenormal]; 
    [_flashlightbtn setimage:[uiimage imagenamed:@ "Closeflashlight.png"] forstate:uicontrolstateselected]; 
    _flashlightbtn.frame = CGRectMake (Leftwith, 80.F, 30.F, 30.F); 
    [_flashlightbtn addtarget:self Action: @selector (Systemflashlight) forcontrolevents:uicontroleventtouchupinside]; 
     
    [Self.view ADDSUBVIEW:_FLASHLIGHTBTN]; 
    _lineimageview = [[Uiimageview alloc] initwithimage:nil]; 
    _lineimageview.backgroundcolor = [Uicolor Greencolor]; _lineimageview.frame = CGRectMake (Leftwith,Topheight, Lightwidth, 2); 
    [Self.view Addsubview:_lineimageview]; 
  [Self scanlineanimation]; 
  }-(void) scanlineanimation {[UIView beginanimations:nil context:nil]; 
  [UIView SETANIMATIONDURATION:4.F]; 
  Set agent [UIView setanimationdelegate:self]; 
  Set the event that the animation has finished calling [UIView setanimationdidstopselector: @selector (didviewanimation)]; 
  _lineimageview.frame = CGRectMake (leftwith,topheight + lightheight-2,lightwidth,2); 
 
[UIView commitanimations]; }-(void) didviewanimation {//Self.navigationcontroller _lineimageview.frame = CGRectMake (Leftwith, TopHeight, Li 
  Ghtwidth, 2); 
[Self scanlineanimation]; 
    }-(void) Insertlayerwithframe: (CGRect) frame withbackgroundcolor: (Uicolor *) BackgroundColor {@autoreleasepool { 
    Calayer *layer = [Calayer layer]; 
    Layer.backgroundcolor = Backgroundcolor.cgcolor; 
    Layer.frame = frame; 
  [Self.view.layer Addsublayer:layer]; }///Initialize layer layer, draw semitransparent region-(void) Initlayer {//Public parameter UicoloR *fillcolor = [Uicolor colorwithred:0xae/255.f green:0xae/255.f blue:0xae/255.f alpha:0.4]; 
  Uicolor *crosscolor = [Uicolor Greencolor]; 
  CGRect screenrect = [UIScreen mainscreen].bounds; 
  [Self insertlayerwithframe:cgrectmake (0, 0, Leftwith, screenRect.size.height) Withbackgroundcolor:fillcolor]; 
  [Self insertlayerwithframe:cgrectmake (leftwith, 0, Lightwidth, topheight) Withbackgroundcolor:fillcolor]; [Self Insertlayerwithframe:cgrectmake (leftwith + lightwidth, 0, Leftwith, screenRect.size.height) withbackgroundcolor 
  : FillColor]; [Self Insertlayerwithframe:cgrectmake (leftwith, Topheight + lightheight, Lightwidth, topheight) WithBackgroundColor: 
   
   
  FillColor]; [Self Insertlayerwithframe:cgrectmake (Leftwith, Topheight, Crosslinewidth, crosslineheight) WithBackgroundColor: 
  Crosscolor]; [Self Insertlayerwithframe:cgrectmake (Leftwith, Topheight, Crosslineheight, crosslinewidth) WithBackgroundColor: 
   
  Crosscolor]; [Self Insertlayerwithframe:cgrectmake (Leftwith + LIghtwidth-crosslineheight, Topheight, Crosslineheight, Crosslinewidth) Withbackgroundcolor:crosscolor]; [Self Insertlayerwithframe:cgrectmake (leftwith + lightwidth-crosslinewidth, Topheight, Crosslinewidth, 
   
  crosslineheight) Withbackgroundcolor:crosscolor]; [Self Insertlayerwithframe:cgrectmake (leftwith, Topheight + lightheight-crosslineheight, CrossLineWidth, 
  crosslineheight) Withbackgroundcolor:crosscolor]; [Self Insertlayerwithframe:cgrectmake (leftwith, Topheight + lightheight-crosslinewidth, Crosslineheight, 
   
  Crosslinewidth) Withbackgroundcolor:crosscolor]; [Self Insertlayerwithframe:cgrectmake (leftwith + lightwidth-crosslineheight, Topheight + lightheight-crosslinewidth, 
  Crosslineheight, Crosslinewidth) Withbackgroundcolor:crosscolor]; [Self Insertlayerwithframe:cgrectmake (leftwith + lightwidth-crosslinewidth, Topheight + lightheight-crosslineheight, 
Crosslinewidth, Crosslineheight) Withbackgroundcolor:crosscolor]; 
}-(void) Initscancode {  @autoreleasepool {_capturedevice = [avcapturedevice defaultdevicewithmediatype:avmediatypevideo]; 
    _captureinput = [Avcapturedeviceinput deviceinputwithdevice: _capturedevice Error:nil]; 
    _captureoutput = [[Avcapturemetadataoutput alloc] init]; 
    [_captureoutput setmetadataobjectsdelegate:self Queue:dispatch_get_main_queue ()]; 
      if (_isrectscan) {CGRect screenrect = [UIScreen mainscreen].bounds; [_captureoutput Setrectofinterest:cgrectmake (Topheight/screenrect.size.height, Leftwith/screenrect.size.width, Li 
    Ghtheight/screenrect.size.height, Lightwidth/screenrect.size.width)]; 
    } _capturesession = [[Avcapturesession alloc] init]; 
    [_capturesession Setsessionpreset:avcapturesessionpresethigh]; 
    if ([_capturesession canaddinput: _captureinput]) {[_capturesession addinput: _captureinput]; } if ([_capturesession canaddoutput: _captureoutput]) {[_capturesession addOutput: _captureoutput]; 
     
    } _captureoutput.metadataobjecttypes = @[avmetadataobjecttypeqrcode]; 
    _capturepreview =[avcapturevideopreviewlayer layerwithsession: _capturesession]; 
    _capturepreview.videogravity = Avlayervideogravityresizeaspectfill; 
    _capturepreview.frame = Self.view.layer.bounds; 
    [Self.view.layer Insertsublayer: _capturepreview atindex:0]; 
  [_capturesession startrunning]; }}-(void) Captureoutput: ( Avcaptureoutput *) Captureoutput didoutputmetadataobjects: ( Nsarray *) metadataobjects fromconnection: ( Avcaptureconnection *) Connection {if (metadataobjects!= nil && [metadataobjects count] > 0) {Avmeta 
    Datamachinereadablecodeobject *metadataobj = [Metadataobjects objectatindex:0]; 
    NSString *scancoderesult; 
      if ([[Metadataobj type] isequaltostring:avmetadataobjecttypeqrcode]) {[Self stopscancode]; 
      Scancoderesult = Metadataobj.stringvalue; Callback Information if (_delegate && [_delegate respondstoselector: @selector (scancoderesultbyviewcontroller:withscancoderesult:)]) {[_deleg 
        Ate scancoderesultbyviewcontroller:self Withscancoderesult:scancoderesult]; 
      [Self.navigationcontroller Popviewcontrolleranimated:yes]; } else {NSLog (@) scan information Error! 
    "); }}-(void) Systemflashlight {#if! Target_iphone_simulator if ([_capturedevice hastorch] && [Self.capturedevice Hasflash]) {[_capturesessi 
    On beginconfiguration]; 
    [_capturedevice Lockforconfiguration:nil]; 
      if (_capturedevice.torchmode = = Avcapturetorchmodeoff) {_flashlightbtn.selected = YES; 
      [_capturedevice Settorchmode:avcapturetorchmodeon]; 
    [_capturedevice Setflashmode:avcaptureflashmodeon]; 
      else {_flashlightbtn.selected = NO; 
      [_capturedevice Settorchmode:avcapturetorchmodeoff]; 
    [_capturedevice Setflashmode:avcaptureflashmodeoff]; } [_capturedevice unlockforconfIguration]; 
  [_capturesession commitconfiguration]; #else [commonutil showalert:g_alerttitle withmessage:@ virtual device cannot run the camera! 
"]; 
  #endif}-(void) Stopscancode {[_capturesession stoprunning]; 
  _capturesession = nil; 
  _capturedevice = nil; 
  _captureinput = nil; 
  _captureoutput = nil; 
[_capturepreview Removefromsuperlayer]; 
}-(void) didreceivememorywarning {[Super didreceivememorywarning]; 
 } @end

Thank you for reading, I hope to help you, thank you for your support for this site!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.