'How to set up event handling with prebuild class
I am very new to C# and try to use Microsoft.MixedReality.QR for reading some QRCodes with the Hololense 2 and have problems accessing the events of the QRCodeWatcher. According to the API, the QRCodeWatcher class has 4 events but it seems that these are never triggered. (QR_Added, QR_Updated,... are never called). Am I setting up something wrong?
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Microsoft.MixedReality.QR;
using QRTracking;
namespace QRTracking {
public class Detector: MonoBehaviour {
public TextMesh debugScreen;
public bool IsSupported {get; private set; }
private QRCodeWatcher qrTracker;
private bool QRstarted=false;
private QRCodeWatcherAccessStatus accessStatus;
// Start is called before the first frame update
async protected virtual void Start() {
debugScreen.text="Hello";
if (QRCodeWatcher.IsSupported()) {
try {
accessStatus=await QRCodeWatcher.RequestAccessAsync();
debugScreen.text=accessStatus.ToString();
}
catch {
debugScreen.text="No World";
}
}
}
// Update is called once per frame
void Update() {
if (accessStatus.ToString()=="Allowed") {
if (QRstarted==false) {
debugScreen.text="Setup Tracking";
SetupQRTracking();
QRstarted=true;
}
}
}
private void SetupQRTracking() {
qrTracker=new QRCodeWatcher();
qrTracker.Added+=QR_Added;
qrTracker.Updated+=QR_Updated;
qrTracker.Removed+=QR_Removed;
qrTracker.EnumerationCompleted+=QR_Enum;
qrTracker.Start();
debugScreen.text="Tracking started";
}
private void QR_Added(object sender, QRCodeAddedEventArgs args) {
debugScreen.text="Added";
}
private void QR_Updated(object sender, QRCodeUpdatedEventArgs args) {
debugScreen.text="Updated";
}
private void QR_Removed(object sender, QRCodeRemovedEventArgs args) {
debugScreen.text="Removed";
}
private void QR_Enum(object sender, object e) {
debugScreen.text="Enum";
}
}
}
(While the script is running the HoloLense is able to read the content of a QRCode. The debugScreen is a TextMesh so that I can see the text while wearing the HoloLense.)
Solution 1:[1]
Workaround to get only the recently seen QR codes to mimic the update event:
private IReadOnlyList<QRCode> detectedQR;
private DateTimeOffset timestamp;
private int timetolerance = 1; // seconds
detectedQR = qrTracker.GetList();
timestamp = DateTimeOffset.Now;
foreach (var QR in detectedQR)
{if ((timestamp - QR.LastDetectedTime ).Duration() < TimeSpan.FromSeconds(timetolerance))
{
//Do stuff with QR Codes seen in the last second
}
}
However, with too many QR codes, performance could suffer.
Solution 2:[2]
Hi I’m Wayne Wang from the Microsoft for Founders Hub team!
We do have an example from my colleague on your current context.
Seems the implementation you may need is here: https://github.com/chgatla-microsoft/QRTracking/blob/master/SampleQRCodes/Assets/Scripts/QRCodesManager.cs
it was little bit of old but I believe the Interfaces and pipelines still the same
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | KuniK |
Solution 2 | Wayne Wang - MSFT |