• iOS开发——生成条形码,二维码


    - (void)viewDidLoad {

        [super viewDidLoad];

        

        self.imageView.image = [self generateBarCode:@"1524829417" 60 height:60];

        

        self.imageView2.image = [self generateQRCode:@"1524829417" 60 height:60];

    }

    - (UIImage *)generateBarCode:(NSString *)code (CGFloat)width height:(CGFloat)height {

        // 生成条形码图片

        CIImage *barcodeImage;

        NSData *data = [code dataUsingEncoding:NSISOLatin1StringEncoding allowLossyConversion:false];

        CIFilter *filter = [CIFilter filterWithName:@"CICode128BarcodeGenerator"];

        

        [filter setValue:data forKey:@"inputMessage"];

        barcodeImage = [filter outputImage];

        

        // 消除模糊

        CGFloat scaleX = width / barcodeImage.extent.size.width; // extent 返回图片的frame

        CGFloat scaleY = height / barcodeImage.extent.size.height;

        CIImage *transformedImage = [barcodeImage imageByApplyingTransform:CGAffineTransformScale(CGAffineTransformIdentity, scaleX, scaleY)];

        

        return [UIImage imageWithCIImage:transformedImage];

    }

    - (UIImage *)generateQRCode:(NSString *)code (CGFloat)width height:(CGFloat)height {

        

        // 生成二维码图片

        CIImage *qrcodeImage;

        NSData *data = [code dataUsingEncoding:NSISOLatin1StringEncoding allowLossyConversion:false];

        CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];

        

        [filter setValue:data forKey:@"inputMessage"];

        [filter setValue:@"H" forKey:@"inputCorrectionLevel"];

        qrcodeImage = [filter outputImage];

        

        // 消除模糊

        CGFloat scaleX = width / qrcodeImage.extent.size.width; // extent 返回图片的frame

        CGFloat scaleY = height / qrcodeImage.extent.size.height;

        CIImage *transformedImage = [qrcodeImage imageByApplyingTransform:CGAffineTransformScale(CGAffineTransformIdentity, scaleX, scaleY)];

        

        return [UIImage imageWithCIImage:transformedImage];

    }

  • 相关阅读:
    1.Spring MVC详解
    servlet的九大内置对象
    Hibernate设置事务的隔离级别
    wamp下php报错session_start(): open(d:/wamp/tmpsess_ku776hvb06ko4lv9d11e7mnfj1, O_RDWR) failed: No such file or directory
    json_decode()相关报错
    wamp下var_dump()相关问题
    es6箭头函数内部判断
    Json数组对象取值
    npm指向淘宝源
    APICloud之封装webApp
  • 原文地址:https://www.cnblogs.com/yyt-hehe-yyt/p/4801555.html
Copyright © 2020-2023  润新知