Barcode Anchor Jitter in Vision Pro due to Invalid enterprise api for barcode scanning Values

We’re using the enterprise API for spatial barcode/QR code scanning in the Vision Pro app, but we often get invalid values for the barcode anchor from the API, leading to jittery barcode positions in the UI. The code we’re using is attached below.

import SwiftUI import RealityKit import ARKit import Combine

struct ImmersiveView: View { @State private var arkitSession = ARKitSession() @State private var root = Entity() @State private var fadeCompleteSubscriptions: Set<AnyCancellable> = []

var body: some View {
    RealityView { content in
        content.add(root)
    }
    .task {
     // Check if barcode detection is supported; otherwise handle this case.
     guard BarcodeDetectionProvider.isSupported else { return }
     
     
     // Specify the symbologies you want to detect.
        let barcodeDetection = BarcodeDetectionProvider(symbologies: [.code128, .qr, .upce, .ean13, .ean8])
     
     
     do {
     try await arkitSession.requestAuthorization(for: [.worldSensing])
     
     try await arkitSession.run([barcodeDetection])
     
    print("Barcode scanning started")

     
     for await update in barcodeDetection.anchorUpdates where update.event == .added {
     
     let anchor = update.anchor
     
     // Play an animation to indicate the system detected a barcode.
     playAnimation(for: anchor)
     
     // Use the anchor's decoded contents and symbology to take action.
     print(
     """
     
     Payload: \(anchor.payloadString ?? "")
     Symbology: \(anchor.symbology)
     """)
     }
     } catch {
     // Handle the error.
     print(error)
     }
     }
     }
     
     // Define this function in ImmersiveView.
     func playAnimation(for anchor: BarcodeAnchor) {
     guard let scene = root.scene else { return }
     
     // Create a plane sized to match the barcode.
     let extent = anchor.extent
     let entity = ModelEntity(mesh: .generatePlane(width: extent.x, depth: extent.z), materials: [UnlitMaterial(color: .green)])
     entity.components.set(OpacityComponent(opacity: 0))
     
     // Position the plane over the barcode.
     entity.transform = Transform(matrix: anchor.originFromAnchorTransform)
     root.addChild(entity)
     
     // Fade the plane in and out.
     do {
     let duration = 0.5
     let fadeIn = try AnimationResource.generate(with: FromToByAnimation<Float>(
     from: 0,
     to: 1.0,
     duration: duration,
     isAdditive: true,
     bindTarget: .opacity)
     )
     let fadeOut = try AnimationResource.generate(with: FromToByAnimation<Float>(
     from: 1.0,
     to: 0,
     duration: duration,
     isAdditive: true,
     bindTarget: .opacity))
     
     let fadeAnimation = try AnimationResource.sequence(with: [fadeIn, fadeOut])
     
     _ = scene.subscribe(to: AnimationEvents.PlaybackCompleted.self, on: entity, { _ in
     // Remove the plane after the animation completes.
     entity.removeFromParent()
     }).store(in: &fadeCompleteSubscriptions)
     
     entity.playAnimation(fadeAnimation)
     } catch {
     print("Error")
     }
     }
}

Hi @AneeshNarayanan

When you observe the jittery barcode, is the barcode stationary or moving?

We're experiencing the issue in both scenarios: when the QR code is moving and when it's stationary, with the user's head moving instead.

Hi @AneeshNarayanan and @praveenfrom

The code you shared doesn't appear to respond to remove or update events. I wrote a variant that does and noticed jittering as the anchor's position updates. I suspect you have similar code.

A few notes:

  • BarcodeDetectionProvider is intended for detecting barcodes, not tracking them.
  • Because BarcodeDetectionProvider has a low refresh rate, use its transform to initialize the position of an "item found" indicator. This code you shared appears to do this.
  • Barcode anchors are removed when the barcode is out of view.

See Locating and decoding barcodes in 3D space for more information.

It sounds like you have a use case that requires barcode tracking. I encourage you to file an enhancement request using feedback assistant. Please explain your use case.

Barcode Anchor Jitter in Vision Pro due to Invalid enterprise api for barcode scanning Values
 
 
Q