二维码生成和识别

生成二维码

常见应用是我的二维码,然后就是一张二维码图片,展示出来,让人家扫描

先生成CIImage

/**
 *  根据字符串生成二维码 CIImage 对象
 *
 *  @param urlString 需要生成二维码的字符串
 *
 *  @return 生成的二维码
 */
+ (CIImage *)createQRcodeWithUrlString:(NSString *)urlString {
    // 1.实例化二维码滤镜
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    // 2.恢复滤镜的默认属性 (因为滤镜有可能保存上一次的属性)
    [filter setDefaults];
    // 3.将字符串转换成NSdata
    NSData *data  = [urlString dataUsingEncoding:NSUTF8StringEncoding];
    // 4.通过KVO设置滤镜, 传入data, 将来滤镜就知道要通过传入的数据生成二维码
    [filter setValue:data forKey:@"inputMessage"];
    // 5.生成二维码
    CIImage *outputImage = [filter outputImage];
    return outputImage;
}

然后合成UIImage

/**
 * 根据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];
}

合成一个对外接口

+ (UIImage *)createWithUrlString:(NSString *)urlString size:(CGFloat)size {
    if ((urlString == nil) || [urlString isEqualToString:@""]) {
        return nil;
    }
    if (size <= 0 ) {
        return nil;
    }
    CIImage *ci = [KJTQrGenerate createQRcodeWithUrlString:urlString];
    return [KJTQrGenerate createNonInterpolatedUIImageFormCIImage:ci withSize:size];
}

这里的size是指widthheight相等的正方形

识别二维码

常见的场景是长按二维码图片,识别二维码内容

+ (NSString *)stringFromImage:(UIImage *)image {
    if (image == nil) {
        return nil;
    }
    // 创建 CIDetector,并设定识别类型:CIDetectorTypeQRCode
    CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{CIDetectorAccuracy: CIDetectorAccuracyHigh}];
    // 获取识别结果
    NSArray *features = [detector featuresInImage:[CIImage imageWithCGImage:image.CGImage]];
    if (features.count == 0) {
        return nil;
    } else {
        NSString *resultString = nil;
        for (int index = 0; index < [features count]; index ++) {
            CIQRCodeFeature *feature = [features objectAtIndex:index];
            resultString = feature.messageString;
        }
        return resultString;
    }
}

参考文章

iOS 使用CIDetector扫描相册二维码、原生扫描

ios开发 根据URL或字符串生成二维码

IOS中生成二维码、扫描二维码

你可能感兴趣的:(二维码生成和识别)