Unreal 中的 QR 代碼QR codes in Unreal


HoloLens 2 可以使用網路攝影機查看世界空間中的 QR 代碼,這會在每個代碼的真實世界位置中使用座標系統將其呈現為全息投影。The HoloLens 2 can see QR codes in world space using the webcam, which renders them as holograms using a coordinate system at each code's real-world position. 除了單一 QR 代碼外,HoloLens 2 還可以在相同位置的多個裝置上呈現全像投影,以建立共用體驗。In addition to single QR codes, HoloLens 2 can also render holograms in the same location on multiple devices to create a shared experience. 確定您遵循將 QR 代碼新增至應用程式的最佳作法:Make sure you're following the best practices for adding QR codes to your applications:

  • 寧靜區域Quiet zones
  • 光源和底圖Lighting and backdrop
  • 大小、距離和角度位置Size, distance, and angular position

將 QR 代碼放在您的應用程式時,請特別注意環境考量Pay special attention to the environment considerations when QR codes are being placed in your app. 您可以在其中每一個主題上找到詳細資訊,以及在主要 QR 代碼追蹤文件中找到如何下載所需 NuGet 套件的指示。You can find more information on each of these topics and instructions on how to download the required NuGet package in the main QR code tracking document.

啟用 QR 偵測Enabling QR detection

因為 HoloLens 2 需要使用網路攝影機來查看 QR 代碼,所以您必須在專案設定中將其啟用:Since the HoloLens 2 needs to use the webcam to see QR codes, you'll need to enable it in the project settings:

  • 開啟 [編輯] > [專案設定]捲動至 [平台] 區段,然後按一下 [HoloLens]。Open Edit > Project Settings, scroll to the Platforms section and click HoloLens.
    • 展開 [功能] 區段,並勾選 [網路攝影機]。Expand the Capabilities section and check Webcam.  

您也必須新增 ARSessionConfig 資產,來加入 QR 代碼追蹤。You'll also need to opt into QR code tracking by adding an ARSessionConfig asset.

在使用之前,您應藉由呼叫 UHoloLensARFunctionLibrary::StartCameraCapture()來手動啟用追蹤。Right before the usage, you should manually enable the tracking by calling UHoloLensARFunctionLibrary::StartCameraCapture(). 在結束 QR 代碼追蹤之後,您應經由 UHoloLensARFunctionLibrary::StopCameraCapture() 將其停用,以儲存裝置資源。After ending the QR code tracking, you should disable it by UHoloLensARFunctionLibrary::StopCameraCapture() to save the device resources.

設定追蹤的影像Setting up a tracked image

QR 代碼會透過 Unreal 的 AR 追蹤幾何系統呈現為追蹤的影像。QR codes are surfaced through Unreal’s AR tracked geometry system as a tracked image. 若要讓此作業運作,您必須:To get this working, you'll need to:

  1. 建立藍圖並新增 ARTrackableNotify 元件。Create a Blueprint and add an ARTrackableNotify component.

QR AR 可追蹤通知

  1. 選取 [ARTrackableNotify],然後展開 [詳細資料] 面板中的 [事件] 區段。Select ARTrackableNotify and expand the Events section in the Details panel.

QR 事件

  1. 按一下 [On Add Tracked Geometry] 旁邊的 + ,將節點新增至事件圖形。Click + next to On Add Tracked Geometry to add the node to the Event Graph.

將節點新增至 On Add Tracked Geometry

使用追蹤的影像Using a tracked image

下圖中的事件圖形會顯示用來在 QR 代碼中心呈現點的 OnUpdateTrackedImage 事件,並印出其資料。The Event Graph in the following image shows the OnUpdateTrackedImage event being used to render a point in the center of a QR code and print out its data.

QR 轉譯範例

以下是後續動作:Here's what's going on:

  1. 首先,追蹤的影像會強制轉型為 ARTrackedQRCode,以檢查目前更新的影像是否為 QR 代碼。First, the tracked image is cast to an ARTrackedQRCode to check that the current updated image is a QR code.
  2. 編碼的資料是從 QRCode 變數中擷取的。The encoded data is retrieved from the QRCode variable. 您可以從 GetLocalToWorldTransform 的位置,以及具有 GetEstimateSize 的維度,到達 QR 代碼的左上角。You can get the top-left of the QR code from the location of GetLocalToWorldTransform and the dimensions with GetEstimateSize.

您也可以在程式碼中取得 QR 代碼的座標系統You can also get the coordinate system for a QR code in code.

尋找唯一識別碼Finding the unique ID

每個 QR 代碼都有唯一的 guid 識別碼,您可以透過下列方式找到該識別碼:Every QR code has a unique guid ID, which you can find by:

  • 拖放 As ARTracked QRCode 釘選並搜尋 [取得唯一識別碼]。Dragging and dropping the As ARTracked QRCode pin and searching for Get Unique ID.


QR 代碼在幕後還有很多工作要做,因此這不是盡頭。There's a lot going on behind the scenes with QR codes, so this isn't the end of the road. 請務必查看下列連結,以取得有關幕後內容的詳細資訊。Be sure to check out the following links for more details on what's under the hood.

下一個開發檢查點Next Development Checkpoint

依循我們配置的 Unreal 開發檢查點旅程,此時您會探索混合實境平台功能和 API。If you're following the Unreal development checkpoint journey we've laid out, you're in the midst of exploring the Mixed Reality platform capabilities and APIs. 接下來,您可以繼續進行下一個主題:From here, you can proceed to the next topic:

或者,直接跳到在裝置或模擬器上部署應用程式的主題:Or jump directly to deploying your app on a device or emulator:

您可以隨時回到 Unreal 開發檢查點You can always go back to the Unreal development checkpoints at any time.

另請參閱See also