Search code examples
iosswiftbarcode

Is there a way to add MicroQR barcodes as an AVMetadataObject.ObjectType?


I need to scan MicroQR barcodes using the camera on iOS devices. Does anyone know of a native way to add the type? For example my constant for the builtins looks like so: Oh yeah I'm using Swift

let supportedCodeTypes = [AVMetadataObject.ObjectType.upce, AVMetadataObject.ObjectType.code39,
                      AVMetadataObject.ObjectType.code39Mod43, AVMetadataObject.ObjectType.code93,
                      AVMetadataObject.ObjectType.code128, AVMetadataObject.ObjectType.ean8,
                      AVMetadataObject.ObjectType.ean13, AVMetadataObject.ObjectType.aztec,
                      AVMetadataObject.ObjectType.pdf417, AVMetadataObject.ObjectType.itf14,
                      AVMetadataObject.ObjectType.dataMatrix, AVMetadataObject.ObjectType.interleaved2of5,
                      AVMetadataObject.ObjectType.qr]

I'm thinking that I might be able to implement it this way:

 let microqr = AVMetadataObject.ObjectType(rawValue: String)

Then I would be able to add it to my array, but I don't know what to place as the string for the rawValue:. Of course I am not even sure if this would work. I would really appreciate a push in the right direction. Thanks


Solution

  • No — the recognition engine(s) for AVCaptureMetadataOutput are in system software or hardware, and Apple provides no API for plugging in your own engine for new object types.

    You're correct that it is possible to construct a value of type AVMetadataObject.ObjectType using the init(rawValue: String) initializer and any arbitrary string. However, the AVCapture system can act only on the object types it knows — other values of the AVMetadataObject.ObjectType type are meaningless to API that use that type. (I'm not sure whether unknown ObjectType values will be ignored by such API or raise an error, but it's easy to check...)

    In other words, calling AVMetadataObject.ObjectType(rawValue: "someNewBarcodeStandard") doesn't teach AVCapture how to recognize that kind of barcode any more than AVMetadataObject.ObjectType(rawValue: "🐟") teaches it how to recognize fish.

    By the way, Swift type inference means you don't have to write out every type/constant name longhand:

    let supportedCodeTypes: [AVMetadataObject.ObjectType] = [.upce, .code39,
                      .code39Mod43, .code93,
                      .code128, .ean8,
                      .ean13, .aztec,
                      .pdf417, .itf14,
                      .dataMatrix, .interleaved2of5,
                      .qr]