欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS 二维码生成及扫码详解及实例代码

程序员文章站 2024-02-16 10:13:58
ios二维码生成及扫码      现在越来越多的应用加入二维码相关的业务,在ios开发市场上很多开发人员都在使用第三方的扫码与生成...

ios二维码生成及扫码

     现在越来越多的应用加入二维码相关的业务,在ios开发市场上很多开发人员都在使用第三方的扫码与生成二维码的控件,个人认为此类的第三方控件识别度不高。最近正好整理新框架的事情,研究了一下。具体代码如下 

生成二维码代码

/** 
 * @author 半  饱, 15-12-18 
 * 
 * @brief 生成二维码图片 
 * 
 * @param code  生成二维码图片内容 
 * @param width 二维码图片宽度 
 * @param height 二维码图片高度 
 * 
 * @return 返回uiimage对象 
 */ 
- (uiimage *)generateqrcode:(nsstring *)code 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]; 
} 

 扫描二维码代码

#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]; //监听是否触发home键挂起程序. 
  [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(didbecomeactivenotification) name:uiapplicationdidbecomeactivenotification object:nil]; //监听是否重新进入程序程序. 
} 
 
-(void)viewwilldisappear:(bool)animated { 
   [self stopscancode]; 
  [super viewwilldisappear:animated]; 
} 
 
- (void)willresignactivenotification { 
  _flashlightbtn.selected = no; 
} 
- (void)didbecomeactivenotification { 
 
} 
//加载界面上的控件,如:加上闪光灯按钮等 
- (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]; 
  //设置代理 
  [uiview setanimationdelegate:self]; 
  //设置动画执行完毕调用的事件 
  [uiview setanimationdidstopselector:@selector(didviewanimation)]; 
  _lineimageview.frame = cgrectmake(leftwith,topheight + lightheight-2,lightwidth,2); 
  [uiview commitanimations]; 
 
} 
 
-(void)didviewanimation { 
//  self.navigationcontroller 
  _lineimageview.frame = cgrectmake(leftwith, topheight, lightwidth, 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]; 
  } 
} 
//初始化layer层,绘制半透明区域 
-(void) initlayer { 
  //公共参数 
  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, lightheight/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) { 
    avmetadatamachinereadablecodeobject *metadataobj = [metadataobjects objectatindex:0]; 
    nsstring *scancoderesult; 
    if ([[metadataobj type] isequaltostring:avmetadataobjecttypeqrcode]) { 
      [self stopscancode]; 
      scancoderesult = metadataobj.stringvalue; 
      //回调信息 
      if (_delegate && [_delegate respondstoselector:@selector(scancoderesultbyviewcontroller:withscancoderesult:)]) { 
        [_delegate scancoderesultbyviewcontroller:self withscancoderesult:scancoderesult]; 
        [self.navigationcontroller popviewcontrolleranimated:yes]; 
      } 
    } else { 
      nslog(@"扫描信息错误!"); 
    } 
  } 
} 
 
- (void)systemflashlight 
{ 
#if !target_iphone_simulator 
  if([_capturedevice hastorch] && [self.capturedevice hasflash]) 
  { 
    [_capturesession 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:@"虚拟设备不能运行摄像头!"]; 
#endif 
} 
 
-(void)stopscancode { 
  [_capturesession stoprunning]; 
  _capturesession = nil; 
  _capturedevice = nil; 
  _captureinput = nil; 
  _captureoutput = nil; 
  [_capturepreview removefromsuperlayer]; 
} 
 
- (void)didreceivememorywarning { 
  [super didreceivememorywarning]; 
} 
 
@end 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!