Swift: Unable to detect linear type Barcodes
Make sure to include AVMetadataObjectTypeCode128Code [.code128]
when setting AVCaptureMetadataOutput
object types metadataObjectTypes
.
metadataOutput.metadataObjectTypes = [.qr, .ean13, .code128]
If you would like to allow all available metadata object types you can use AVCaptureMetadataOutput
property availableMetadataObjectTypes
which return all available types:
metadataOutput.metadataObjectTypes = metadataOutput.availableMetadataObjectTypes
If you would like to allow all of then except the faces which is the first one, you can drop the first element of the availableMetadataObjectTypes. Note that Apple might change the metadata elements order in the near future so it is better to manually select only the barcode types required by your app:
metadataOutput.metadataObjectTypes = Array(metadataOutput.availableMetadataObjectTypes.dropFirst())
Just a few notes on your actual code. You shouldn't check the elements count if it is equal to zero to check if it is empty. Array has a property called isEmpty exactly for that if !metadataObjects.isEmpty { //...
. Another option is if only the first element of the array is used I recommend using Array .first
instead of subscript [0]
which returns an optional element AVMetadataObject?
and AVMetadataObject.ObjectType
is redundant in your comparison:
if let object = metadataObjects.first as? AVMetadataMachineReadableCodeObject {
if object.type == .qr {
if let text = object.stringValue {
print(text)
session.stopRunning()
let alertVC = UIAlertController(title: "QR Code", message: text, preferredStyle: .alert)
alertVC.addAction(UIAlertAction(title: "Ok", style: .default) { _ in
self.session.startRunning()
})
present(alertVC, animated: true)
}
}
else
if let text = object.stringValue {
print("Other code detected: ", text)
}
}
Linear barcode generation in iPhone
I found this project easy to learn.
https://github.com/netshade/Cocoa-Touch-Barcodes
For a quick demonstration
There are several other barcode types this project renders, just make sure to import the header for your specific need.
Add the files from the Cocoa-Touch-Barcodes project into your Xcode project.
In the class you will use to help display the barcode
#import "UIImage-NKDBarcode.h"
#import "NKDEAN13Barcode.h"
-(void)showBarcode {
NKDBarcode * nkdbarcode = [[NKDEAN13Barcode alloc] initWithContent:@"1234567890123"];
UIImage * image = [UIImage imageFromBarcode:nkdbarcode];
// assume barcodeIV is an outlet to an imageview
[barcodeIV setImage:image];
}
SocketMobile Scanner : How to detect barcode or QR get scanned?
ISktScanDecodedData has two methods which you could use to determine the barcode type
getSymbologyID
- returns an intgetSymbologyName
- returns a string
I'd recommend using the first one, because the name is subject to change.
- (void) onDecodedDataResult:(long)result device:(DeviceInfo *)device decodedData:(ISktScanDecodedData*)decodedData
{
NSString * scannedText = [NSString stringWithUTF8String:(const char *)[decodedData getData]];
int symbologyId = [decodedData getSymbologyID];
if (symbologyId == ISktScanSymbology.id.kSktScanSymbologyQRCode) {
// do something
} else if (symbologyId == /* INSERT "BARCODE" SYMBOLOGY ID HERE */ ) {
// do something else
}
}
If by "barcode" you mean any linear barcode (a.k.a one-dimensional barcode), you will need to specify all the different types of linear barcodes in your code
iOS Barcode Efficient Scanner
I tried ZXing SDK first but it didn't work. I then tried ZBar SDK which worked just amazingly great.
If in future someone would need the same thing I am going to post the link which helped me make it work.
http://zbar.sourceforge.net/iphone/sdkdoc/tutorial.html
Related Topics
In Swift 3, How to Calculate the Factorial When the Result Becomes Too High
Function with Datatask Returning a Value
How to Create a Fixed-Size Array of Objects
How to Find the Number of Days in Given Month and Year Using Swift
String Convert to Int and Replace Comma to Plus Sign
Downloading and Caching Images from Url Asynchronously
Firebase Get User Uid by Email
Swift Turn a Country Code into a Emoji Flag via Unicode
Is Force Cast Really Bad and Should Always Avoid It
Cannot Resolve Swift Packages After 15Th March 2022 in Xcode
Multiple Workers in Swift Command Line Tool
Iboutlet and Ibaction in Swift
How to Make Class Methods/Properties in Swift
Override Func Error in Swift 2
How to Get Rgb Components from Color in Swiftui
Color Attribute Is Ignored in Nsattributedstring with Nslinkattributename