Swift_二维码、条形码的生成

Object-C对应功能实现


  • 二维码的生成
class func generateQRCode(messgae:NSString,width:CGFloat,height:CGFloat) -> UIImage {
        var returnImage:UIImage?
        if (messgae.length > 0 && width > 0 && height > 0){
            let inputData = messgae.data(using: String.Encoding.utf8.rawValue)! as NSData
            // CIQRCodeGenerator
            let filter = CIFilter.init(name: "CIQRCodeGenerator")!
            filter.setValue(inputData, forKey: "inputMessage")
            var ciImage = filter.outputImage!
            let min = width > height ? height :width;
            let scaleX = min/ciImage.extent.size.width
            let scaleY = min/ciImage.extent.size.height
            ciImage = ciImage.applying(CGAffineTransform.init(scaleX: scaleX, y: scaleY))
            returnImage = UIImage.init(ciImage: ciImage)
        }else {
            returnImage = nil;
        }
        return returnImage!
    }
  • 二维码中心添加图片
class func generateQRCodeWithCenterImage(messgae:NSString,width:CGFloat,height:CGFloat,centerImage:UIImage) -> UIImage {
        let backImage = generateQRCode(messgae: messgae, width: width, height: height)
        UIGraphicsBeginImageContext(backImage.size);
        backImage.draw(in: CGRect.init(x: 0, y: 0, width: backImage.size.width, height: backImage.size.height))
        let centerImageWH:CGFloat = backImage.size.width > backImage.size.height ? backImage.size.height * 0.2 : backImage.size.width*0.2
        centerImage.draw(in: CGRect.init(x: (backImage.size.width - centerImageWH)*0.5, y: (backImage.size.height - centerImageWH)*0.5, width: centerImageWH, height: centerImageWH))
        let returnImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return returnImage!
    }
  • 条形码的生成
class func generateBarCode(messgae:NSString,width:CGFloat,height:CGFloat) -> UIImage {
        var returnImage:UIImage?
        if (messgae.length > 0 && width > 0 && height > 0){
            let inputData:NSData? = messgae.data(using: String.Encoding.utf8.rawValue)! as NSData
            // CICode128BarcodeGenerator
            let filter = CIFilter.init(name: "CICode128BarcodeGenerator")!
            filter.setValue(inputData, forKey: "inputMessage")
            var ciImage = filter.outputImage!
            let scaleX = width/ciImage.extent.size.width
            let scaleY = height/ciImage.extent.size.height
            ciImage = ciImage.applying(CGAffineTransform.init(scaleX: scaleX, y: scaleY))
            returnImage = UIImage.init(ciImage: ciImage)
        }else {
            returnImage = nil;
        }
        return returnImage!
    }

  • 识别本地二维码图片
class func decodeQRCode(QRCodeImage:UIImage) -> NSString {
        var outputString:NSString?
        let detector:CIDetector = CIDetector.init(ofType: CIDetectorTypeQRCode, context: nil, options: [CIDetectorAccuracy:CIDetectorAccuracyHigh])!
        let image = CIImage.init(image: QRCodeImage)!
        let features = detector.features(in: image) as NSArray
        for feature in features {
            outputString = (feature as! CIQRCodeFeature).messageString! as NSString;
        }
        return outputString!
    }

代码地址:
https://github.com/FlyingKuiKui/BarCodeAndORCode_Swift.git

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值