zoukankan      html  css  js  c++  java
  • iOS 二维码的生成和扫描

    属性

    @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(@"没有扫描到数据");
        }
    }
    
  • 相关阅读:
    FullCalendar日历插件说明文档
    Git忽略规则及.gitignore规则不生效的解决办法
    学习git config配置文件
    拼接最长回文串
    Gildong owns a bulgogi restaurant
    前m大的数
    求水洼(dfs)
    循环数组的最大子段和
    求叶子节点
    周期
  • 原文地址:https://www.cnblogs.com/menglingxu/p/6478970.html
Copyright © 2011-2022 走看看