Swift CIDetector识别相册的二维码
Posted 小炮陈
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Swift CIDetector识别相册的二维码相关的知识,希望对你有一定的参考价值。
import UIKit
class firstViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
var picker :UIImagePickerController!
@IBAction func button(_ sender: UIButton) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .photoLibrary
self.present(picker, animated: true, completion: nil)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
//获取选择的图片
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
//二维码读取
DispatchQueue.global().async {
let imageData = UIImagePNGRepresentation(image)
let ciImage:CIImage=CIImage(data: imageData!)!
let detector = CIDetector(ofType: CIDetectorTypeQRCode, context:nil,options:[CIDetectorAccuracy:CIDetectorAccuracyHigh])
let feature = detector?.features(in: ciImage)
let f = feature?.first as! CIQRCodeFeature
print(f.messageString ?? "")
}
//返回
picker.dismiss(animated: true, completion: {
() -> Void in
})
}
}
以上是关于Swift CIDetector识别相册的二维码的主要内容,如果未能解决你的问题,请参考以下文章
[iOS]CIDetector之CIDetectorTypeFace人脸识别