iOS图形处理相关ios二维码

技术分享之二维码开发

2016-06-12  本文已影响1129人  coderwhy

二维码的介绍

二维码的生成

#pragma mark - 生成二维码
- (IBAction)generateQRCode {
    // 1.创建滤镜
    CIFilter * filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];

    // 2.设置滤镜的输入内容
    // 2.1.取出需要设置的内容
    NSString *inputContent = self.textField.text;

    // 2.2.设置内容
    NSData *inputData = [inputContent dataUsingEncoding:NSUTF8StringEncoding];
    [filter setValue:inputData forKey:@"inputMessage"];

    // 3.获取输出的图片
    CIImage *outputImage = filter.outputImage;

    // 4.放大图片
    CGAffineTransform scale = CGAffineTransformMakeScale(10, 10);
    outputImage = [outputImage imageByApplyingTransform:scale];

    // 5.显示图片
    self.imageView.image = [UIImage imageWithCIImage:outputImage];
}
#pragma mark - 添加前景图片
- (UIImage *)appendForegroundImageWithOriginal:(UIImage *)originalImage foreground:(UIImage *)foregroundImage {
    // 1.获取原始图片的尺寸
    CGSize originalSize = originalImage.size;

    // 2.获取图片上下文
    UIGraphicsBeginImageContext(originalSize);

    // 3.将大图绘制到上下文中
    [originalImage drawInRect:CGRectMake(0, 0, originalSize.width, originalSize.height)];

    // 4.将小图绘制到上下文中
    CGFloat width = 80;
    CGFloat height = 80;
    CGFloat x = (originalSize.width - 80) * 0.5;
    CGFloat y = (originalSize.height - 80) * 0.5;
    [foregroundImage drawInRect:CGRectMake(x, y, width, height)];

    // 5.获取上下文中的图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

    // 6.关闭上下文
    UIGraphicsEndImageContext();

    return newImage;
}

识别图片中的二维码

#pragma mark - 识别图形中的二维码
- (IBAction)detectorQRCode {
    // 1.创建过滤器
    CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:nil];

    // 2.创建CIImage对象
    CIImage *imageCI = [[CIImage alloc] initWithImage:self.imageView.image];

    // 3.获取识别到的所有结果
    NSArray *features = [detector featuresInImage:imageCI];

    // 4.遍历所有的结果
    NSMutableString *strM = [NSMutableString string];
    for (CIQRCodeFeature *f in features) {
        [strM appendString:f.messageString];
    }

    // 5.弹出弹窗显示结果
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"识别的信息" message:strM preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:action];
    [self presentViewController:alert animated:true completion:nil];
}

扫描二维码

#pragma mark - 开始扫描
- (void)startScanning {
    // 1.创建输入设备(摄像头)
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];

    // 2.创建输入方式(Metadata)
    AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];

    // 3.创建会话,将输入和输出联系起来
    AVCaptureSession *session = [[AVCaptureSession alloc] init];
    [session addInput:input];
    [session addOutput:output];
    [output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];

    // 4.创建会话图层
    AVCaptureVideoPreviewLayer *layer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
    layer.frame = self.view.bounds;
    [self.view.layer insertSublayer:layer atIndex:0];
    self.layer = layer;

    // 5.开始扫描
    [session startRunning];

    // 6.设置扫描的区域
    CGSize size = [UIScreen mainScreen].bounds.size;
    CGFloat x = self.qrCodeView.frame.origin.y / size.height;
    CGFloat y = self.qrCodeView.frame.origin.x / size.width;
    CGFloat w = self.qrCodeView.frame.size.height / size.height;
    CGFloat h = self.qrCodeView.frame.size.width / size.width;
    output.rectOfInterest = CGRectMake(x, y, w, h);
}

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

    // 0.移除之前的绘制
    for (CAShapeLayer *layer in self.layers) {
        [layer removeFromSuperlayer];
    }

    // 1.获取扫描结果
    NSMutableString *resultMStr = [NSMutableString string];
    for (AVMetadataMachineReadableCodeObject *result in metadataObjects) {
        // 1.1.获取扫描到的字符串
        [resultMStr appendString:result.stringValue];

        // 1.2.绘制扫描到内容的边框
        [self drawEdgeBorder:result];
    }

    // 2.显示结果
    NSString *string = [resultMStr isEqualToString:@""] ? @"请将二维码放入输入框中" : resultMStr;
    self.resultLabel.text = string;
}

- (void)drawEdgeBorder:(AVMetadataMachineReadableCodeObject *)resultObjc {
    // 0.转化object
    resultObjc = (AVMetadataMachineReadableCodeObject *)[self.layer transformedMetadataObjectForMetadataObject:resultObjc];

    // 1.创建绘制的图层
    CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];

    // 2.设置图层的属性
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    shapeLayer.strokeColor = [UIColor blueColor].CGColor;
    shapeLayer.lineWidth = 5;

    // 3.创建贝塞尔曲线
    // 3.1.创建贝塞尔曲线
    UIBezierPath *path = [[UIBezierPath alloc] init];

    // 3.2.将path移动到起始位置
    int index = 0;
    for (id dict in resultObjc.corners) {
        // 3.2.1.获取点
        CGPoint point = CGPointZero;
        CGPointMakeWithDictionaryRepresentation((CFDictionaryRef)dict, &point);
        // NSLog(@"%@", NSStringFromCGPoint(point));
        // 3.2.2.判断如何使用该点
        if (index == 0) {
            [path moveToPoint:point];
        } else {
            [path addLineToPoint:point];
        }

        // 3.2.3.下标值自动加1
        index++;
    }

    // 3.3.关闭路径
    [path closePath];

    // 4.画出路径
    shapeLayer.path = path.CGPath;

    // 5.将layer添加到图册中
    [self.view.layer addSublayer:shapeLayer];

    // 6.添加到数组中
    [_layers addObject:shapeLayer];
}

- (NSMutableArray *)layers {
    if (_layers == nil) {
        _layers = [NSMutableArray array];
    }
    return _layers;
}

总结

上一篇下一篇

猜你喜欢

热点阅读