代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import UIKit class VIEwController : UIVIEwController { overrIDe func vIEwDIDLoad() { super .vIEwDIDLoad() let qrcodeimg = UIImage (named: "codeBg.png" ) ciImage: CIImage = CIImage (image:qrcodeimg!)! context = CIContext (options: nil ) detector: CIDetector (ofType: CIDetectorTypeQRCode , context: context,options: [ CIDetectorAccuracy CIDetectorAccuracyHigh ]) features=detector.featuresInImage(ciImage) print ( "扫描到二维码个数:\(features.count)" ) //遍历所有的二维码,并框出 for feature in features as ! [ CiqrCodeFeature ] { (feature.messageString) } } dIDReceiveMemoryWarning() { .dIDReceiveMemoryWarning() } } |
http://www.hangge.com
http://www.hangge.com 可以看到两个二维码都成功的读取到了。
(注:这个我原来用模拟器一直检测不到二维码,使用真机调试就没问题。 感谢网友“落叶”的提醒,模拟器选iphone5s及以上设备也是可以检测到的。)
2,从相册中选择图片读取二维码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | import UIKit class VIEwController : UIVIEwController UIImagePickerControllerDelegate UINavigationControllerDelegate { overrIDe func vIEwDIDLoad() { super .vIEwDIDLoad() } //选取相册 @IBAction fromAlbum(sender: AnyObject ) { //判断设置是否支持图片库 if UIImagePickerController .isSourceTypeAvailable(. Photolibrary ){ //初始化图片控制器 let picker = UIImagePickerController () //设置代理 picker.delegate = self //指定图片控制器类型 picker.sourceType = UIImagePickerControllerSourceType . Photolibrary //d出控制器,显示界面 self .presentVIEwController(picker,animated: true VoID in }) } else { print ( "读取相册错误" ) } } //选择图片成功后代理 imagePickerController(picker: String : ]) { //获取选择的原图 image = info[ UIImagePickerControllerOriginalimage ] as ! UIImage //二维码读取 ciImage: CIImage = CIImage (image:image)! context = CIContext (options: nil ) detector: CIDetector (ofType: CIDetectorTypeQRCode CIDetectorAccuracy CIDetectorAccuracyHigh ]) features=detector.featuresInImage(ciImage) "扫描到二维码个数:\(features.count)" ) //遍历所有的二维码,并框出 for feature in features ! [ CiqrCodeFeature ] { (feature.messageString) } //图片控制器退出 picker.dismissVIEwControllerAnimated( VoID in }) } dIDReceiveMemoryWarning() { .dIDReceiveMemoryWarning() } } |
3,使用摄像头扫描读取二维码
(1)扫描主要使用的是 AVFoundation,用起来方便简单
(2)通过 AVCaptureMetadataOutput的 rectOfInterest属性,可以设置探测探测区域。同时给这个探测区域添加个方框,只有在框中的二维码才会被扫描到。
效果图如下:
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | import UIKit AVFoundation class VIEwController : UIVIEwController AVCaptureMetadataOutputObjectsDelegate UIAlertVIEwDelegate { var scanRectVIEw: UIVIEw ! device: AVCaptureDevice ! input: AVCaptureDeviceinput ! output: AVCaptureMetadataOutput ! session: AVCaptureSession ! prevIEw: AVCaptureVIDeoPrevIEwLayer ! overrIDe func vIEwDIDLoad() { super .vIEwDIDLoad() } //通过摄像头扫描 @IBAction fromCamera(sender: AnyObject ) { do{ self .device = AVCaptureDevice .defaultDeviceWithMediaType( AVMediaTypeVIDeo ) .input = try (device: device) .output = AVCaptureMetadataOutput () output.setMetadataObjectsDelegate( () if UIScreen .mainScreen().bounds.size.height<500 { .session.sessionPreset = AVCaptureSessionPreset640x480 } else { AVCaptureSessionPresetHigh } .session.addinput( .input) .session.addOutput( .output) .output.MetadataObjectTypes = [ AVMetadataObjectTypeQRCode ] //计算中间可探测区域 let windowsize: CGSize = .mainScreen().bounds.size; scanSize: CGSizeMake (windowsize.wIDth*3/4, windowsize.wIDth*3/4); scanRect: CGRect = CGRectMake ((windowsize.wIDth-scanSize.wIDth)/2, (windowsize.height-scanSize.height)/2,scanSize.wIDth,scanSize.height); //计算rectOfInterest 注意x,y交换位置 scanRect = (scanRect.origin.y/windowsize.height, scanRect.origin.x/windowsize.wIDth, scanRect.size.height/windowsize.height, scanRect.size.wIDth/windowsize.wIDth); //设置可探测区域 .output.rectOfInterest = scanRect .prevIEw = AVCaptureVIDeoPrevIEwLayer (session: .session) .prevIEw.vIDeoGravity = AVLayerVIDeoGravityResizeAspectFill .prevIEw.frame = .mainScreen().bounds .vIEw.layer.insertSublayer( .prevIEw,atIndex:0) //添加中间的探测区域绿框 .scanRectVIEw = UIVIEw (); .vIEw.addSubvIEw( .scanRectVIEw) .scanRectVIEw.frame = CGRectMake (0,scanSize.height); .scanRectVIEw.center = CGPointMake ( CGRectGetMIDX ( UIScreen .mainScreen().bounds), CGRectGetMIDY .mainScreen().bounds)); .scanRectVIEw.layer.bordercolor = UIcolor .greencolor(). CGcolor .scanRectVIEw.layer.borderWIDth = 1; //开始捕获 .session.startRunning() }catch _ as NSError { //打印错误消息 errorAlert = UIAlertVIEw (Title: "提醒" "请在iPhone的\"设置-隐私-相机\"选项中,允许本程序访问您的相机" "确定" ) errorAlert.show() } } //摄像头捕获 captureOutput(captureOutput: AVCaptureOutput !, dIDOutputMetadataObjects MetadataObjects: [ AnyObject ]!, fromConnection connection: AVCaptureConnection !) { stringValue: String ? if MetadataObjects.count > 0 { MetadataObject = MetadataObjects[0] as ! AVMetadataMachineReadableCodeObject stringValue = MetadataObject.stringValue @H_403_1448@ stringValue != nil { .session.stopRunning() } } .session.stopRunning() //输出结果 alertVIEw = UIAlertVIEw "二维码" ) alertVIEw.show() } //消息框确认后消失 alertVIEw(alertVIEw: Int ) { //继续扫描 .session.startRunning() } dIDReceiveMemoryWarning() { .dIDReceiveMemoryWarning() } } |
原文出自: www.hangge.com 转载请保留原文链接: http://www.hangge.com/blog/cache/detail_915.html 总结
以上是内存溢出为你收集整理的Swift - 二维码QRCode的读取全部内容,希望文章能够帮你解决Swift - 二维码QRCode的读取所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)