项目终于不需要支持iOS6了(泪崩),在二维码扫描这一块,能够完全的放弃ZXing
库,改用系统的AVFoundation
了,拿swift
写了个Demo,效果如下:
github地址:点这里
有关AVFoundation
和Core Image
(滤镜等),可以先看看objc.io第21期和第23期的有关介绍.
// 初始化视频捕获 private func initCapture() { // 代表抽象的硬件设备,这里传入vIDeo let captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVIDeo) var error: NSError? // 输入流 var captureinput = AVCaptureDeviceinput.deviceinputWithDevice(captureDevice,error: &error) as? AVCaptureDeviceinput if (error != nil && captureinput == nil) { let errorAlert = UIAlertController(Title: "提醒",message: "请在iPhone的\"设置-隐私-相机\"选项中,允许XXX访问您的相机",preferredStyle: .Alert) errorAlert.addAction(UIAlertAction(Title: "确定",style: UIAlertActionStyle.Default,handler: nil)) self.presentVIEwController(errorAlert,animated: true,completion: nil) } else { // input和output的桥梁,它协调着intput到output的数据传输.(见字意,session-会话) captureSession = AVCaptureSession() captureSession!.addinput(captureinput) // 输出流 let captureMetadataOutput = AVCaptureMetadataOutput() // 限制扫描区域http://blog.csdn.net/lc_obj/article/details/41549469 captureMetadataOutput.rectOfInterest = CGRectMake(128.0/ScreenWH.screenHeight,(ScreenWH.screenWIDth - 280.0)/ScreenWH.screenWIDth * 2.0,280.0/ScreenWH.screenHeight,280.0/ScreenWH.screenWIDth) captureSession!.addOutput(captureMetadataOutput) // 添加的队列按规定必须是串行 captureMetadataOutput.setMetadataObjectsDelegate(self,queue: dispatch_get_main_queue()) // 指定信息类型,QRCode,你懂的 captureMetadataOutput.MetadataObjectTypes = [AVMetadataObjectTypeQRCode] // 用这个预览图层和图像信息捕获会话(session)来显示视频 vIDeoPrevIEwLayer = AVCaptureVIDeoPrevIEwLayer(session: captureSession!) vIDeoPrevIEwLayer!.vIDeoGravity = AVLayerVIDeoGravityResizeAspectFill vIDeoPrevIEwLayer!.frame = vIEw.bounds vIEw.layer.addSublayer(vIDeoPrevIEwLayer!) } }
PS:LZ用了下微信和新浪微博的扫一扫,发现那个扫描框是忽悠人的,也就是你没拿它对准二维码,只要二维码进入手机摄像头范围,就能够解码成功….囧
所以LZ在代码中做了一个扫描区域的限制(感觉蛮无聊的)
// MARK: - AVCaptureMetadataOutputObjectsDelegate func captureOutput(captureOutput: AVCaptureOutput!,dIDOutputMetadataObjects MetadataObjects: [AnyObject]!,fromConnection connection: AVCaptureConnection!) { if MetadataObjects == nil || MetadataObjects.count == 0 { captureVIEw!.frame = CGRectZero return } // 刷取出来的数据 for MetadataObject in MetadataObjects { if MetadataObject.type == AVMetadataObjectTypeQRCode { let Metadata = MetadataObject as! AVMetadataMachineReadableCodeObject // 元数据对象就会被转化成图层的坐标 let codeCoord = vIDeoPrevIEwLayer!.transformedMetadataObjectForMetadataObject(Metadata) as! AVMetadataMachineReadableCodeObject captureVIEw!.frame = codeCoord.bounds if Metadata.stringValue != nil { println("\(Metadata.stringValue)") self.captureSession!.stopRunning() let successAlert = UIAlertController(Title:"提示",message:"是否打开" + Metadata.stringValue,preferredStyle: .Alert) successAlert.addAction(UIAlertAction(Title:"取消",style: .Default,handler: { (_) -> VoID in self.stopCapture() })) successAlert.addAction(UIAlertAction(Title:"确定",handler: { (_) -> VoID in if Metadata.stringValue.lowercaseString.hasPrefix("http") { UIApplication.sharedApplication().openURL(NSURL(string: Metadata.stringValue)!) self.navigationController!.popVIEwControllerAnimated(true) } })) self.presentVIEwController(successAlert,animated: true,completion: nil) } } } }
数据转换AVMetadataMachineReadableCodeObject
对应二维码.
// MARK: - Private Methods private func createQRForString(qrString: String?,qrImagename: String?) -> UIImage?{ if let sureQRString = qrString { let stringData = sureQRString.dataUsingEnCoding(NSUTF8StringEnCoding,allowLossyConversion: false) // 创建一个二维码的滤镜 let qrFilter = CIFilter(name: "CiqrCodeGenerator") qrFilter.setValue(stringData,forKey: "inputMessage") qrFilter.setValue("H",forKey: "inputCorrectionLevel") let qrCIImage = qrFilter.outputimage // 创建一个颜色滤镜,黑白色 let colorFilter = CIFilter(name: "CIFalsecolor") colorFilter.setDefaults() colorFilter.setValue(qrCIImage,forKey: "inputimage") colorFilter.setValue(CIcolor(red: 0,green: 0,blue: 0),forKey: "inputcolor0") colorFilter.setValue(CIcolor(red: 1,green: 1,blue: 1),forKey: "inputcolor1") // 返回二维码image let codeImage = UIImage(CIImage: colorFilter.outputimage.imageByApplyingtransform(CGAffinetransformMakeScale(5,5))) // 通常,二维码都是定制的,中间都会放想要表达意思的图片 if let iconImage = UIImage(named: qrImagename!) { let rect = CGRectMake(0,0,codeImage!.size.wIDth,codeImage!.size.height) UIGraphicsBeginImageContext(rect.size) codeImage!.drawInRect(rect) let avatarSize = CGSizeMake(rect.size.wIDth * 0.25,rect.size.height * 0.25) let x = (rect.wIDth - avatarSize.wIDth) * 0.5 let y = (rect.height - avatarSize.height) * 0.5 iconImage.drawInRect(CGRectMake(x,y,avatarSize.wIDth,avatarSize.height)) let resultimage = UIGraphicsGetimageFromCurrentimageContext() UIGraphicsEndImageContext() return resultimage } return codeImage } return nil }
如图
结尾:AVFoundation
这个框架特别的强大,也可以用它来写自定义相机,拍照和录制视频等
以上是内存溢出为你收集整理的Swift AVFoundation 二维码扫描和生成全部内容,希望文章能够帮你解决Swift AVFoundation 二维码扫描和生成所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)