美文网首页
iOS二维码的生成与扫描

iOS二维码的生成与扫描

作者: archyly | 来源:发表于2017-03-26 23:50 被阅读78次

    一、生成二维码

    1.首先要导入CoreImage框架

    #import <Corelmage/CoreImage.h>

    2.创建过滤器

    CIFilter*filter = [CIFilterfilterWithName:@"CIQRCodeGenerator"];

    3.设置过滤器为默认模式

    [filtersetDefaults];

    4.给过滤添加数据

    NSString*dataString =@"http://www.jianshu.com/u/663e6f9b7f55";

    NSData*data = [dataStringdataUsingEncoding:NSUTF8StringEncoding];

    [filtersetValue:dataforKeyPath:@"inputMessage"];

    5.获取输出的二维码

    CIImage*outputImage = [filterout putImage];

    6.显示二维码

    self.imageView.image= [self  createNonInterpolatedUIImageFormCIImage:outputImage withSize:200];

    7.为了展示清楚的二维码图片需要对outputImage做进一步处理

    - (UIImage*)createNonInterpolatedUIImageFormCIImage:(CIImage*)image withSize:(CGFloat) size

    {

    CGRectextent =CGRectIntegral(image.extent);

    CGFloatscale =MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));

    // 1.创建bitmap;

    size_twidth =CGRectGetWidth(extent) * scale;

    size_theight =CGRectGetHeight(extent) * scale;

    CGColorSpaceRefcs =CGColorSpaceCreateDeviceGray();

    CGContextRefbitmapRef =CGBitmapContextCreate(nil, width, height,8,0, cs, (CGBitmapInfo)kCGImageAlphaNone);

    CIContext*context = [CIContextcontextWithOptions:nil];

    CGImageRefbitmapImage = [contextcreateCGImage:imagefromRect:extent];

    CGContextSetInterpolationQuality(bitmapRef,kCGInterpolationNone);

    CGContextScaleCTM(bitmapRef, scale, scale);

    CGContextDrawImage(bitmapRef, extent, bitmapImage);

    // 2.保存bitmap到图片

    CGImageRefscaledImage =CGBitmapContextCreateImage(bitmapRef);

    CGContextRelease(bitmapRef);

    CGImageRelease(bitmapImage);

    return[UIImageimageWithCGImage:scaledImage];

    }

    二、扫描二维码

    1.创建捕捉会话

    AVCaptureSession*session = [[AVCaptureSessionalloc]init];

    self.session= session;

     2.添加输入设备(数据从摄像头输入)

    AVCaptureDevice*device = [AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeVideo];

    AVCaptureDeviceInput*input = [AVCaptureDeviceInputdeviceInputWithDevice:deviceerror:nil];

    [sessionaddInput:input];

     3.添加输出数据(示例对象-->类对象-->元类对象-->根元类对象)

    AVCaptureMetadataOutput*output = [[AVCaptureMetadataOutput alloc]init];

    [outputsetMetadataObjectsDelegate:selfqueue:dispatch_get_main_queue()];

    [sessionaddOutput:output];

     3.1.设置输入元数据的类型(类型是二维码数据)

    [outputsetMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];

    4.添加扫描图层

    AVCaptureVideoPreviewLayer*layer = [AVCaptureVideoPreviewLayerlayerWithSession:session];

    layer.frame=self.view.bounds;

    [self.view.layeraddSublayer:layer];

    self.layer= layer;

     5.开始扫描

    [sessionstartRunning];

    6.实现output的回调方法

    //当扫描到数据时就会执行该方法

    - (void)captureOutput:(AVCaptureOutput*)captureOutput didOutputMetadataObjects:(NSArray*)metadataObjects fromConnection:(AVCaptureConnection*)connection

    {

    if(metadataObjects.count>0) {

    AVMetadataMachineReadableCodeObject*object = [metadataObjects lastObject];

    NSLog(@"%@", object.stringValue);

    //停止扫描

    [self.sessionstopRunning];

    //将预览图层移除

    [self.layer removeFromSuperlayer];

    }else{

    NSLog(@"没有扫描到数据");

    }

    }

    相关文章

      网友评论

          本文标题:iOS二维码的生成与扫描

          本文链接:https://www.haomeiwen.com/subject/wtstmttx.html