Vision Framework Barcode detection for iOS 11

后端 未结 3 1372
悲哀的现实
悲哀的现实 2021-01-02 12:32

I\'ve been implementing a test of the new Vision framework which Apple introduced in WWDC2017. I am specifically looking at the barcode detection - I\'ve been able to get af

3条回答
  •  误落风尘
    2021-01-02 13:07

    If Apple is not going to provide a library for this, something like the following will work:

    extension CIQRCodeDescriptor {
        var bytes: Data? {
            return errorCorrectedPayload.withUnsafeBytes { (pointer: UnsafePointer) in
                var cursor = pointer
    
                let representation = (cursor.pointee >> 4) & 0x0f
                guard representation == 4 /* byte encoding */ else { return nil }
    
                var count = (cursor.pointee << 4) & 0xf0
                cursor = cursor.successor()
                count |= (cursor.pointee >> 4) & 0x0f
    
                var out = Data(count: Int(count))
                guard count > 0 else { return out }
    
                var prev = (cursor.pointee << 4) & 0xf0
                for i in 2...errorCorrectedPayload.count {
                    if (i - 2) == count { break }
    
                    let cursor = pointer.advanced(by: Int(i))
                    let byte = cursor.pointee
                    let current = prev | ((byte >> 4) & 0x0f)
                    out[i - 2] = current
                    prev = (cursor.pointee << 4) & 0xf0
                }
                return out
            }
        }
    }
    

    And then

    String(data: descriptor.bytes!, encoding: .utf8 /* or whatever */)
    

提交回复
热议问题