SWIFT Scan QRCode

简介: SWIFT中扫描QRCode代码如下,照着敲一次再看下API的注释应该就没问题了。 import UIKit import Foundation import AVFoundation class ViewController: UIViewController,AVCaptureM...

SWIFT中扫描QRCode代码如下,照着敲一次再看下API的注释应该就没问题了。

import UIKit
import Foundation
import AVFoundation

class ViewController: UIViewController,AVCaptureMetadataOutputObjectsDelegate,UIAlertViewDelegate {

    let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
    let session = AVCaptureSession()
    var layer:AVCaptureVideoPreviewLayer?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.grayColor()
        let lblIntroduction = UILabel(frame: CGRectMake(10, 80, 300, 50))
        lblIntroduction.text = "Scan QRCode"
        lblIntroduction.textColor = UIColor.whiteColor()
        self.view.addSubview(lblIntroduction)
        
        //let imageView = UIImageView(frame: CGRectMake(10, 140, 300, 300))
        //imageView.image = UIImage(named: "pick_bg")
        //self.view.addSubview(imageView)
        
//add scan line effect
        let line = UIView(frame: CGRectMake(20, 150, 280, 1))
        line.backgroundColor = UIColor.yellowColor()
        self.view.addSubview(line)
        
        UIView.animateWithDuration(2.5, delay: 0, options: UIViewAnimationOptions.Repeat, animations: { () -> Void in
              line.frame = CGRectMake(20, 430, 280, 1)
            }, completion: nil)

setupCamera() } func setupCamera() { //An AVCaptureSession preset suitable for medium quality output self.session.sessionPreset = AVCaptureSessionPresetMedium //AVCaptureSessionPresetHigh var error:NSError? //This step is to ask device where it can use back camera let input = AVCaptureDeviceInput(device: device, error: &error) if error != nil { println(error?.description) return } if session.canAddInput(input) { session.addInput(input) } layer = AVCaptureVideoPreviewLayer(session: session) layer?.videoGravity = AVLayerVideoGravityResizeAspectFill layer?.frame = CGRectMake(20, 150, 280, 280) self.view.layer.insertSublayer(self.layer!, atIndex: 0) let output = AVCaptureMetadataOutput() output.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue()) if session.canAddOutput(output) { session.addOutput(output) output.metadataObjectTypes = [AVMetadataObjectTypeQRCode] } session.startRunning() } func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) { var stringValue:String? if metadataObjects.count > 0 { var metadataObject = metadataObjects[0] as! AVMetadataMachineReadableCodeObject stringValue = metadataObject.stringValue } self.session.stopRunning() let alertView = UIAlertView() alertView.message = stringValue alertView.addButtonWithTitle("Sure") alertView.delegate = self alertView.cancelButtonIndex = 0 alertView.show() } func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) { session.startRunning() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }

 

 

refer to :https://github.com/hitourlee/Swift_QRCode

目录
相关文章
|
Go 索引
Go 1.21.0 新增标准库 slices 和 maps 详解
Go 1.21.0 新增标准库 slices 和 maps 详解
101 0
|
安全 Swift
Swift 中的 KeyPath
Swift 中的 反斜杠点 (\.xxx)到底是个啥
|
Swift
Swift中AnyObject、Any、AnyClass、T.self、T.Type、type(of:)、Self的使用和区别
Swift中AnyObject、Any、AnyClass、T.self、T.Type、type(of:)、Self的使用和区别
218 0
|
Android开发
【Android应用开发】 Universal Image Loader ( 使用简介 | 示例代码解析 )(三)
【Android应用开发】 Universal Image Loader ( 使用简介 | 示例代码解析 )(三)
128 0
【Android应用开发】 Universal Image Loader ( 使用简介 | 示例代码解析 )(三)
|
测试技术 Swift 索引
Swift:Array+Extension
Swift:Array+Extension
209 0
|
测试技术 Swift
Swift:UIImage+Extension
Swift:UIImage+Extension
734 0
|
测试技术 Swift
Swift:FileManager+Extension
Swift:FileManager+Extension
592 0
|
测试技术 Swift
Swift:UITextView+Extension
Swift:UITextView+Extension
253 0
|
存储 编解码 Dart
Flutter Raw Image Provider
Flutter 中的 Image Widget 内置支持 file、network、memory三种形式的文件。 但这几种都只支持常规的经过压缩后的图片文件或二进制数据,如jpg、png、webp文件等。并没有支持原始的rgba 二进制数据。 这里说的原始二进制数据是指图像的每个像素的色彩值所组成的字节数组。一张图有宽x高个像素点,一个像素点的色彩值用32bit来存储,分为4个通道,每个通道各占用8bit,分别为红、绿、蓝、透明度(RGBA),这个数组就是每个像素点色彩值的集合,dart 中一般用Uint8List。
401 0
|
前端开发 Android开发
React Native之react-native bundle --platform android --dev false --entry-file index.js --bundle失败
React Native之react-native bundle --platform android --dev false --entry-file index.js --bundle失败
151 0