ScanCode

open class ScanCode : NSObject, AVCaptureMetadataOutputObjectsDelegate, AVCaptureVideoDataOutputSampleBufferDelegate, @unchecked Sendable

二维码、条形码扫描,默认仅开启二维码

不建议同时开启二维码和条形码,因为开启后条形码很难识别且只有中心位置可识别。 默认二维码类型示例:[.qr] 默认条形码类型示例:[.code39, .code39Mod43, .code93, .code128, .ean8, .ean13, .upce, .interleaved2of5]

SGQRCode

Accessor

  • 默认二维码类型,可自定义

    Declaration

    Swift

    public static var metadataObjectTypesQRCode: [AVMetadataObject.ObjectType] { get set }
  • 默认条形码类型,可自定义

    Declaration

    Swift

    public static var metadataObjectTypesBarcode: [AVMetadataObject.ObjectType] { get set }
  • 预览视图,必须设置(传外界控制器视图)

    Declaration

    Swift

    @MainActor
    open var preview: UIView? { get set }
  • 扫描区域,以屏幕右上角为坐标原点,取值范围:0~1,默认为整个屏幕

    Declaration

    Swift

    open var rectOfInterest: CGRect { get set }
  • 视频缩放因子,默认同系统(捕获内容)

    Declaration

    Swift

    open var videoZoomFactor: CGFloat { get set }
  • 扫描二维码数据代理

    Declaration

    Swift

    open weak var delegate: ScanCodeDelegate? { get set }
  • 扫描到结果时是否自动停止,停止后需调用startRunning才会继续扫描,默认false

    Declaration

    Swift

    open var stopWhenScanned: Bool
  • 采样缓冲区代理

    Declaration

    Swift

    open weak var sampleBufferDelegate: ScanCodeSampleBufferDelegate? { get set }
  • 扫描二维码回调句柄

    Declaration

    Swift

    open var scanResultBlock: (@Sendable (String?) -> Void)? { get set }
  • 扫描二维码光线强弱回调句柄

    Declaration

    Swift

    open var scanBrightnessBlock: (@Sendable (CGFloat) -> Void)? { get set }
  • 元对象类型,默认仅开启二维码

    Declaration

    Swift

    open lazy var metadataObjectTypes: [AVMetadataObject.ObjectType] { get set }
  • 会话预制,默认自动设置

    Declaration

    Swift

    open lazy var sessionPreset: AVCaptureSession.Preset { get set }

Lifecycle

  • Undocumented

    Declaration

    Swift

    override public init()

Public

  • 配置扫描设备,比如自动聚焦等

    Declaration

    Swift

    open func configCaptureDevice(_ block: ((AVCaptureDevice) -> Void)?)
  • 开启扫描

    Declaration

    Swift

    open func startRunning()
  • 停止扫描

    Declaration

    Swift

    open func stopRunning()

AVCaptureMetadataOutputObjectsDelegate

  • Undocumented

    Declaration

    Swift

    open func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection)

AVCaptureVideoDataOutputSampleBufferDelegate

  • Undocumented

    Declaration

    Swift

    open func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection)

Torch

  • 手电筒是否已激活

    Declaration

    Swift

    open class func isTorchActive() -> Bool
  • 打开手电筒

    Declaration

    Swift

    open class func turnOnTorch()
  • 关闭手电筒

    Declaration

    Swift

    open class func turnOffTorch()
  • 检测后置摄像头是否可用

    Declaration

    Swift

    @MainActor
    open class func isCameraRearAvailable() -> Bool
  • 播放音效

    Declaration

    Swift

    open class func playSoundEffect(_ file: String)

Read

  • 读取图片中的二维码,主线程回调

    Declaration

    Swift

    open class func readQRCode(_ image: UIImage?, compress: Bool = true, completion: @escaping @MainActor @Sendable (String?) -> Void)

    Parameters

    image

    图片

    compress

    是否按默认算法压缩图片,默认true,图片过大可能导致闪退,建议开启

    completion

    回调方法,读取成功时,回调参数 result 等于二维码数据,否则等于 nil

  • 读取图片中的条形码/二维码,主线程回调

    Declaration

    Swift

    open class func readBarcode(_ image: UIImage?, compress: Bool = true, completion: @escaping @MainActor @Sendable (String?) -> Void)

    Parameters

    image

    图片

    compress

    是否按默认算法压缩图片,默认true,图片过大可能导致闪退,建议开启

    completion

    回调方法,读取成功时,回调参数 result 等于条形码/二维码数据,否则等于 nil

Generate

  • 生成二维码

    Declaration

    Swift

    open class func generateQrcode(
        data: String,
        size: CGFloat,
        color: UIColor = .black,
        backgroundColor: UIColor = .white
    ) -> UIImage?

    Parameters

    data

    二维码数据

    size

    二维码大小

    color

    二维码颜色,默认黑色

    backgroundColor

    二维码背景颜色,默认白色

    Return Value

    二维码图片

  • 生成带 logo 的二维码

    Declaration

    Swift

    open class func generateQrcode(
        data: String,
        size: CGFloat,
        logoImage: UIImage?,
        ratio: CGFloat = 0.25,
        logoImageCornerRadius: CGFloat = 5,
        logoImageBorderWidth: CGFloat = 5,
        logoImageBorderColor: UIColor? = .white
    ) -> UIImage?

    Parameters

    data

    二维码数据

    size

    二维码大小

    logoImage

    logo

    ratio

    logo 相对二维码的比例,默认0.25(取值范围 0.0 ~ 1.0f)

    logoImageCornerRadius

    logo 外边框圆角,默认5

    logoImageBorderWidth

    logo 外边框宽度,默认5

    logoImageBorderColor

    logo 外边框颜色,默认白色

    Return Value

    二维码图片

  • 生成code128条形码,无空白区域

    Declaration

    Swift

    open class func generateBarcode(
        data: String,
        size: CGSize,
        color: UIColor = .black,
        backgroundColor: UIColor = .white
    ) -> UIImage?

    Parameters

    data

    二维码数据

    size

    二维码大小

    color

    二维码颜色,默认黑色

    backgroundColor

    二维码背景颜色,默认白色

    Return Value

    条形码图片