swift4.2 史上最好用的条码扫描库

GTMBarcodeScanner是Swift实现的条码扫描组件库,支持微信、支付宝风格的扫码动效,自动调节光线并开启闪光灯,条码小会自动拉近镜头,可自定义扫码音效。提供Cocoapods和常规安装方式,适用于Xcode 9.0及Swift 4.2。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 文件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值