本人不善于编辑文字,就直接上代码了
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
// let codeImg = UIImage(named: "code")
let ciImage : CIImage = CIImage(image: image)!
let context = CIContext(options: nil)
//CIDetector可以识别人脸,条形码,二维码,通过设定ofType来决定
let detector = CIDetector(ofType: CIDetectorTypeQRCode, context: context, options: [CIDetectorAccuracy:CIDetectorAccuracyHigh])
//如果图片里有多个二维码的话m,就会返回多个二维码信息,需要自己做判断选取
let features = detector?.features(in: ciImage)
print("扫描二维码个数:\(features?.count ?? 0)")
self.dismiss(animated: true) {
for feature in features as![CIQRCodeFeature] {
let alert = UIAlertController.init(title: "识别目标", message:feature.messageString, preferredStyle:.alert)
let action = UIAlertAction.init(title: "确定", style: .default) { (UIAlertAction) in
}
alert.addAction(action)
self .present(alert, animated: true, completion: nil)
}
}