struct BarcodeView: View {
@Environment(AppModel.self) var appModel
@State private var root = Entity()
@State private var arKitSession = ARKitSession()
var body: some View {
RealityView { content, attachments in
content.add(root)
if let barcodeAttachment = attachments.entity(for: "testView") {
barcodeAttachment.position = [0, 1, -1]
root.addChild(barcodeAttachment)
}
} placeholder: {
ProgressView()
} attachments: {
Attachment(id: "testView") {
Text("Looking for barcodes")
.bold()
.font(.largeTitle)
}
}
.task {
guard BarcodeDetectionProvider.isSupported else {
print("Barcode detection is not supported")
return
}
await arKitSession.requestAuthorization(for: [.cameraAccess, .worldSensing])
let barcodeDetection = BarcodeDetectionProvider(symbologies: [.code128, .code39, .code93, .ean8, .ean13, .qr, .upce])
do {
try await arKitSession.run([barcodeDetection])
for await update in barcodeDetection.anchorUpdates {
switch update.event {
case .added:
print("New barcode detected: \(update.anchor.payloadString ?? "")")
case .updated:
print("Barcode updated: \(update.anchor.payloadString ?? "")")
case .removed:
print("Barcode removed: \(update.anchor.payloadString ?? "")")
}
}
} catch {
print("Error running ARKit session: \(error.localizedDescription)")
}
}
}
}