美文网首页iOS-swiftSwift开发进阶
Swift4 条码扫描镜头拉近flashlight

Swift4 条码扫描镜头拉近flashlight

作者: GTMYang | 来源:发表于2019-03-13 12:39 被阅读15次

    GTMBarcodeScanner

    Swift 实现的条码扫描组件库

    logo.png

    说明

    • 支持设置不同风格的扫码动效
    • 自动检测光线强度,控制闪光开关的显示隐藏
    • 条码太小时候自动拉近镜头效果

    例子

    直接下载代码,里面Example里面有详细的使用实例代码


    1.png 2.png 3.png 4.png

    !

    安装

    Cocoapods方式

    Install Cocoapods if need be.

    $ gem install cocoapods
    

    Add GTMBarcodeScanner in your Podfile.

    use_frameworks!
    
    pod 'GTMBarcodeScanner'
    

    Then, run the following command.

    $ pod install
    

    常规方式

    Copy GTMBarcodeScanner folder to your project. That's it.

    Note: Make sure that all files in GTMBarcodeScanner included in Compile Sources in Build Phases.

    版本

    Vesrion 1.0

    This version requires Xcode 9.0 and Swift 4.

    使用帮助

    首先, import GTMBarcodeScanner.

    import GTMBarcodeScanner
    

    创建Scanner

    let scanner = BarcodeScanner.create(view: self.view)
    
    // 风格设置
    scanner.makeStyle { (make) in
        let color = UIColor.init(red: 255/255, green: 157/255, blue: 0/255, alpha: 1)
        make.positionUpVal(44)
        make.anglePosition(ScanViewStyle.AnglePosition.inner)
        make.angleLineWeight(5)
        make.angleLineLength(18)
        make.isShowRetangleBorder(true)
        make.width(280)
        make.height(180)
        make.retangleLineWeight(1/UIScreen.main.scale)
        make.animateType(ScanViewStyle.Animation.lineMove)
        make.colorOfAngleLine(color)
        make.colorOfRetangleLine(color)
        let c = UIColor(red: 255/255, green: 157/255, blue: 0/255, alpha: 0.5)
        make.colorOutside(c)
        make.soundSource(forName: "VoiceSearchOn", andType: "wav")
    }
    
    // 配置
    scanner.config { (make) in
        make.autoCloser(true)       // 自动拉近镜头
        make.caputureImage(true)    // 记录扫码的源图片
        make.printLog(true)         // 调试信息打印控制
    }
    
    // 设置代理
    scanner.delegate = self
    
    // 开始扫码
    scanner.start()
    

    代理对象

    public protocol GTMBarcodeCoreDelegate: class {
        func barcodeRecognized(result: BarcodeResult)
        func lightnessChange(needFlashButton: Bool)
        func barcodeForPhoto(result: BarcodeResult?)
    }
    

    风格设置

    public struct ScanViewStyle {
    
        /// 是否需要绘制扫码矩形框,默认YES
        public var isShowRetangle:Bool = true
        public var size = CGSize(width: 255, height: 255)
        public var positionUpVal: CGFloat = 44
        ///矩形框线条颜色,默认白色
        public var colorRetangleLine = UIColor.white
        ///4个角的颜色
        public var colorAngleLine = UIColor(red: 0.0, green: 167.0/255.0, blue: 231.0/255.0, alpha: 1.0)
        ///非识别区域颜色,默认 RGBA (0,0,0,0.5),范围(0--1)
        public var colorOutside = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.5)
    
        ///扫码区域的4个角类型
        public var anglePosition = AnglePosition.outer
    
    
        ///扫码区域4个角的宽度和高度
        public var angleW:CGFloat = 24.0
        public var angleH:CGFloat = 24.0
        ///扫码区域4个角的线条宽度,默认6,建议8到4之间
        public var angleLineWeight: CGFloat = 6
        public var retangleLineWeight: CGFloat = 1
    
        ///扫码动画效果: 线条或网格
        public var animateType = Animation.lineMove
    
        /// 动画效果的图像,如线条或网格的图像
        public var animateImage:UIImage?
        /// 动画 duration 值, 默认值 2.6
        public var animateDuration: TimeInterval = 2.6
    
        /// 扫描提示音资源文件
        public var soundSource: (name: String, type: String)?
    
    
        public init() { }
        
        public enum Animation {
            case lineMove
            case gridGrow
            case none
        }
        
        public enum AnglePosition {
            case inner
            case outer
            case on
        }
    }
    
    

    参考

    本库参考了swifScan

    参与开源

    欢迎提交 issue 和 PR,大门永远向所有人敞开。

    开源协议

    本项目遵循个人协议开源,具体请查看根目录下的 LICENSE 文件。

    相关文章

      网友评论

        本文标题:Swift4 条码扫描镜头拉近flashlight

        本文链接:https://www.haomeiwen.com/subject/zqpzpqtx.html