zoukankan      html  css  js  c++  java
  • 使用iOS-QR-Code-Encoder 生成二维码

    一:所需类库

         iOS-QR-Code-Encoder

      官网主页:https://github.com/moqod/iOS-QR-Code-Encoder

        导入:QuartzCore.framework

    二:使用流程

    1. 导入libqrencode文件夹里的所有文件
    2. 在使用的地方 导入 

      #import "qrencode.h"

    3. 添加如下生成代码:
    //生成二维码:
    
    
    
    - (UIImage *)qrImageForString:(NSString *)string imageSize:(CGFloat)size {
        if (![string length]) {
            return nil;
        }
        
        // generate QR
        QRcode *code = QRcode_encodeString([string UTF8String], 0, QR_ECLEVEL_L, QR_MODE_8, 1);
        if (!code) {
            return nil;
        }
        
        if (code->width > size) {
            printf("Image size is less than qr code size (%d)
    ", code->width);
            return nil;
        }
        
        // create context
        
        CGBitmapInfo bitmapInfo = (CGBitmapInfo) kCGImageAlphaPremultipliedLast;
        
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        CGContextRef ctx = CGBitmapContextCreate(0, size, size, 8, size * 4, colorSpace, bitmapInfo);
        
        CGAffineTransform translateTransform = CGAffineTransformMakeTranslation(0, -size);
        CGAffineTransform scaleTransform = CGAffineTransformMakeScale(1, -1);
        CGContextConcatCTM(ctx, CGAffineTransformConcat(translateTransform, scaleTransform));
        
        // draw QR on this context
        [self drawQRCode:code context:ctx size:size];
        
        // get image
        CGImageRef qrCGImage = CGBitmapContextCreateImage(ctx);
        UIImage * qrImage = [UIImage imageWithCGImage:qrCGImage];
        
        // free memory
        CGContextRelease(ctx);
        CGImageRelease(qrCGImage);
        CGColorSpaceRelease(colorSpace);
        QRcode_free(code);
        
        return qrImage;
    }
    
    - (void)drawQRCode:(QRcode *)code context:(CGContextRef)ctx size:(CGFloat)size {
        int margin = 0;
        unsigned char *data = code->data;
        int width = code->width;
        int totalWidth = width + margin * 2;
        int imageSize = (int)floorf(size);
        
        // @todo - review float->int stuff
        int pixelSize = imageSize / totalWidth;
        if (imageSize % totalWidth) {
            pixelSize = imageSize / width;
            margin = (imageSize - width * pixelSize) / 2;
        }
        
        CGRect rectDraw = CGRectMake(0.0f, 0.0f, pixelSize, pixelSize);
        // draw
        CGContextSetFillColor(ctx, CGColorGetComponents([UIColor blackColor].CGColor));
        for(int i = 0; i < width; ++i) {
            for(int j = 0; j < width; ++j) {
                if(*data & 1) {
                    rectDraw.origin = CGPointMake(margin + j * pixelSize, margin + i * pixelSize);
                    CGContextAddRect(ctx, rectDraw);
                }
                ++data;
            }
        }
        CGContextFillPath(ctx);
    }

    三:调用生成二维:

            //数据处理部分
            CGFloat qrcodeLength = 150;
            UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake((contentView.width-qrcodeLength)/2, 10, qrcodeLength,qrcodeLength)];
            //imageV.backgroundColor = [UIColor grayColor];
            //添加二维码
            imageV.image = [self qrImageForString:@"2013.11.11购物狂欢节" imageSize:qrcodeLength];
            [contentView addSubview:imageV];

                             

  • 相关阅读:
    Ubuntu 16.04 compare 软件安装
    ubuntu 18 常用软件安装
    LSTM时间序列预测学习
    ubuntu 16.04 屏幕截图
    ubuntu 16.04 tensorboard 学习
    ubuntu 16 .04常见指令整理
    ABAP 更改销售订单(BAPI'BAPI_SALESORDER_CHANGE')
    ABAP SM30表维护生成器,新加一列描述仅供用户维护时参考,不存内表。(例如物料描述,客户描述)
    93年到底多少岁
    一个93年的中年人对2019年的总结
  • 原文地址:https://www.cnblogs.com/cocoajin/p/3418039.html
Copyright © 2011-2022 走看看