iOS 二維碼生成以及識別

weixin_34148340發表於2019-02-21

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()

}


相關文章