Swift实现二维码扫描和生成

Swift实现二维码扫描和生成

二维码生成

定义属性

    // 二维码
    var qrView = UIView()
    var qrImgView = UIImageView()
    
    // 条形码
    var tView = UIView()
    var tImgView = UIImageView()

二维码、条形码显示位置

//MARK: ------二维码、条形码显示位置
    func drawCodeShowView()
    {
        // 二维码
        let rect = CGRectMake( (CGRectGetWidth(self.view.frame)-CGRectGetWidth(self.view.frame)*5/6)/2, 100, CGRectGetWidth(self.view.frame)*5/6, CGRectGetWidth(self.view.frame)*5/6)
        qrView.frame = rect
        self.view.addSubview(qrView)

        qrView.backgroundColor = UIColor.whiteColor()
        qrView.layer.shadowOffset = CGSizeMake(0, 2);
        qrView.layer.shadowRadius = 2;
        qrView.layer.shadowColor = UIColor.blackColor().CGColor
        qrView.layer.shadowOpacity = 0.5;
        qrImgView.bounds = CGRectMake(0, 0, CGRectGetWidth(qrView.frame)-12, CGRectGetWidth(qrView.frame)-12)
        qrImgView.center = CGPointMake(CGRectGetWidth(qrView.frame)/2, CGRectGetHeight(qrView.frame)/2);
        qrView .addSubview(qrImgView)

        // 条形码
        tView.frame = CGRectMake( (CGRectGetWidth(self.view.frame)-CGRectGetWidth(self.view.frame)*5/6)/2,
            CGRectGetMaxY(rect)+20,
            CGRectGetWidth(self.view.frame)*5/6,
            CGRectGetWidth(self.view.frame)*5/6*0.5)
        self.view .addSubview(tView)
        
        tView.layer.shadowOffset = CGSizeMake(0, 2);
        tView.layer.shadowRadius = 2;
        tView.layer.shadowColor = UIColor.blackColor().CGColor
        tView.layer.shadowOpacity = 0.5;
        tImgView.bounds = CGRectMake(0, 0, CGRectGetWidth(tView.frame)-12, CGRectGetHeight(tView.frame)-12);
        tImgView.center = CGPointMake(CGRectGetWidth(tView.frame)/2, CGRectGetHeight(tView.frame)/2);
        tView .addSubview(tImgView)
    }

生成二维码

    func createQR1()
    {
        // qrView.hidden = false
        // tView.hidden = true
        let qrImg = LBXScanWrapper.createCode("CIQRCodeGenerator",codeString:"[email protected]", size: qrImgView.bounds.size, qrColor: UIColor.blackColor(), bkColor: UIColor.whiteColor())

        let logoImg = UIImage(named: "登陆logo")
        qrImgView.image = LBXScanWrapper.addImageLogo(qrImg!, logoImg: logoImg!, logoSize: CGSizeMake(30, 30))
    }

生成条形码

    func createCode128()
    {
        let qrImg = LBXScanWrapper.createCode128("005103906002", size: qrImgView.bounds.size, qrColor: UIColor.blackColor(), bkColor: UIColor.whiteColor())
        tImgView.image = qrImg
    }

二维码扫描

   func toScan(){
        print("toScan")
        
        
        var style = LBXScanViewStyle()     // 设置扫码区域参数
        
        style.centerUpOffset = 50;         // 矩形框(视频显示透明区)域向上移动偏移量
        style.xScanRetangleOffset = 50;    // 矩形框(视频显示透明区)域离界面左边及右边距离
        
        if UIScreen.mainScreen().bounds.size.height <= 480 {
            // 3.5inch 显示的扫码缩小
            style.centerUpOffset = 40;
            style.xScanRetangleOffset = 30;
        }
        
        style.red_notRecoginitonArea = 0.4     // 非识别区域颜色,默认 RGBA (0,0,0,0.5),范围(0--1)
        style.green_notRecoginitonArea = 0.4   // 非识别区域颜色,默认 RGBA (0,0,0,0.5),范围(0--1)
        style.blue_notRecoginitonArea = 0.4    // 非识别区域颜色,默认 RGBA (0,0,0,0.5),范围(0--1)
        style.alpa_notRecoginitonArea = 0.4    // 非识别区域颜色,默认 RGBA (0,0,0,0.5),范围(0--1)

        style.photoframeAngleStyle = LBXScanViewPhotoframeAngleStyle.Inner;  // 扫码区域的4个角类型,内嵌,一般不显示矩形框情况下
        style.photoframeLineW = 2.0;  // 扫码区域4个角的线条宽度,默认6,建议8到4之间
        style.photoframeAngleW = 16;  // 扫码区域4个角的宽度
        style.photoframeAngleH = 16;  // 扫码区域4个角的高度
        
        style.isNeedShowRetangle = false;   // 是否需要绘制扫码矩形框,默认YES
        
        style.anmiationStyle = LBXScanViewAnimationStyle.NetGrid;     // 扫码动画效果:线条或网格
        style.animationImage = UIImage(named: "qrcode_scan_full_net") // 动画效果的图像,如线条或网格的图像
        
        if #available(iOS 8.0, *) {
            let vc = LBXScanViewController()
            vc.scanStyle = style
            self.navigationController?.pushViewController(vc, animated: true)
        } else {
            // Fallback on earlier versions
        }
    }

你可能感兴趣的:(Swift实现二维码扫描和生成)