识别以及生成二维码(长按&扫面方式)

方式一:长按识别二维码

#pragma mark 长按识别图片中识别二维码
-(void)identifyQRimage{
    UIImageView *imageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"face1.png"]];
    imageV.contentMode = UIViewContentModeScaleAspectFit;
    imageV.userInteractionEnabled = YES;
    imageV.frame = CGRectMake(0, 100, self.view.frame.size.width, 200);
    [self.view addSubview:imageV];
    
    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longpressAction:)];
    [imageV addGestureRecognizer:longPressGesture];
}

- (void)longpressAction:(UILongPressGestureRecognizer*)sender{
    
    UIImageView *iv = (UIImageView*)sender.view;
    // 创建扫描器
    CIDetector *QRdector = [CIDetector detectorOfType:CIDetectorTypeFace context:nil options:@{CIDetectorAccuracy:CIDetectorAccuracyHigh}];
    
    CIImage *ciImage = [[CIImage alloc] initWithImage:iv.image];
    
   // CIFeature 是父类,二维码具体使用其子类
    NSArray *features =  [QRdector featuresInImage:ciImage];
    if (features.count <= 0)  return;

    for (CIFeature *feature in features) {
        if (feature.type == CIFeatureTypeQRCode) {
            CIQRCodeFeature *QRFeature = (CIQRCodeFeature*)feature;
            NSLog(@"%@",QRFeature.messageString);//
        }
    }
}

方式二: 扫一扫识别二维码

#pragma mark 扫一扫识别二维码
- (void)scanQR{
    self.session = [[AVCaptureSession alloc] init];
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    NSError *error = nil;
    AVCaptureDeviceInput *input = [[AVCaptureDeviceInput alloc] initWithDevice:device error:&error];
    [self.session addInput:input];
    
    if (error) {
        return;
    }

    AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
    [self.session addOutput:output];
    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    [output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
    self.layer = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
    self.layer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    self.layer.bounds = CGRectMake(0, 0, 100, 100);
    self.layer.position = self.view.center;
    [self.view.layer addSublayer:self.layer];
    [self.session startRunning];
}

- (void)captureOutput:(AVCaptureOutput *)output didOutputMetadataObjects:(NSArray<__kindof AVMetadataObject *> *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
    if (metadataObjects.count <= 0) return;
    AVMetadataMachineReadableCodeObject *object = metadataObjects.lastObject;
    NSLog(@"%@",object.stringValue);
    [self.layer removeFromSuperlayer];
    [self.session stopRunning];
    
}


生成二维码

- (void)createQR{
    // 设置数据
    NSString *info = @"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=1273941293";
    NSData *data = [info dataUsingEncoding:NSISOLatin1StringEncoding];
    // 创建滤镜对象
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    // 恢复滤镜默认设置
    [filter setValue:data forKey:@"inputMessage"];
    CIImage *outputImg = filter.outputImage;
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef imgageRef = [context createCGImage:outputImg fromRect:outputImg.extent];
    UIImage *qrImage = [UIImage imageWithCGImage:imgageRef];
    UIImageView *imgaev = [[UIImageView alloc] initWithImage:qrImage];
    imgaev.frame = CGRectMake(0, 0, 100, 100);
    imgaev.contentMode = UIViewContentModeScaleAspectFit;
    [self.view addSubview:imgaev];
    UIImageWriteToSavedPhotosAlbum(qrImage, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);
    CGImageRelease(imgageRef);
   
}

- (void)imageSavedToPhotosAlbum:(UIImage*)image didFinishSavingWithError:  (NSError*)error contextInfo:(id)contextInfo{
    if(error) NSLog(@"%@",error);
}




你可能感兴趣的:(iOS,开发)