Swift - 二维码QRCode的读取

前端之家收集整理的这篇文章主要介绍了Swift - 二维码QRCode的读取前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
1,直接读取图片中的二维码
使用 CIDetector可以很方便的检测并读取二维码。下面是一个从 UIImage中读取二维码的样例,我们要把图片上所有的二维码信息都打印出来。 @H_403_11@
@H_403_11@
@H_403_11@

@H_403_11@
代码如下: @H_403_11@
1 @H_403_11@
2 @H_403_11@
3 @H_403_11@
4 @H_403_11@
5 @H_403_11@
6 @H_403_11@
7 @H_403_11@
8 @H_403_11@
9 @H_403_11@
10 @H_403_11@
11 @H_403_11@
12 @H_403_11@
13 @H_403_11@
14 @H_403_11@
15 @H_403_11@
16 @H_403_11@
17 @H_403_11@
18 @H_403_11@
19 @H_403_11@
20 @H_403_11@
21 @H_403_11@
22 @H_403_11@
23 @H_403_11@
24 @H_403_11@
25 @H_403_11@
26 @H_403_11@
27 @H_403_11@
import UIKit @H_403_11@
@H_403_11@
class ViewController : UIViewController { @H_403_11@
@H_403_11@
override func viewDidLoad() { @H_403_11@
super .viewDidLoad() @H_403_11@
@H_403_11@
let qrcodeImg = UIImage (named: "codeBg.png" ) @H_403_11@
ciImage: CIImage = CIImage (image:qrcodeImg!)! @H_403_11@
@H_403_11@
context = CIContext (options: nil ) @H_403_11@
detector: CIDetector (ofType: CIDetectorTypeQRCode , @H_403_11@
context: context,options: [ CIDetectorAccuracy CIDetectorAccuracyHigh ]) @H_403_11@
@H_403_11@
features=detector.featuresInImage(ciImage) @H_403_11@
@H_403_11@
print ( "扫描到二维码个数:\(features.count)" ) @H_403_11@
//遍历所有的二维码,并框出 @H_403_11@
for feature in features as ! [ CIQRCodeFeature ] { @H_403_11@
(feature.messageString) @H_403_11@
} @H_403_11@
} @H_403_11@
@H_403_11@
didReceiveMemoryWarning() { @H_403_11@
.didReceiveMemoryWarning() @H_403_11@
} @H_403_11@
} @H_403_11@ @H_403_11@
@H_403_11@ @H_403_11@
控制台输出如下: @H_403_11@
扫描到二维码个数:2
http://www.hangge.com
http://www.hangge.com @H_403_11@ 可以看到两个二维码都成功的读取到了。

@H_403_11@
(注:这个我原来用模拟器一直检测不到二维码,使用真机调试就没问题。 感谢网友“落叶”的提醒,模拟器选iphone5s及以上设备也是可以检测到的。

2,从相册中选择图片读取二维码
1 @H_403_11@
2 @H_403_11@
3 @H_403_11@
4 @H_403_11@
5 @H_403_11@
6 @H_403_11@
7 @H_403_11@
8 @H_403_11@
9 @H_403_11@
10 @H_403_11@
11 @H_403_11@
12 @H_403_11@
13 @H_403_11@
14 @H_403_11@
15 @H_403_11@
16 @H_403_11@
17 @H_403_11@
18 @H_403_11@
19 @H_403_11@
20 @H_403_11@
21 @H_403_11@
22 @H_403_11@
23 @H_403_11@
24 @H_403_11@
25 @H_403_11@
26 @H_403_11@
27 @H_403_11@
28 @H_403_11@
29 @H_403_11@
30 @H_403_11@
31 @H_403_11@
32 @H_403_11@
33 @H_403_11@
34 @H_403_11@
35 @H_403_11@
36 @H_403_11@
37 @H_403_11@
38 @H_403_11@
39 @H_403_11@
40 @H_403_11@
41 @H_403_11@
42 @H_403_11@
43 @H_403_11@
44 @H_403_11@
45 @H_403_11@
46 @H_403_11@
47 @H_403_11@
48 @H_403_11@
49 @H_403_11@
50 @H_403_11@
51 @H_403_11@
52 @H_403_11@
53 @H_403_11@
54 @H_403_11@
55 @H_403_11@
56 @H_403_11@
57 @H_403_11@
import UIKit @H_403_11@
@H_403_11@
class ViewController : UIViewController UIImagePickerControllerDelegate 403_11@
UINavigationControllerDelegate { @H_403_11@
@H_403_11@
override func viewDidLoad() { @H_403_11@
super .viewDidLoad() @H_403_11@
} @H_403_11@
@H_403_11@
//选取相册 @H_403_11@
@IBAction fromAlbum(sender: AnyObject ) { @H_403_11@
//判断设置是否支持图片库 @H_403_11@
if UIImagePickerController .isSourceTypeAvailable(. PhotoLibrary ){ @H_403_11@
//初始化图片控制器 @H_403_11@
let picker = UIImagePickerController () @H_403_11@
//设置代理 @H_403_11@
picker.delegate = self @H_403_11@
//指定图片控制器类型 @H_403_11@
picker.sourceType = UIImagePickerControllerSourceType . PhotoLibrary @H_403_11@
//弹出控制器,显示界面 @H_403_11@
self .presentViewController(picker,animated: true 403_11@
() -> Void in @H_403_11@
}) @H_403_11@
} else { @H_403_11@
print ( "读取相册错误" ) @H_403_11@
} @H_403_11@
} @H_403_11@
@H_403_11@
//选择图片成功后代理 @H_403_11@
imagePickerController(picker: 403_11@
didFinishPickingMediaWithInfo info: [ String : ]) { @H_403_11@
@H_403_11@
//获取选择的原图 @H_403_11@
image = info[ UIImagePickerControllerOriginalImage ] as ! UIImage @H_403_11@
@H_403_11@
//二维码读取 @H_403_11@
ciImage: CIImage = CIImage (image:image)! @H_403_11@
context = CIContext (options: nil ) @H_403_11@
detector: CIDetector (ofType: CIDetectorTypeQRCode 403_11@
CIDetectorAccuracy CIDetectorAccuracyHigh ]) @H_403_11@
features=detector.featuresInImage(ciImage) @H_403_11@
"扫描到二维码个数:\(features.count)" ) @H_403_11@
//遍历所有的二维码,并框出 @H_403_11@
for feature in features ! [ CIQRCodeFeature ] { @H_403_11@
(feature.messageString) @H_403_11@
} @H_403_11@
@H_403_11@
//图片控制器退出 @H_403_11@
picker.dismissViewControllerAnimated( 403_11@
() -> Void in @H_403_11@
}) @H_403_11@
} @H_403_11@
@H_403_11@
didReceiveMemoryWarning() { @H_403_11@
.didReceiveMemoryWarning() @H_403_11@
} @H_403_11@
} @H_403_11@ @H_403_11@
@H_403_11@ @H_403_11@
3,使用摄像头扫描读取二维码
(1)扫描主要使用的是 AVFoundation,用起来方便简单
(2)通过 AVCaptureMetadataOutputrectOfInterest属性,可以设置探测探测区域。同时给这个探测区域添加个方框,只有在框中的二维码才会被扫描到。

效果图如下:
@H_403_11@ @H_403_11@

代码如下:
1 @H_403_11@
2 @H_403_11@
3 @H_403_11@
4 @H_403_11@
5 @H_403_11@
6 @H_403_11@
7 @H_403_11@
8 @H_403_11@
9 @H_403_11@
10 @H_403_11@
11 @H_403_11@
12 @H_403_11@
13 @H_403_11@
14 @H_403_11@
15 @H_403_11@
16 @H_403_11@
17 @H_403_11@
18 @H_403_11@
19 @H_403_11@
20 @H_403_11@
21 @H_403_11@
22 @H_403_11@
23 @H_403_11@
24 @H_403_11@
25 @H_403_11@
26 @H_403_11@
27 @H_403_11@
28 @H_403_11@
29 @H_403_11@
30 @H_403_11@
31 @H_403_11@
32 @H_403_11@
33 @H_403_11@
34 @H_403_11@
35 @H_403_11@
36 @H_403_11@
37 @H_403_11@
38 @H_403_11@
39 @H_403_11@
40 @H_403_11@
41 @H_403_11@
42 @H_403_11@
43 @H_403_11@
44 @H_403_11@
45 @H_403_11@
46 @H_403_11@
47 @H_403_11@
48 @H_403_11@
49 @H_403_11@
50 @H_403_11@
51 @H_403_11@
52 @H_403_11@
53 @H_403_11@
54 @H_403_11@
55 @H_403_11@
56 @H_403_11@
57 @H_403_11@
58 @H_403_11@
59 @H_403_11@
60 @H_403_11@
61 @H_403_11@
62 @H_403_11@
63 @H_403_11@
64 @H_403_11@
65 @H_403_11@
66 @H_403_11@
67 @H_403_11@
68 @H_403_11@
69 @H_403_11@
70 @H_403_11@
71 @H_403_11@
72 @H_403_11@
73 @H_403_11@
74 @H_403_11@
75 @H_403_11@
76 @H_403_11@
77 @H_403_11@
78 @H_403_11@
79 @H_403_11@
80 @H_403_11@
81 @H_403_11@
82 @H_403_11@
83 @H_403_11@
84 @H_403_11@
85 @H_403_11@
86 @H_403_11@
87 @H_403_11@
88 @H_403_11@
89 @H_403_11@
90 @H_403_11@
91 @H_403_11@
92 @H_403_11@
93 @H_403_11@
94 @H_403_11@
95 @H_403_11@
96 @H_403_11@
97 @H_403_11@
98 @H_403_11@
99 @H_403_11@
100 @H_403_11@
101 @H_403_11@
102 @H_403_11@
103 @H_403_11@
104 @H_403_11@
105 @H_403_11@
106 @H_403_11@
107 @H_403_11@
108 @H_403_11@
109 @H_403_11@
110 @H_403_11@
111 @H_403_11@
import UIKit @H_403_11@
AVFoundation @H_403_11@
@H_403_11@
class ViewController : UIViewController AVCaptureMetadataOutputObjectsDelegate 403_11@
UIAlertViewDelegate { @H_403_11@
@H_403_11@
var scanRectView: UIView ! @H_403_11@
device: AVCaptureDevice ! @H_403_11@
input: AVCaptureDeviceInput ! @H_403_11@
output: AVCaptureMetadataOutput ! @H_403_11@
session: AVCaptureSession ! @H_403_11@
preview: AVCaptureVideoPreviewLayer ! @H_403_11@
@H_403_11@
override func viewDidLoad() { @H_403_11@
super .viewDidLoad() @H_403_11@
} @H_403_11@
@H_403_11@
//通过摄像头扫描 @H_403_11@
@IBAction fromCamera(sender: AnyObject ) { @H_403_11@
do{ @H_403_11@
self .device = AVCaptureDevice .defaultDeviceWithMediaType( AVMediaTypeVideo ) @H_403_11@
@H_403_11@
.input = try (device: device) @H_403_11@
@H_403_11@
.output = AVCaptureMetadataOutput () @H_403_11@
output.setMetadataObjectsDelegate( 403_11@
@H_403_11@
.session = () @H_403_11@
if UIScreen .mainScreen().bounds.size.height<500 { @H_403_11@
.session.sessionPreset = AVCaptureSessionPreset640x480 @H_403_11@
} else { @H_403_11@
AVCaptureSessionPresetHigh @H_403_11@
} @H_403_11@
@H_403_11@
.session.addInput( .input) @H_403_11@
.session.addOutput( .output) @H_403_11@
@H_403_11@
.output.MetadataObjectTypes = [ AVMetadataObjectTypeQRCode ] @H_403_11@
@H_403_11@
//计算中间可探测区域 @H_403_11@
let windowSize: CGSize = .mainScreen().bounds.size; @H_403_11@
scanSize: CGSizeMake (windowSize.width*3/4, @H_403_11@
windowSize.width*3/4); @H_403_11@
scanRect: CGRect = CGRectMake ((windowSize.width-scanSize.width)/2, @H_403_11@
(windowSize.height-scanSize.height)/2,scanSize.width,scanSize.height); @H_403_11@
//计算rectOfInterest 注意x,y交换位置 @H_403_11@
scanRect = (scanRect.origin.y/windowSize.height, @H_403_11@
scanRect.origin.x/windowSize.width, @H_403_11@
scanRect.size.height/windowSize.height, @H_403_11@
scanRect.size.width/windowSize.width); @H_403_11@
//设置可探测区域 @H_403_11@
.output.rectOfInterest = scanRect @H_403_11@
@H_403_11@
.preview = AVCaptureVideoPreviewLayer (session: .session) @H_403_11@
.preview.videoGravity = AVLayerVideoGravityResizeAspectFill @H_403_11@
.preview.frame = .mainScreen().bounds @H_403_11@
.view.layer.insertSublayer( .preview,atIndex:0) @H_403_11@
@H_403_11@
//添加中间的探测区域绿框 @H_403_11@
.scanRectView = UIView (); @H_403_11@
.view.addSubview( .scanRectView) @H_403_11@
.scanRectView.frame = CGRectMake (0,scanSize.height); @H_403_11@
.scanRectView.center = CGPointMake ( @H_403_11@
CGRectGetMidX ( UIScreen .mainScreen().bounds), @H_403_11@
CGRectGetMidY .mainScreen().bounds)); @H_403_11@
.scanRectView.layer.borderColor = UIColor .greenColor(). CGColor @H_403_11@
.scanRectView.layer.borderWidth = 1; @H_403_11@
@H_403_11@
//开始捕获 @H_403_11@
.session.startRunning() @H_403_11@
}catch _ as NSError { @H_403_11@
//打印错误消息 @H_403_11@
errorAlert = UIAlertView (title: "提醒" 403_11@
message: "请在iPhone的\"设置-隐私-相机\"选项中,允许本程序访问您的相机" 403_11@
delegate: 403_11@
cancelButtonTitle: "确定" ) @H_403_11@
errorAlert.show() @H_403_11@
} @H_403_11@
} @H_403_11@
@H_403_11@
//摄像头捕获 @H_403_11@
captureOutput(captureOutput: AVCaptureOutput !, @H_403_11@
didOutputMetadataObjects MetadataObjects: [ AnyObject ]!, @H_403_11@
fromConnection connection: AVCaptureConnection !) { @H_403_11@
@H_403_11@
stringValue: String ? @H_403_11@
if MetadataObjects.count > 0 { @H_403_11@
MetadataObject = MetadataObjects[0] as ! AVMetadataMachineReadableCodeObject @H_403_11@
stringValue = MetadataObject.stringValue @H_403_11@
@H_403_11@
stringValue != nil { @H_403_11@
.session.stopRunning() @H_403_11@
} @H_403_11@
} @H_403_11@
.session.stopRunning() @H_403_11@
//输出结果 @H_403_11@
alertView = UIAlertView "二维码" 403_11@
) @H_403_11@
alertView.show() @H_403_11@
} @H_403_11@
@H_403_11@
//消息框确认后消失 @H_403_11@
alertView(alertView: Int ) { @H_403_11@
//继续扫描 @H_403_11@
.session.startRunning() @H_403_11@
} @H_403_11@
@H_403_11@
didReceiveMemoryWarning() { @H_403_11@
.didReceiveMemoryWarning() @H_403_11@
} @H_403_11@
} @H_403_11@ @H_403_11@
@H_403_11@ @H_403_11@ @H_403_11@ @H_403_11@
原文出自: www.hangge.com 转载请保留原文链接 http://www.hangge.com/blog/cache/detail_915.html

猜你在找的Swift相关文章