美文网首页iOS Developer
FanQRCodeScan(二维码条形码扫描生成解析)

FanQRCodeScan(二维码条形码扫描生成解析)

作者: 风清水遥 | 来源:发表于2017-04-19 11:18 被阅读202次
    一个Objective-C的github 系统自带二维码条形码的扫描和生成及图片解析二维码(上线App Store的开源项目)

    FanQRCodeScan(iOS8+)

    • 可以定制扫描框和字体图标的颜色
    • 可以定制生成二维码的前景色和背景色哦
    • 另外还支持横竖屏切换哦,是不是很激爽
    • 其实还是支持中文,英文,繁体中文的哦

    Demo功能

    • 支持创建各种二维码
    • 3D-Touch的icon及图片和列表按压支持
    • 随时随地的系统分享功能
    • 每次生成扫描都记录,随时删除单个或全部
    • storyboard的界面方式,可供新手学习,autolayout和旧版UIViewAutoresizing的约束方法
    • 除了FMDB,没有接入任何第三方库。

    App store :韩QRCode 上线预览图如下:

    创建二维码
    扫一扫
    3D-Touch
    我的二维码

    功能介绍

    1.二维码条形码扫描 (一个block搞定是不是很酸爽)
    FanQRCodeScanViewController *qrCoreVC=[[FanQRCodeScanViewController alloc]initWithQRBlock:^(NSString *resultSrt,NSString *type, BOOL isSuccess) {
            if (isSuccess) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self fan_showAlertWithTitle:@"扫描结果" message:resultSrt];
                });
                NSLog(@"%@",resultSrt);
            }else{
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self fan_showAlertWithTitle:@"扫描失败" message:resultSrt];
                });
                NSLog(@"关闭或失败:%@",resultSrt);
            }
            
        }];
        qrCoreVC.themColor=[UIColor redColor];
        qrCoreVC.scanColor=[UIColor yellowColor];
        [self presentViewController:qrCoreVC animated:YES completion:nil];
    
    2.二维码生成
    //二维码调用
    UIImage *image=[FanQRCodeScanViewController fan_qrCodeImageWithText:textField.text size:CGSizeMake(300, 300)];
    
    #pragma mark - 二维码生成
    +(UIImage *)fan_qrCodeImageWithText:(NSString *)text size:(CGSize)size{
        return [[self class] fan_qrCodeImageWithText:text size:size color:[UIColor blackColor] bgColor:[UIColor whiteColor]];
    }
    +(UIImage *)fan_qrCodeImageWithText:(NSString *)text size:(CGSize)size color:(UIColor *)color bgColor:(UIColor *)bgColor{
        NSData *stringData = [text dataUsingEncoding: NSUTF8StringEncoding];
        
        //生成
        CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
        [qrFilter setValue:stringData forKey:@"inputMessage"];
        [qrFilter setValue:@"M" forKey:@"inputCorrectionLevel"];
        
        UIColor *onColor = color;
        UIColor *offColor = bgColor;
        //上色
        CIFilter *colorFilter = [CIFilter filterWithName:@"CIFalseColor"
                                           keysAndValues:
                                 @"inputImage",qrFilter.outputImage,
                                 @"inputColor0",[CIColor colorWithCGColor:onColor.CGColor],
                                 @"inputColor1",[CIColor colorWithCGColor:offColor.CGColor],
                                 nil];
        
        CIImage *qrImage = colorFilter.outputImage;
        
        //绘制
        CGImageRef cgImage = [[CIContext contextWithOptions:nil] createCGImage:qrImage fromRect:qrImage.extent];
        UIGraphicsBeginImageContext(size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetInterpolationQuality(context, kCGInterpolationNone);
        CGContextScaleCTM(context, 1.0, -1.0);
        CGContextDrawImage(context, CGContextGetClipBoundingBox(context), cgImage);
        if (cgImage==nil) {
            return nil;
        }
        UIImage *codeImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        CGImageRelease(cgImage);
        return codeImage;
    }
    
    
    
    3.条形码生成
    //条形码调用
    UIImage *image=[FanQRCodeScanViewController fan_generateBarImageWithCode:textField.text size:CGSizeMake(300, 160)];
    
    //条形码生成
    + (UIImage *)fan_generateBarImageWithCode:(NSString *)code size:(CGSize)size{
        //生成条形码
        CIImage *barcodeImage;
        //NSISOLatin1StringEncoding
        NSData *data = [code dataUsingEncoding:NSISOLatin1StringEncoding allowLossyConversion:false];
        CIFilter *filter = [CIFilter filterWithName:@"CICode128BarcodeGenerator"];
        
        [filter setValue:data forKey:@"inputMessage"];
        
        barcodeImage = [filter outputImage];
        
        //消除模糊(此种方法,得到的图片不能保存到相册)
    //    CGFloat scaleX = size.width / barcodeImage.extent.size.width;// extent 返回图片的frame
    //    CGFloat scaleY = size.height / barcodeImage.extent.size.height;
    //    CIImage *transformedImage = [barcodeImage imageByApplyingTransform:CGAffineTransformScale(CGAffineTransformIdentity, scaleX, scaleY)];
    //    return [UIImage imageWithCIImage:transformedImage];
        
        //用绘制方法(可以保存到相册)不知道什么原因
        CGImageRef cgImage = [[CIContext contextWithOptions:nil] createCGImage:barcodeImage fromRect:barcodeImage.extent];
        UIGraphicsBeginImageContext(size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetInterpolationQuality(context, kCGInterpolationNone);
        CGContextScaleCTM(context, 1.0, -1.0);
        CGContextDrawImage(context, CGContextGetClipBoundingBox(context), cgImage);
        if (cgImage==nil) {
            return nil;
        }
        UIImage *codeImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        CGImageRelease(cgImage);
        return codeImage;
    
    }
    
    

    Like(喜欢)

    有问题请直接在文章下面留言,喜欢就给个Star(小星星)吧!

    Email:fqsyfan@gmail.com

    github:https://github.com/fanxiangyang/FanQRCodeScan

    相关文章

      网友评论

        本文标题:FanQRCodeScan(二维码条形码扫描生成解析)

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