美文网首页
iOS原生二维码(生成+扫描)

iOS原生二维码(生成+扫描)

作者: 心底碎片 | 来源:发表于2017-05-23 10:16 被阅读105次

    1.需要导入

    import <CoreImage/CoreImage.h>

    import <AVFoundation/AVFoundation.h>

    两个库。
    2.生成二维码

    #pragma mark 清晰二维码
    - (void)MakeQRCode2{
        CIFilter * filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
        [filter setDefaults];
        NSString *dataString = @"http://w.url.cn/s/A6zf4SV";
        NSData * data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
        [filter setValue:data forKey:@"inputMessage"];
        CIImage * outputImage = [filter outputImage];
        ErWeiMa.image = [self creatClearImageFromCIImage:outputImage withSize:200];
    }
    - (UIImage *)creatClearImageFromCIImage:(CIImage *)image withSize:(CGFloat) size
    {
        CGRect extent = CGRectIntegral(image.extent);
        CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
        
        size_t width = CGRectGetWidth(extent) * scale;
        size_t height = CGRectGetHeight(extent) * scale;
        // 创建灰度色调空间
        CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceGray();
        CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, colorSpaceRef, (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);
        
        // 保存图片
        CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
        CGContextRelease(bitmapRef);
        CGImageRelease(bitmapImage);
        return [UIImage imageWithCGImage:scaledImage];
    }
    

    3.扫描二维码

    #pragma mark 扫描二维码
    - (void)MakeQRCode3{
        //创建拍摄会话
        Session = [[AVCaptureSession alloc] init];
        //添加输入设备
        AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
        [Session addInput:input];
        
        //添加输出数据
        AVCaptureMetadataOutput * output = [[AVCaptureMetadataOutput alloc] init];
        [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
        [Session addOutput:output];
        
        //设置输入元数据的类型
        [output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
        
        //添加扫描图层
        Layer = [AVCaptureVideoPreviewLayer layerWithSession:Session];
        Layer.frame = self.view.bounds;
        [self.view.layer addSublayer:Layer];
        
        //启动会话
        [Session startRunning];
    }
    #pragma mark - 扫描到数据就会执行该代理方法
    - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
    {
        if (metadataObjects.count > 0) {
            // 提示:如果需要对url或者文本等信息进行扫描,可以在此进行扩展
            AVMetadataMachineReadableCodeObject *object = [metadataObjects lastObject];
            NSLog(@"%@", object.stringValue);
            
            // 如果扫描完成,停止会话
            [Session stopRunning];
            
            // 删除预览图层
            [Layer removeFromSuperlayer];
        } else {
            NSLog(@"没有扫描到数据");
        }
    }
    

    相关文章

      网友评论

          本文标题:iOS原生二维码(生成+扫描)

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