Vuforia AR篇(七)— 二维码识别

前言

在数字化时代,条形码和二维码已成为连接现实世界与数字信息的重要桥梁。Vuforia作为领先的AR开发平台,提供了Barcode Scanner功能,使得在Unity中实现条形码和二维码的识别变得简单而高效。本文将详细介绍如何在Unity中利用Vuforia的Barcode Scanner功能,创建一个能够识别和响应条形码和二维码的AR体验。


一、什么是Barcode ?

Vuforia的Barcode Scanner是一个强大的功能,它支持广泛的条形码和二维码类型。开发者可以通过简单的设置,在Unity中实现对这些码的扫描和识别,从而触发不同的数字内容展示或交互。

二、使用步骤

  1. 设置相机:在 Unity 场景中添加一个 Vuforia AR 相机。可以在 GameObject -> Vuforia Engine -> AR Camera 中找到。

  2. 创建Barcode

​ 在 AR Camera 的 Inspector 面板中,确保 Vuforia Behaviour 启用。

​ 添加一个 Barcode。可以在 GameObject -> Vuforia Engine -> Barcode` 中找到。
在这里插入图片描述

三、点击二维码显示信息

  1. 创建TestCollider脚本给显示的二维码添加碰撞体
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vuforia;

public class TestCollider : MonoBehaviour
{
    private BarcodeBehaviour _behaviour; // 声明一个BarcodeBehaviour类型的私有变量
    private MeshCollider _collider; // 声明一个MeshCollider类型的私有变量
    
    void Awake()
    {
        _behaviour = GetComponent<BarcodeBehaviour>(); // 获取当前物体的BarcodeBehaviour组件
    }

    void Start()
    {
        if (_behaviour != null) // 如果BarcodeBehaviour组件不为空
        {
            _behaviour.OnBarcodeOutlineChanged += ChangeTest; // 订阅OnBarcodeOutlineChanged事件,指定事件处理函数为ChangeTest
        }
    }

    private void ChangeTest(Vector3[] v3)
    {
        UpdateMeshCollider(v3); // 调用UpdateMeshCollider函数并传递v3参数
    }

    void UpdateMeshCollider(Vector3[] v3)
    {
        if (!_collider) // 如果MeshCollider组件为空
        {
            _collider = gameObject.AddComponent<MeshCollider>(); // 给当前物体添加一个MeshCollider组件
            _collider.cookingOptions = MeshColliderCookingOptions.None; // 设置MeshCollider的cookingOptions属性
        }

        Mesh mesh = new Mesh // 创建一个新的Mesh对象
        {
            vertices = v3, // 设置Mesh对象的顶点数组为传入的v3
            triangles = new[] { 0, 1, 2, 0, 2, 3 } // 设置Mesh对象的三角形数组
        };
        _collider.sharedMesh = mesh; // 将创建的Mesh对象赋值给MeshCollider的sharedMesh属性
    }
    
    void Update()
    {

    }
}

  1. 创建TestShoot脚本用于射线检测
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Vuforia;

public class TestShoot : MonoBehaviour
{
    public Text tex; // 公共的Text组件变量,用于显示射线检测结果
    
    void Awake()
    {
 
    }

    void Start()
    {

    }

    void Update()
    {
        // 从摄像机的屏幕坐标向场景中发射一条射线
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        
        // 如果射线检测到碰撞
        if (Physics.Raycast(ray.origin, ray.direction, out RaycastHit hit))
        {
            // 获取碰撞到的物体上的BarcodeBehaviour组件
            var behaviour = hit.transform.GetComponent<BarcodeBehaviour>();
            
            // 将BarcodeBehaviour组件的InstanceData.Text赋值给tex的文本属性
            tex.text = behaviour.InstanceData.Text;
        }
        else
        {
            // 如果射线没有检测到任何碰撞,将tex的文本属性设为空
            tex.text = "";
        }
    }
}

3.创建一个Panel和Text来显示内容

在这里插入图片描述

四、效果

在这里插入图片描述

  • 34
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

墨染青枫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值