- December 17, 2024
- Mins Read
RSBarcodes allows you to read 1D and 2D barcodes using the metadata scanning capabilities introduced with iOS 7 and generate the same set of barcode images for displaying and sharing. Now implemented in Swift.
To add a package dependency to your Xcode project, select File > Swift Packages > Add Package Dependency and enter https://github.com/yeahdongcn/RSBarcodes_Swift to the text field.
Simply add the following lines to your Podfile
:
# required by Cocoapods 0.36.0.rc.1 for Swift Pods
use_frameworks!
pod ‘RSBarcodes_Swift’, ‘~> 5.2.0’
You will need to import RSBarcodes_Swift manually in the ViewController file after creating the file using wizard.
(CocoaPods v0.36 or later required. See this blog post for details.)
Simply add the following line to your Cartfile
:
github “yeahdongcn/RSBarcodes_Swift” >= 5.2.0
You will need to import RSBarcodes_Swift manually in the ViewController file after creating the file using wizard.
https://github.com/yeahdongcn/RSBarcodes_Swift
in the “Choose Package Repository” dialog.cd
-ing into your top-level project directory, and entering the command git submodule add https://github.com/yeahdongcn/RSBarcodes_Swift.git
RSBarcodes_Swift
folder, and drag RSBarcodes.xcodeproj
into the file navigator of your app project.RSBarcodes.framework
.+
button at the top left of the panel and select “New Copy Files Phase”. Rename this new phase to “Copy Frameworks”, set the “Destination” to “Frameworks”, and add RSBarcodes.framework
.How to Use Generator and How to Use Reader
First, import the following frameworks:
import RSBarcodes_Swift
import AVFoundation
Then, use the generator to generate a barcode. For example:
RSUnifiedCodeGenerator.shared.generateCode(“2166529V”, machineReadableCodeObjectType: AVMetadataObjectTypeCode39Code)
It will generate a UIImage
instance if the 2166529V
is a valid code39 string. For AVMetadataObjectTypeCode128Code
, you can change useBuiltInCode128Generator
to false
to use my implementation (AutoTable for code128).
P.S. There are 4 tables for encoding a string to code128, TableA
, TableB
, TableC
and TableAuto
; the TableAuto
is always the best choice, but if one has specific requirements, try this:
RSCode128Generator(codeTable: .A).generateCode(“123456”, machineReadableCodeObjectType: AVMetadataObjectTypeCode128Code)
Example of these simple calls can be found in the test project.
The following are steps to get the barcode reader working:
File
-> New
-> File
iOS
click source
and make sure Cocoa Touch Class
is selected and hit Next
.ScanViewController
from now on.RSCodeReaderViewController
and ensure the language is Swift
and hit Next
and then Create
UIViewController
onto it.ScanViewController
ScanViewController.swift
file add the following code into the viewDidLoad()
or some place more suitable for you:
override func viewDidLoad() {
super.viewDidLoad()
self.focusMarkLayer.strokeColor = UIColor.red.cgColor
self.cornersLayer.strokeColor = UIColor.yellow.cgColor
self.tapHandler = { point in
print(point)
}
self.barcodesHandler = { barcodes in
for barcode in barcodes {
print(“Barcode found: type=” + barcode.type + ” value=” + barcode.stringValue)
}
}
}
If you want to ignore some code types (for example, AVMetadataObjectTypeQRCode
), add the following lines:
let types = NSMutableArray(array: self.output.availableMetadataObjectTypes)
types.remove(AVMetadataObjectTypeQRCode)
self.output.metadataObjectTypes = NSArray(array: types)
To validate codes:
let isValid = RSUnifiedCodeValidator.shared.isValid(code, machineReadableCodeObjectType: AVMetadataObjectTypeEAN13Code)
Use RSAbstractCodeGenerator.resizeImage(source: UIImage, scale: CGFloat)
to scale the generated image.
Use RSAbstractCodeGenerator.resizeImage(source: UIImage, targetSize: CGSize, contentMode: UIViewContentMode)
to fill/fit the bounds of something to the best capability and don’t necessarily know what scale is too much to fill/fit, or if the UIImageView
itself is flexible.
The Swift Programming Language 中文版
Online version generated using GitBook
A vertical stackview which takes subviews with different widths and adds them to it's rows with paddings, spacings etc.
AudioManager is a Swift package that provides a modular and easy-to-use interface for implementing audio feedback in your applications. It ...