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

iOS二维码的生成和扫描

程序员文章站 2023-12-20 20:45:28
本文实例为大家分享了android九宫格图片展示的具体代码,供大家参考,具体内容如下 属性 @property (strong,nonatomic)avcapt...

本文实例为大家分享了android九宫格图片展示的具体代码,供大家参考,具体内容如下

属性

@property (strong,nonatomic)avcapturedevice * device;
@property (strong,nonatomic)avcapturedeviceinput * input;
@property (strong,nonatomic)avcapturemetadataoutput * output;
@property (strong,nonatomic)avcapturesession * session;
@property (strong,nonatomic)avcapturevideopreviewlayer * layer; 
@property (nonatomic, strong)uiimageview *imageview;

二维码的生成

// 1.创建过滤器
  cifilter *filter = [cifilter filterwithname:@"ciqrcodegenerator"];
 
  // 2.恢复默认
  [filter setdefaults];
 
  // 3.给过滤器添加数据(正则表达式/账号和密码)
  nsstring *datastring = @"http://www.520it.com";
  nsdata *data = [datastring datausingencoding:nsutf8stringencoding];
  [filter setvalue:data forkeypath:@"inputmessage"];
 
  // 4.获取输出的二维码
  ciimage *outputimage = [filter outputimage];
 
  //因为生成的二维码模糊,所以通过createnoninterpolateduiimageformciimage:outputimage来获得高清的二维码图片
 
  // 5.显示二维码
  self.imageview.image = [self createnoninterpolateduiimageformciimage:outputimage withsize:200];

 * createnoninterpolateduiimageformciimage:outputimage方法的实现

/**
 * 根据ciimage生成指定大小的uiimage
 *
 * @param image ciimage
 * @param size 图片宽度
 */
- (uiimage *)createnoninterpolateduiimageformciimage:(ciimage *)image withsize:(cgfloat) size
{
  cgrect extent = cgrectintegral(image.extent);
  cgfloat scale = min(size/cgrectgetwidth(extent), size/cgrectgetheight(extent));
 
  // 1.创建bitmap;
  size_t width = cgrectgetwidth(extent) * scale;
  size_t height = cgrectgetheight(extent) * scale;
  cgcolorspaceref cs = cgcolorspacecreatedevicegray();
  cgcontextref bitmapref = cgbitmapcontextcreate(nil, width, height, 8, 0, cs, (cgbitmapinfo)kcgimagealphanone);
  cicontext *context = [cicontext contextwithoptions:nil];
  cgimageref bitmapimage = [context createcgimage:image fromrect:extent];
  cgcontextsetinterpolationquality(bitmapref, kcginterpolationnone);
  cgcontextscalectm(bitmapref, scale, scale);
  cgcontextdrawimage(bitmapref, extent, bitmapimage);
 
  // 2.保存bitmap到图片
  cgimageref scaledimage = cgbitmapcontextcreateimage(bitmapref);
  cgcontextrelease(bitmapref);
  cgimagerelease(bitmapimage);
  return [uiimage imagewithcgimage:scaledimage];
}

 二维码的扫描

// 1.创建捕捉会话
  avcapturesession *session = [[avcapturesession alloc] init];
  self.session = session;
 
  // 2.添加输入设备(数据从摄像头输入)
  avcapturedevice *device = [avcapturedevice defaultdevicewithmediatype:avmediatypevideo];
  avcapturedeviceinput *input = [avcapturedeviceinput deviceinputwithdevice:device error:nil];
  [session addinput:input];
 
  // 3.添加输出数据(示例对象-->类对象-->元类对象-->根元类对象)
  avcapturemetadataoutput *output = [[avcapturemetadataoutput alloc] init];
  [output setmetadataobjectsdelegate:self queue:dispatch_get_main_queue()];
  [session addoutput:output];
 
  // 3.1.设置输入元数据的类型(类型是二维码数据)
  [output setmetadataobjecttypes:@[avmetadataobjecttypeqrcode]];
 
  // 4.添加扫描图层
  avcapturevideopreviewlayer *layer = [avcapturevideopreviewlayer layerwithsession:session];
  layer.frame = self.view.bounds;
  [self.view.layer addsublayer:layer];
  self.layer = layer;
 
  // 5.开始扫描
  [session startrunning];

 *扫描到结果后会调用的方法

// 当扫描到数据时就会执行该方法
- (void)captureoutput:(avcaptureoutput *)captureoutput didoutputmetadataobjects:(nsarray *)metadataobjects fromconnection:(avcaptureconnection *)connection
{
  if (metadataobjects.count > 0) {
    //获得扫描数据,最后一个时最新扫描的数据
    avmetadatamachinereadablecodeobject *object = [metadataobjects lastobject];
    nslog(@"%@", object.stringvalue);
 
    // 停止扫描
    [self.session stoprunning];
 
    // 将预览图层移除
    [self.layer removefromsuperlayer];
  } else {
    nslog(@"没有扫描到数据");
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: