参考: http://www.jianshu.com/p/203a48d64e71
代码: https://github.com/cirelir/2DBarcode
ViewController.swift
import UIKit class ViewController: UIViewController{ override func viewDidLoad() { super.viewDidLoad() self.title = "首页" let button = UIButton(frame: CGRect(x: (UIScreen.main.bounds.size.width-100) * 0.5, y: (UIScreen.main.bounds.size.height-30) * 0.5, width: 100, height: 30)) button.setTitle("扫一扫", for: UIControlState()) button.setTitleColor(UIColor.blue, for: UIControlState()) button.addTarget(self, action: #selector(ViewController.buttonAction(_:)), for: UIControlEvents.touchUpInside) self.view.addSubview(button) } func buttonAction(_ sender : AnyObject){ print("扫一扫") let scanner = ScannerViewController() self.present(scanner, animated: true, completion: nil); //self.navigationController?.pushViewController(scanner, animated: true) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
ScannerViewController.swift
import UIKit import AVFoundation class ScannerViewController: UIViewController,AVCaptureMetadataOutputObjectsDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate { //相机显示视图 let cameraView = ScannerBackgroundView(frame: UIScreen.main.bounds) let captureSession = AVCaptureSession() override func viewDidLoad() { super.viewDidLoad() //self.title = "扫一扫" self.view.backgroundColor = UIColor.black //设置导航栏 /*let barButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.add, target: self, action: #selector(ScannerViewController.selectPhotoFormPhotoLibrary(_:))) self.navigationItem.rightBarButtonItem = barButtonItem*/ let getFrmPhoto = UIButton(frame: CGRect(x: 10, y: 10, width: 200, height: 80)); getFrmPhoto.backgroundColor = UIColor.red getFrmPhoto.setTitle("get From Photo", for: .normal) getFrmPhoto.addTarget(self, action: #selector(selectPhotoFormPhotoLibrary(_:)), for: .touchUpInside) self.view.addSubview(getFrmPhoto) // cameraView.frame.size.height -= 80; cameraView.frame.origin.y += 80; self.view.addSubview(cameraView) //初始化捕捉设备(AVCaptureDevice),类型AVMdeiaTypeVideo let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo) let input :AVCaptureDeviceInput //创建媒体数据输出流 let output = AVCaptureMetadataOutput() //捕捉异常 do{ //创建输入流 input = try AVCaptureDeviceInput(device: captureDevice) //把输入流添加到会话 captureSession.addInput(input) //把输出流添加到会话 captureSession.addOutput(output) }catch { print("异常") } //创建串行队列 let dispatchQueue = DispatchQueue(label: "queue", attributes: []) //设置输出流的代理 output.setMetadataObjectsDelegate(self, queue: dispatchQueue) //设置输出媒体的数据类型 output.metadataObjectTypes = NSArray(array: [AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code,AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code]) as [AnyObject] //创建预览图层 let videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession) //设置预览图层的填充方式 videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill //设置预览图层的frame videoPreviewLayer?.frame = cameraView.bounds //将预览图层添加到预览视图上 cameraView.layer.insertSublayer(videoPreviewLayer!, at: 0) //设置扫描范围 output.rectOfInterest = CGRect(x: 0.2, y: 0.2, width: 0.6, height: 0.6) } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) self.tabBarController?.tabBar.isHidden = true self.scannerStart() } func scannerStart(){ captureSession.startRunning() cameraView.scanning = "start" } func scannerStop() { captureSession.stopRunning() cameraView.scanning = "stop" } //扫描代理方法 func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) { print("_______") if metadataObjects != nil && metadataObjects.count > 0 { let metaData : AVMetadataMachineReadableCodeObject = metadataObjects.first as! AVMetadataMachineReadableCodeObject print(metaData.stringValue) DispatchQueue.main.async(execute: { print("相机扫描结果:") print(metaData.stringValue); //let result = WebViewController() //result.url = metaData.stringValue //self.navigationController?.pushViewController(result, animated: true) }) captureSession.stopRunning() } } //从相册中选择图片 func selectPhotoFormPhotoLibrary(_ sender : AnyObject){ let picture = UIImagePickerController() picture.sourceType = UIImagePickerControllerSourceType.photoLibrary picture.delegate = self self.present(picture, animated: true, completion: nil) } //选择相册中的图片完成,进行获取二维码信息 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { let image = info[UIImagePickerControllerOriginalImage] let imageData = UIImagePNGRepresentation(image as! UIImage) let ciImage = CIImage(data: imageData!) let detector = CIDetector(ofType: CIDetectorTypeQRCode, context: nil, options: [CIDetectorAccuracy: CIDetectorAccuracyLow]) let array = detector?.features(in: ciImage!) let result : CIQRCodeFeature = array!.first as! CIQRCodeFeature print("从相册获取结果:"); print(result.messageString ?? String()) } }
ScannerBackgroundView.swift
// // ScannerBackgroundView.swift // BDHome // // Created by Erwin on 16/6/17. // Copyright © 2016年 BDHome. All rights reserved. // import UIKit class ScannerBackgroundView: UIView { //屏幕扫描区域视图 let barcodeView = UIView(frame: CGRect(x: UIScreen.main.bounds.size.width * 0.2, y: UIScreen.main.bounds.size.height * 0.35, width: UIScreen.main.bounds.size.width * 0.6, height: UIScreen.main.bounds.size.width * 0.6)) //扫描线 let scanLine = UIImageView() var scanning : String! var timer = Timer() override init(frame: CGRect) { super.init(frame: frame) barcodeView.layer.borderWidth = 1.0 barcodeView.layer.borderColor = UIColor.white.cgColor self.addSubview(barcodeView) //设置扫描线 scanLine.frame = CGRect(x: 0, y: 0, width: barcodeView.frame.size.width, height: 5) scanLine.image = UIImage(named: "QRCodeScanLine") //添加扫描线图层 barcodeView.addSubview(scanLine) self.createBackGroundView() self.addObserver(self, forKeyPath: "scanning", options: .new, context: nil) timer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(moveScannerLayer(_:)), userInfo: nil, repeats: true) } func createBackGroundView() { let topView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height * 0.35)) let bottomView = UIView(frame: CGRect(x: 0, y: UIScreen.main.bounds.size.width * 0.6 + UIScreen.main.bounds.size.height * 0.35, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height * 0.65 - UIScreen.main.bounds.size.width * 0.6)) let leftView = UIView(frame: CGRect(x: 0, y: UIScreen.main.bounds.size.height * 0.35, width: UIScreen.main.bounds.size.width * 0.2, height: UIScreen.main.bounds.size.width * 0.6)) let rightView = UIView(frame: CGRect(x: UIScreen.main.bounds.size.width * 0.8, y: UIScreen.main.bounds.size.height * 0.35, width: UIScreen.main.bounds.size.width * 0.2, height: UIScreen.main.bounds.size.width * 0.6)) topView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.4) bottomView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.4) leftView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.4) rightView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.4) let label = UILabel(frame: CGRect(x: 0, y: 10, width: UIScreen.main.bounds.size.width, height: 21)) label.textAlignment = .center label.font = UIFont.systemFont(ofSize: 14) label.textColor = UIColor.white label.text = "将二维码/条形码放入扫描框内,即自动扫描" bottomView.addSubview(label) self.addSubview(topView) self.addSubview(bottomView) self.addSubview(leftView) self.addSubview(rightView) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { if scanning == "start" { timer.fire() }else{ timer.invalidate() } } //让扫描线滚动 func moveScannerLayer(_ timer : Timer) { scanLine.frame = CGRect(x: 0, y: 0, width: self.barcodeView.frame.size.width, height: 12); UIView.animate(withDuration: 2) { self.scanLine.frame = CGRect(x: self.scanLine.frame.origin.x, y: self.scanLine.frame.origin.y + self.barcodeView.frame.size.height - 10, width: self.scanLine.frame.size.width, height: self.scanLine.frame.size.height); } } }
其中要用的 扫描时上下移动的 横线 QRCodeScanLine@2x.png 看附件
相关推荐
iOS原生二维码扫描,识别图片中的二维码信息。(赠送扫描动画,手电筒?功能让你夜里轻松扫描二维码)
RHScan - 实现二维码扫描功能,含各种UI、手势放大缩小镜头、仿微信扫码放大
swift-ios原生的二维码和条形码扫描.zip
现在越来越多的App添加了一个扫码的功能,方便而且很实用,但是目前对弈iOS来说有些比较知名的库(比如ZXingObjC)库都比较大,而实际上,在iOS 7.0之后系统在AVFoundation框架中就已经实现了对于二维码扫描和生成的...
二维码的生成和使用,包括去相册识别照片中的二维码和扫描二维码等
IOS应用源码之swift编写的二维码扫描demo.zip
QRCode 二维码创建扫描
IQEngUICamera:自定义相机、二维码扫描、人脸追踪
相机扫描二维码or长按图片识别二维码 系统定位 FMDB(目前出现一些问题正在修改) 键盘高度 ...后续还有本地播放音频、语音识别、模糊搜索
XBQRCodeHandler:支持扫描区域的二维码扫描
用于生成二维码制作和扫描
这是本人学习swift开发语言时重写的一个二维码扫描Demo,界面已经自定义。很适合swift新手参考,实现逻辑与ios7的完全相同,系统自带的库扫码效率很高。
帮助用Swift开发二维码的同学们,快速集成。觉得不错的可以关注我,不定期上传给你代码。
一个基于iOS系统API的二维码扫描小框架,继承QRCodeViewController即可
swift版利用AVFoundation进行二维码扫描
FanQRCodeScan(iOS8 )原生二维码条形码扫描和生成
ios原生的二维码和条形码扫描
SSJImage-Scan 图片压缩和二维码生成 or 扫描二维码
SWQRCode swift 版本,高仿微信扫一扫功能
使用swift coreImage框架实现 二维码扫描 从相册二维码相片扫描 以及 二维码生成与保存