Swift 扫一扫实现(二维码 条形码)


新的一年加油

有几天没写东西了,新的一年新气象,希望大家新的一年好运连连、钞票多多。好了废话不多说,今天来说下swift实现扫一扫功能,希望能对大家有些帮助:

1.添加必要的视图创建UI

(1).全局视图变量

/// 扫描容器

var customContainerView: UIView!

/// 底部工具条

var customTabbar: UITabBar!

/// 结果文本

var customLabel: UILabel!

/// 冲击波视图

var scanLineView: UIImageView!

///框

var borderIV: UIImageView!

(2)加载UI

private func setUpUI() {

let rightItem: UIBarButtonItem = UIBarButtonItem(title: "相册", style: .plain, target: self, action: #selector(choosePicFromPhotoLib(sender:)))

navigationItem.rightBarButtonItem = rightItem

customContainerView = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth-100, height: kScreenWidth-100))

customContainerView.center = self.view.center;

customContainerView.backgroundColor = UIColor.yellow

customContainerView.clipsToBounds = true

view.addSubview(customContainerView)

customLabel = UILabel(frame: CGRect(x: 0, y: kNavbarHeight+20, width: kScreenWidth, height: 40))

customLabel.textColor = UIColor.white

customLabel.textAlignment = NSTextAlignment.center

view.addSubview(customLabel)

borderIV = UIImageView(frame: customContainerView.frame)

borderIV.image = UIImage(named: "codeframe")

borderIV.clipsToBounds = true

view.addSubview(borderIV)

scanLineView = UIImageView(frame: CGRect(x: 0, y: 0-customContainerView.frame.size

.height, width: customContainerView.frame.size.width, height: customContainerView.frame.size.height))

scanLineView.image = UIImage(named: "qrcode_scanline_qrcode")

borderIV.addSubview(scanLineView)

customTabbar = UITabBar(frame: CGRect(x: 0, y: (kScreenHeight-kTabBarHeight), width: kScreenWidth, height: 49))

customTabbar.delegate = self

customTabbar.backgroundColor = UIColor.red

customTabbar.barTintColor = UIColor.red

view.addSubview(customTabbar)

let leftBarItem: UITabBarItem = UITabBarItem(title: "", image: UIImage(named: "qrcode_tabbar_icon_qrcode"), selectedImage: UIImage(named: "qrcode_tabbar_icon_qrcode_highlighted"));

let rightBarItem: UITabBarItem = UITabBarItem(title: "", image: UIImage(named: "qrcode_tabbar_icon_barcode"), selectedImage: UIImage(named: "qrcode_tabbar_icon_barcode_highlighted"));

customTabbar.setItems([leftBarItem,rightBarItem], animated: true)

customTabbar.selectedItem = customTabbar.items?.first

scanQRCode()

}

2.创建使用相机等所必需的方法 AVCaptureSession相关

流程:1 建立Session  2 添加 input  3 添加output  4 开始捕捉  5 录制、照相  6 捕捉  7 结束捕捉 8 参考

// MARK: -懒加载

//设备输入

private lazy var input: AVCaptureDeviceInput? = {

let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)

return try? AVCaptureDeviceInput(device: device)

}()

//创建Session

private lazy var session:AVCaptureSession = AVCaptureSession()

//设备输出

private lazy var output: AVCaptureMetadataOutput = {

let out = AVCaptureMetadataOutput()

let viewRect = self.view.frame

let containerRect = customContainerView.frame;

let x = containerRect.origin.y / viewRect.height;

let y = containerRect.origin.x / viewRect.width;

let width = containerRect.height / viewRect.height;

let height = containerRect.width / viewRect.width;

out.rectOfInterest = CGRect(x: x, y: y, width: width, height: height)

return out

}()

lazy var containerLayer:CALayer = CALayer()

/// 预览图层

lazy var previewLayer: AVCaptureVideoPreviewLayer = AVCaptureVideoPreviewLayer(session: self.session)

3.扫描准备scanQRCode

// MARK: - 内部控制方法

private func scanQRCode()

{

// 1.判断输入能否添加到会话中

if !session.canAddInput(input)

{

return

}

// 2.判断输出能够添加到会话中

if !session.canAddOutput(output)

{

return

}

// 3.添加输入和输出到会话中

session.addInput(input)

session.addOutput(output)

// 4.设置输出能够解析的数据类型

// 注意点: 设置数据类型一定要在输出对象添加到会话之后才能设置

output.metadataObjectTypes = output.availableMetadataObjectTypes

// 5.设置监听监听输出解析到的数据

output.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)

// 6.添加预览图层

view.layer.insertSublayer(previewLayer, at: 0)

previewLayer.frame = view.bounds

// 7.添加容器图层

view.layer.addSublayer(containerLayer)

containerLayer.frame = view.bounds

// 8.开始扫描

session.startRunning()

}

4.开始扫描动画

func startAnimation()

{

//2.执行扫描动画

UIView.animate(withDuration: 1.5) { () -> Void in

UIView.setAnimationRepeatCount(MAXFLOAT)

if (customTabbar.selectedItem == customTabbar.items?.first){

scanLineView.frame = CGRect(x: scanLineView.frame.origin.x, y: scanLineView.frame.origin.y+customContainerView.frame.size.height+100, width: scanLineView.frame.size.width, height: scanLineView.frame.size.height)

}else {

scanLineView.frame = CGRect(x:scanLineView.frame.origin.x, y:borderIV.frame.origin.y+borderIV.frame.size.height, width:borderIV.frame.size.width, height:borderIV.frame.size.height)

}

}

}

5.扩展实现UITabBarDelegate

extension QRCodeScanViewController: UITabBarDelegate {

func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {

// 根据当前选中的按钮重新设置二维码容器高度

if (tabBar.selectedItem == customTabbar.items?.first){

UIView.animate(withDuration: 0.5) { () -> Void in

borderIV.frame = customContainerView.frame

borderIV.center = self.view.center;

scanLineView.image = UIImage(named: "qrcode_scanline_qrcode")

}

}else{

UIView.animate(withDuration: 0.5) { () -> Void in

borderIV.frame = CGRect(x:borderIV.frame.origin.x, y:borderIV.frame.origin.y, width:borderIV.frame.size.width, height:borderIV.frame.size.width/2)

borderIV.center = self.view.center;

scanLineView.image = UIImage(named: "qrcode_scanline_barcode")

 }

}

}

}

6.扩展实现UINavigationControllerDelegate, UIImagePickerControllerDelegate

extension QRCodeScanViewController: UINavigationControllerDelegate, UIImagePickerControllerDelegate {

private func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

// 1.取出选中的图片

guard let image = info[UIImagePickerControllerOriginalImage] as? UIImage else

{

return

}

guard let ciImage = CIImage(image: image) else

{

return

}

// 2.从选中的图片中读取二维码数据

// 2.1创建一个探测器

if #available(iOS 8.0, *) {

let detector = CIDetector(ofType: CIDetectorTypeQRCode, context: nil, options: [CIDetectorAccuracy: CIDetectorAccuracyLow])

// 2.2利用探测器探测数据

let results = detector?.features(in: ciImage)

// 2.3取出探测到的数据

for result in results!

{

print((result as! CIQRCodeFeature).messageString)

customLabel.text = (result as! CIQRCodeFeature).messageString

}

} else {

// Fallback on earlier versions

}

picker.dismiss(animated: true) { () -> Void in

self.startAnimation()

}

}

}

7.扩展AVCaptureMetadataOutputObjectsDelegate

extension QRCodeScanViewController: AVCaptureMetadataOutputObjectsDelegate

{

/// 只要扫描到结果就会调用

private func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!)

{

// 1.显示结果

print(metadataObjects.last?.stringValue)

customLabel.text =  metadataObjects.last?.stringValue

clearLayers()

// 2.拿到扫描到的数据

guard let metadata = metadataObjects.last as? AVMetadataObject else

{

return

}

// 通过预览图层将corners值转换为我们能识别的类型

let objc = previewLayer.transformedMetadataObject(for: metadata)

// 2.对扫描到的二维码进行描边

drawLines(objc: objc as! AVMetadataMachineReadableCodeObject)

}

// 绘制描边

private func drawLines(objc: AVMetadataMachineReadableCodeObject)

{

// 0.安全校验

guard let array = objc.corners else

{

return

}

// 1.创建图层, 用于保存绘制的矩形

let layer = CAShapeLayer()

layer.lineWidth = 2

layer.strokeColor = UIColor.green.cgColor

layer.fillColor = UIColor.clear.cgColor

// 2.创建UIBezierPath, 绘制矩形

let path = UIBezierPath()

let point = CGPoint(x: 0, y: 0)

var index = 0

index+=1

CGPoint(dictionaryRepresentation: (array[index] as! CFDictionary))

//        CGPointWithDictionaryRepresentation((array[index++] as! CFDictionary), &point)

// 2.1将起点移动到某一个点

path.move(to: point)

// 2.2连接其它线段

while index < array.count

{

index+=1

CGPoint(dictionaryRepresentation: (array[index] as! CFDictionary))

path.addLine(to: point)

}

// 2.3关闭路径

path.close()

layer.path = path.cgPath

// 3.将用于保存矩形的图层添加到界面上

containerLayer.addSublayer(layer)

}

/// 清空描边

private func clearLayers()

{

guard let subLayers = containerLayer.sublayers else

{

return

}

for layer in subLayers

{

layer.removeFromSuperlayer()

}

}

}


8.viewDidLoad

override func viewDidLoad() {

super.viewDidLoad()

title = "扫一扫"

setUpUI()

startAnimation()

}

到这里已经实现了扫一扫的功能。图片附上:


二维码
条形码

大家有什么问题可以给我回复。

完整demo戳这里https://github.com/miaozhang9/Swift3.0Test_ImitateAliPay

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,928评论 6 509
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,748评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,282评论 0 357
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,065评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,101评论 6 395
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,855评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,521评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,414评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,931评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,053评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,191评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,873评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,529评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,074评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,188评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,491评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,173评论 2 357

推荐阅读更多精彩内容

  • SwiftDay011.MySwiftimport UIKitprintln("Hello Swift!")var...
    smile丽语阅读 3,839评论 0 6
  • 自己的项目大多都是以OC写的,随着swift版本的更新和苹果的力推,学习swift成为刻不容缓的事情,今天我们就聊...
    薄阳映初雪阅读 3,044评论 2 1
  • 今晚室友突然说起要去听考研讲座,想想便跟着去了,本打算在寝室准备二级的,但想着考研总不能做无头苍蝇吧,多去了...
    叶儿爱琵琶阅读 268评论 0 0
  • “本文参加#未完待续,就要表白#活动,本人承诺,文章为原创,且未在其他平台发表过。” 等待,是寂无声息的;喜欢,也...
    纳兰_浅语阅读 164评论 0 3