美文网首页iOS 知识点iOS
ios二维码生成扫描

ios二维码生成扫描

作者: 风车大战骑士 | 来源:发表于2017-08-12 13:17 被阅读70次

    ios二维码生成

    
        //创建滤镜对象
    
        CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    
        //恢复滤镜的默认属性
    
        [filter setDefaults];
    
        NSData *infoData = [data dataUsingEncoding:NSUTF8StringEncoding];
    
        //设置filter的InputMessages数据
    
        [filter setValue:infoData forKeyPath:@"inputMessage"];
    
        CIImage *outputImage = [filter outputImage];
    
        UIImage *image = [self createUIImageFromCIImage:outputImage withWidth:width];
    
        return image;
    }
    

    根据CIImage生成指定大小的UIImage

    + (UIImage *)createUIImageFromCIImage:(CIImage *)CIImage withWidth:(CGFloat)imageWidth{
    
        CGRect extent = CGRectIntegral(CIImage.extent);
        CGFloat scale = MIN(imageWidth / CGRectGetWidth(extent), imageWidth / CGRectGetHeight(extent));
        
        //创建bitMap
        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:CIImage 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];
    }
    
    + (UIImage *)generateLogoQRCode:(NSString *)data imageWidth:(CGFloat)imageWidth logoImageName:(NSString *)logoImageName logoScaleToSuperView:(CGFloat)logoScale{
        
        CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
        //恢复滤镜的默认属性
        [filter setDefaults];
        
        NSData *infoData = [data dataUsingEncoding:NSUTF8StringEncoding];
        
        //设置filter的InputMessages数据
        [filter setValue:infoData forKeyPath:@"inputMessage"];
        CIImage *outputImage = [filter outputImage];
        
    //    outputImage = [outputImage imageByApplyingTransform:CGAffineTransformMakeScale(20, 20)];
        //将CIImage转化为UIImage类型
        UIImage *start_image = [self createUIImageFromCIImage:outputImage withWidth:imageWidth];
        
        //添加logo
        UIGraphicsBeginImageContext(start_image.size);
        [start_image drawInRect:CGRectMake(0, 0, start_image.size.width, start_image.size.height)];
        
        //画logo
        UIImage *logoImage = [UIImage imageNamed:logoImageName];
        CGFloat logoWidth = start_image.size.width * logoScale;
        CGFloat logoHeight = start_image.size.height * logoScale;
        CGFloat logoX = (start_image.size.width - logoWidth) / 2;
        CGFloat logoY = (start_image.size.width - logoHeight) / 2;
        [logoImage drawInRect:CGRectMake(logoX, logoY, logoWidth, logoHeight)];
        
        //获取最后的图片
        UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return finalImage;
    }
    

    扫描

    扫描前需要申请调用摄像头的权限

        AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        //创建输入流
        AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
        //创建输出流
        AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
        //设置代理,在主线程中刷新
        [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
        //设置扫描范围(取值范围0-1,一屏幕右上角为坐标原点)
        output.rectOfInterest = CGRectMake(0.2, 0.2, 0.6, 0.6);
        
        _session = [[AVCaptureSession alloc] init];
        //设置采集率
        [_session setSessionPreset:AVCaptureSessionPresetHigh];
        
        //添加会话输入输出
        [_session addInput:input];
        [_session addOutput:output];
        
        //设置输出数据类型,需要将元数据输出添加到会话中,才能指定元数据类型
        //设置扫码支持的编码格式
        output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN8Code,AVMetadataObjectTypeEAN13Code,AVMetadataObjectTypeCode128Code];
        
        //初始化previewLayer,传递给session将要显示什么
        _previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session];
        _previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
        _previewLayer.frame = self.view.layer.bounds;
        
        //将图层插入当前视图
        [self.view.layer insertSublayer:_previewLayer atIndex:0];
        //启动会话
        [_session startRunning];
    

    具体代码见GitHub:https://github.com/huyong668/HYQRCode

    相关文章

      网友评论

        本文标题:ios二维码生成扫描

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