react-native-vision-camera/package/ios/CameraView+CodeScanner.swift
Marc Rousavy 6640b72a00
feat: Code Scanner API (#1912)
* feat: CodeScanner JS API

* feat: iOS

* Use guard

* Format

* feat: Android base

* fix: Attach Surfaces

* Use isBusy var

* fix: Use separate Queue

* feat: Finish iOS types

* feat: Implement all other code types on Android

* fix: Call JS event

* fix: Pass codetypes on Android

* fix: iOS use Preview coordinate system

* docs: Add comments

* chore: Format code

* Update CameraView+AVCaptureSession.swift

* docs: Add Code Scanner docs

* docs: Update

* feat: Use lazily downloaded model on Android

* Revert changes in CameraPage

* Format

* fix: Fix empty QR codes

* Update README.md
2023-10-04 12:53:52 +02:00

46 lines
1.1 KiB
Swift

//
// CameraView+CodeScanner.swift
// VisionCamera
//
// Created by Marc Rousavy on 03.10.23.
// Copyright © 2023 mrousavy. All rights reserved.
//
import AVFoundation
import Foundation
extension CameraView: AVCaptureMetadataOutputObjectsDelegate {
public func metadataOutput(_: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from _: AVCaptureConnection) {
guard let onCodeScanned = onCodeScanned else {
return
}
guard !metadataObjects.isEmpty else {
return
}
// Map codes to JS values
let codes = metadataObjects.map { object in
var value: String?
if let code = object as? AVMetadataMachineReadableCodeObject {
value = code.stringValue
}
let frame = previewView.layerRectConverted(fromMetadataOutputRect: object.bounds)
return [
"type": object.type.descriptor,
"value": value as Any,
"frame": [
"x": frame.origin.x,
"y": frame.origin.y,
"width": frame.size.width,
"height": frame.size.height,
],
]
}
// Call JS event
onCodeScanned([
"codes": codes,
])
}
}