美文网首页
iOS 二维码生成以及识别

iOS 二维码生成以及识别

作者: 砖加 | 来源:发表于2019-02-21 16:29 被阅读0次

PS: 一直忙着解决提审上线问题,终于下下来精心整理下以前写的东西了,最近苹果审核槽点太多. 感觉不爱了.

import AVFoundation

//MARK: -二维码图片生成以及识别

extension QRCodeTool{

//MARK: -生成二维码图片

    @objc class func encodeQrString(_ infoDic:[String:Any])->CIImage{

          //infoDicNew 为二维码内需要包含的信息

        // 1.创建过滤器,这里的@"CIQRCodeGenerator"是固定的

        let filter = CIFilter.init(name: "CIQRCodeGenerator")

        // 2.恢复默认设置

        filter?.setDefaults()

        // 3. 给过滤器添加数据

        var infoJsonStr = infoDic转成的jsonstring

        var charset = CharacterSet.urlHostAllowed

        charset.remove(charactersIn: "+")

        charset.remove(charactersIn: "=")

let urlPrefix = "Associated Domains" 或者 app 的 scheme:// 

//按需要拼接,如果项目配置了通用链接(Universal

Links)的话,可以让server配置个默认落地页,直接二维码识别出url可以跳转到落地页展示app或者直接跳转到你的app,关于通用链接的说明我就不写了,别人写的挺好的.可参照http://www.cocoachina.com/ios/20150902/13321.html

        let urlSuffix = infoJsonStr(infoJsonStr 可做加密)

        let urlStr = urlPrefix + urlSuffix

        let qrcodeData = urlStr.data(using: String.Encoding.utf8)

        filter?.setValue(qrcodeData, forKey: "inputMessage")

        // 4. 生成二维码图片

        return filter!.outputImage!

    }

//MARK: -识别图片 成功结果为空时表明识别失败

    class func scan(img:UIImage?)->String?{

        if img == nil{

             //二维码图片为空

            return nil;

        }

      let detector = CIDetector.init(ofType: CIDetectorTypeQRCode,

context: nil, options: [CIDetectorAccuracy:CIDetectorAccuracyHigh])

        // 识别二维码取得识别结果

        //先识别原图片

        var features = detector?.features(in: CIImage.init(cgImage: img!.cgImage!))

        //messageString

        var resultStr:String?

        if (features?.count ?? 0) == 0{

            //识别失败,放大的二维码图片并识别

            let image = screenSizeImage(with: img!)

            features = detector?.features(in:  CIImage.init(cgImage:image!.cgImage!))

            if (features?.count ?? 0) > 0{

                if let feature = features![0] as? CIQRCodeFeature{

                    resultStr = feature.messageString

                }

            }

        } else {

            for feature in features!{

                if feature.type == CIFeatureTypeQRCode {

                    resultStr = (feature as? CIQRCodeFeature)?.messageString

                    break;

                }

            }

        }

       return  resultStr

}

// 放大二维码图片

class  func screenSizeImage(with image:UIImage)->UIImage{

           let imageWidth = image.size.width

           let imageHeight = image.size.height

          if (imageWidth <= kScreenWidth && imageHeight <= kScreenHeight) {

                 return image;

          }

         let maxWH = max(imageWidth, imageHeight)

         let scale = maxWH / (kScreenHeight * 2.0)

         let size = CGSize(width:imageWidth / scale, height: imageHeight / scale)

          UIGraphicsBeginImageContext(size);

          image.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))

          let newImage = UIGraphicsGetImageFromCurrentImageContext();

           UIGraphicsEndImageContext();

           return newImage ?? UIImage()

}

相关文章

网友评论

      本文标题:iOS 二维码生成以及识别

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