标签:
1 // 1.创建Transform orginalImage的数据类型为CIImage 2 let scale = imageView.bounds.width / orginalImage.extent.width 3 let transform = CGAffineTransformMakeScale(scale, scale) 4 // 2.放大图片 5 let hdImage = orginalImage.imageByApplyingTransform(transform)
// 1.创建照片选择控制器 let ipc = UIImagePickerController() // 2.设置来源的类型 ipc.sourceType = .PhotoLibrary // 3.设置代理 ipc.delegate = self // 4.弹出控制器 presentViewController(ipc, animated: true, completion: nil) 在代理方法中实现 选中图片dismiss掉控制器 func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { //选中照片 imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage picker.dismissViewControllerAnimated(true, completion: nil) }
// 1.创建识别器 let detector = CIDetector(ofType: CIDetectorTypeQRCode, context: nil, options: nil) // 2.获取图片,并且将图片转成CIIImage let image = imageView.image! guard let ciImage = CIImage(image: image) else { return } // 3.识别图片中二维码 let features = detector.featuresInImage(ciImage) // 4.遍历数组中所有的元素 for f in features { // feature类型是CIFeature 要转换成二维码类型 CIQRCodeFeature guard let qrCodeF = f as? CIQRCodeFeature else { continue } //打印二维码的信息 print(qrCodeF.messageString) }
// 1.改变约束(原来约束为-240) scanViewBottomCons.constant = 240 // 2.执行动画 UIView.animateWithDuration(1.0) { UIView.setAnimationRepeatCount(MAXFLOAT) self.qrCodeView.layoutIfNeeded() }
// 1.创建捕捉会话 let session = AVCaptureSession() // 2.设置输入(摄像头) let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo) guard let input = try? AVCaptureDeviceInput(device: device) else { return } session.addInput(input) // 3.设置输出(Metadata) let output = AVCaptureMetadataOutput() // 设置代理 output.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue()) session.addOutput(output) // 设置output的输出的类型(该类型的设置必须在添加到session之后) output.metadataObjectTypes = [AVMetadataObjectTypeQRCode] // 4.添加预览图层(可以没有) let previewLayer = AVCaptureVideoPreviewLayer(session: session) previewLayer.frame = view.bounds view.layer.insertSublayer(previewLayer, atIndex: 0) // 5.开始扫描 session.startRunning()
func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) { guard let objc = metadataObjects.last as? AVMetadataMachineReadableCodeObject else { return } print(objc.stringValue) }
// 设置扫描的区域 let screenW = UIScreen.mainScreen().bounds.width let screenH = UIScreen.mainScreen().bounds.height let x : CGFloat = qrCodeView.frame.origin.x / screenW let y : CGFloat = qrCodeView.frame.origin.y / screenH let w : CGFloat = qrCodeView.frame.width / screenW let h : CGFloat = qrCodeView.frame.height / screenH output.rectOfInterest = CGRect(x: y, y: x, width: h, height: w)
注意:扫描区域的坐标系与屏幕的坐标系正好相反 ( 扫描区域x = 屏幕坐标系 y)
标签:
原文地址:http://www.cnblogs.com/xiaotian666/p/5802641.html