Using CIDetector to scan bar Codes (1 and 2 dimentional)
Till now apple's AVFoundation Framework does not provide a way to scan bar codes picked from image library.
So I solved my issue by using AVFoundation Framework for scanning QR and Bar code in live camera, whereas when user pics photos from gallery I use ZBar Framework to scan qr and bar codes.
Scanning barcode from UIImage natively (i.e., not using ZBar)
An option available now is MLKit from Firebase.
Add Firebase to your project after setting up your project in their console https://firebase.google.com
I use cocoapods to manage dependencies, if you do then add this to your Podfile and pod update
:
pod 'Firebase/Core'
pod 'Firebase/MLVision'
pod 'Firebase/MLVisionBarcodeModel'
Here's a demo view controller implementation in which you pick a barcode image from your photo library to be scanned by ML kit, and the results are printed in a label.
// (Don't forget to ask for Photos access by including this in your info.plist)
<key>NSPhotoLibraryUsageDescription</key>
<string>Enable photo library access to select a photo from your library.</string>
import Firebase
class MyDemoViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
lazy var vision = Vision.vision()
@IBOutlet var textLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func selectImage(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.savedPhotosAlbum){
print("Button capture")
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .savedPhotosAlbum;
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
guard let image = info[UIImagePickerControllerOriginalImage] as? UIImage else {
return
}
dismiss(animated: true, completion: {
self.checkForCodeInImage(image: image)
})
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion:nil)
}
internal func checkForCodeInImage(image: UIImage) {
let detector = vision.barcodeDetector(options: VisionBarcodeDetectorOptions(formats: .all))
let vImage = VisionImage(image: image)
detector.detect(in: vImage) { features, error in
guard error == nil, let barcodes = features, barcodes.isEmpty == false else {
DispatchQueue.main.async {
self.textLabel.text = "No code found in selected image."
}
return
}
var text = ""
for barcode in barcodes {
guard let rawValue = barcode.rawValue else {
continue
}
let corners = barcode.cornerPoints
let displayValue = barcode.displayValue
print("Corners: \(String(describing: corners))")
print("Found: \(String(describing: displayValue))")
text.append(rawValue)
text.append("\n\n")
// let valueType = barcode.valueType
// switch valueType {
// case .wiFi:
// let ssid = barcode.wifi!.ssid
// let password = barcode.wifi!.password
// let encryptionType = barcode.wifi!.type
// case .URL:
// let title = barcode.url!.title
// let url = barcode.url!.url
// default:
// // See API reference for all supported value types
// break
// }
}
DispatchQueue.main.async {
self.textLabel.text = text
}
}
}
}
Here's a link to their documentation: https://firebase.google.com/docs/ml-kit/ios/read-barcodes
Zbar barcode reader is not properly scaning code128 format barcodes for ipod touch 4G
Regarding the iPod Touch 4G issue this is most likely a hardware limitation. The device only has a 0.7MP resolution and no autofocus which makes scanning barcodes much more difficult.
One option is to limit these devices from downloading the app using UIRequiredDeviceCapabilities.
You could try improving frame resolution by filtering first with a sharpen filter before passing to ZBar. Have a look at:
- Core Image - https://developer.apple.com/library/ios/documentation/GraphicsImaging/Conceptual/CoreImaging/ci_intro/ci_intro.html
- GPUImage - https://github.com/BradLarson/GPUImage
Related Topics
How to Change/Modify The Displayed Title of an Nspopupbutton
Swift Compound Arithmetic Operation Error
Swiftui - Form with Error Message on Button Press and Navigation
How to Bind My Array Controller to My Core Data Model
Swift 4 Base64 String to Data Not Working Due to String Containing "Incomplete" Emoji
Swiftui List .Ondelete: Index Out of Range
Firebase - Swift 4: Message Sent But Not Received
How to Count Number of Sprites Swift
Swift Generics and Protocol Extensions
How to Download Video Urlstring from Firebase Database Not Storage in Swift
Difference Between Orientation and Rotation in Scnnode
Swift: Gradient Splits on Rotation
Understanding UIviewrepresentable
Touch Sprite, Make It Jump Up Then Fall Down Again(Repeat as Many Times as Spritenode Is Tapped.)
Can't Hide Status Bar in Avplayerviewcontroller's Portrait Mode