react-native-vision-camera/ios/CameraView+CodeScanning.swift

49 lines
1.5 KiB
Swift
Raw Normal View History

2021-02-19 08:28:05 -07:00
//
// CameraView+CodeScanning.swift
// Cuvent
//
// Created by Marc Rousavy on 16.12.20.
// Copyright © 2020 Facebook. All rights reserved.
//
import AVFoundation
import Foundation
extension CameraView: AVCaptureMetadataOutputObjectsDelegate {
func metadataOutput(_: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from _: AVCaptureConnection) {
2021-02-25 06:03:52 -07:00
if metadataObjects.isEmpty {
2021-02-19 08:28:05 -07:00
return
}
let objects = metadataObjects.map { (object) -> [String: Any]? in
guard let object = object as? AVMetadataMachineReadableCodeObject else {
return nil
}
return [
"code": object.stringValue as Any,
"type": object.type.descriptor,
"bounds": [
"minX": object.bounds.minX,
"minY": object.bounds.minY,
"maxX": object.bounds.maxX,
"maxY": object.bounds.maxY,
"width": object.bounds.width,
"height": object.bounds.height,
],
]
}
invokeOnCodeScanned(codes: objects)
}
private func invokeOnCodeScanned(codes: [[String: Any]?]) {
guard let onCodeScanned = self.onCodeScanned else {
2021-02-25 06:05:20 -07:00
ReactLogger.log(level: .warning,
message: "onCodeScanned was invoked with no listeners. " +
"This means that the Camera is unnecessarily scanning codes. This indicates a memory leak.",
alsoLogToJS: true)
2021-02-19 08:28:05 -07:00
return
}
onCodeScanned(["codes": codes])
}
}