使用Unity扫描场景内的二维码,使用插件ZXing

使用Unity扫描场景内的二维码,使用插件ZXing

使用Unity扫描场景内的二维码,ZXing可能没有提供场景内扫描的方法,只有调用真实摄像机扫描二维码的方法。
实现的原理是:在摄像机上添加脚本,发射射线,当射线打到rawimage的时候获取rawimage的texture并作为二维码扫描。
支持webgl,windows。

代码

生成二维码,需要有zxing.unity.dll

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using ZXing.Common;
using ZXing;/// <summary>
/// 生成二维码
/// </summary>
public class GenerateQRCode : MonoBehaviour
{public RawImage rawImage;void Start(){//整数倍变化256*NrawImage.texture = GenerateQRImageWithColor("hello word!", 256 * 3, Color.black);}/// <summary>/// 生成二维码 /// </summary>/// <param name="content">二维码内容</param>/// <param name="widthAndTall">宽度高度和宽度</param>/// <param name="color">二维码颜色</param>public Texture2D GenerateQRImageWithColor(string content, int widthAndTall, Color color){BitMatrix bitMatrix;Texture2D texture = GenerateQRImageWithColor(content, widthAndTall, widthAndTall, color, out bitMatrix);return texture;}/// <summary>/// 生成2维码 方法二/// 经测试:能生成任意尺寸的正方形/// </summary>/// <param name="content"></param>/// <param name="width"></param>/// <param name="height"></param>Texture2D GenerateQRImageWithColor(string content, int width, int height, Color color, out BitMatrix bitMatrix){// 编码成color32MultiFormatWriter writer = new MultiFormatWriter();Dictionary<EncodeHintType, object> hints = new Dictionary<EncodeHintType, object>();//设置字符串转换格式,确保字符串信息保持正确hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");// 设置二维码边缘留白宽度(值越大留白宽度大,二维码就减小)hints.Add(EncodeHintType.MARGIN, 1);hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.M);//实例化字符串绘制二维码工具bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints);// 转成texture2dint w = bitMatrix.Width;int h = bitMatrix.Height;print(string.Format("w={0},h={1}", w, h));Texture2D texture = new Texture2D(w, h);for (int x = 0; x < h; x++){for (int y = 0; y < w; y++){if (bitMatrix[x, y]){texture.SetPixel(y, x, color);}else{texture.SetPixel(y, x, Color.white);}}}texture.Apply();return texture;}/// <summary>/// 生成2维码 方法三/// 在方法二的基础上,添加小图标/// </summary>/// <param name="content"></param>/// <param name="width"></param>/// <param name="height"></param>/// <returns></returns>Texture2D GenerateQRImageWithColorAndIcon(string content, int width, int height, Color color, Texture2D centerIcon){BitMatrix bitMatrix;Texture2D texture = GenerateQRImageWithColor(content, width, height, color, out bitMatrix);int w = bitMatrix.Width;int h = bitMatrix.Height;// 添加小图int halfWidth = texture.width / 2;int halfHeight = texture.height / 2;int halfWidthOfIcon = centerIcon.width / 2;int halfHeightOfIcon = centerIcon.height / 2;int centerOffsetX = 0;int centerOffsetY = 0;for (int x = 0; x < h; x++){for (int y = 0; y < w; y++){centerOffsetX = x - halfWidth;centerOffsetY = y - halfHeight;if (Mathf.Abs(centerOffsetX) <= halfWidthOfIcon && Mathf.Abs(centerOffsetY) <= halfHeightOfIcon){texture.SetPixel(x, y,centerIcon.GetPixel(centerOffsetX + halfWidthOfIcon, centerOffsetY + halfHeightOfIcon));}}}texture.Apply();// 存储成文件byte[] bytes = texture.EncodeToPNG();string path = System.IO.Path.Combine(Application.dataPath, "qr.png");System.IO.File.WriteAllBytes(path, bytes);return texture;}
}

扫描二维码

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using ZXing;/// <summary>
/// 扫描二维码
/// </summary>
public class ScanQRCode : MonoBehaviour
{public Camera customCamera;public UnityEngine.UI.Text showText;public UnityEngine.UI.Button btnStartScan;public LayerMask layerMask; // 需要检测的图层public float rayLength = 5f; // 射线长度void Start(){btnStartScan.onClick.AddListener(OnStartScamOnClick);}private void OnStartScamOnClick(){// 发射一条射线从指定摄像机的屏幕中心Ray ray = customCamera.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));// 使用 EventSystem 进行 UI 射线检测PointerEventData pointerEventData = new PointerEventData(EventSystem.current);pointerEventData.position = new Vector2(Screen.width / 2, Screen.height / 2);List<RaycastResult> results = new RaycastResult[1].ToList();EventSystem.current.RaycastAll(pointerEventData, results);// 如果检测到了UI元素if (results.Count > 0){// 返回UI元素的名称Debug.Log("Hit UI object: " + results[0].gameObject.name);string imgo = GetColorImage(results[0].gameObject);showText.text= imgo;}else{// 如果没有检测到UI元素,则进行3D场景中的射线检测RaycastHit hitInfo;if (Physics.Raycast(ray, out hitInfo, rayLength)){// 返回击中物体的名称Debug.Log("Hit object: " + hitInfo.collider.gameObject.name);}}}private string GetColorImage(GameObject go){RawImage rawImage;if (!go.TryGetComponent<RawImage>(out rawImage)){return "";}Texture2D texture = rawImage.texture as Texture2D;try{// 创建二维码解码器IBarcodeReader barcodeReader = new BarcodeReader();// 解码纹理中的二维码Result result = barcodeReader.Decode(texture.GetPixels32(), texture.width, texture.height);// 返回解码结果return result != null ? result.Text : null;}catch (System.Exception ex){showText.text = ("Error scanning QR code: " + ex.Message);Debug.LogError("Error scanning QR code: " + ex.Message);return null;}}}

场景布置

在这里插入图片描述

下载

可以私信下载
https://download.csdn.net/download/GoodCooking/89208272

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/2054.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【面试八股总结】Linux系统下的I/O多路复用

参考资料 &#xff1a;小林Coding、阿秀、代码随想录 I/O多路复用是⼀种在单个线程或进程中处理多个输入和输出操作的机制。它允许单个进程同时监视多个文件描述符(通常是套接字)&#xff0c;一旦某个描述符就绪&#xff08;一般是读就绪或者写就绪&#xff09;&#xff0c;能够…

分享三个转换速度快、准确率高的视频转文字工具

想要直接将视频转换成文字&#xff0c;转换工具很重要&#xff01;给大家分享三个转换速度快、准确率高的视频转文字工具&#xff0c;轻松完成转换。 1.网易见外 https://sight.youdao.com/ 网易家的智能转写翻译服务工作站&#xff0c;网页端就可以直接使用&#xff0c;支持视…

Spring Bean依赖注入-Spring入门(二)

1、SpringBean概述 在Spring中&#xff0c;一切Java对象都被视为Bean&#xff0c;用于实现某个具体功能。 Bean的依赖关系注入的过程&#xff0c;也称为Bean的装配过程。 Bean的装配方式有3种&#xff1a; XML配置文件注解Java类 Spring中常用的两种装配方式分别是基于XML的…

Codeforces Round 821 (Div. 2) D2. Zero-One

题目 #include <bits/stdc.h> using namespace std; #define int long long #define pb push_back #define fi first #define se second #define lson p << 1 #define rson p << 1 | 1 const int maxn 1e5 5, inf 1e18, maxm 4e4 5; const int N 1e6;c…

vue用法示例(二)

1、v-if决定标签是否显示 <!DOCTYPE html> <html> <head> <meta charset"utf-8"> <title>Vue 测试实例 - 菜鸟教程(runoob.com)</title> <script src"https://cdn.staticfile.net/vue/2.2.2/vue.min.js"></…

【MySQL】InnoDB与MyISAM存储引擎的区别与选择

存储引擎就是存储数据、建立索引、更新/查询数据等技术的实现方式 。 存储引擎是基于表的&#xff0c;而不是基于库的&#xff0c;所以存储引擎也可被称为表类型。我们可以在创建表的时候&#xff0c;来指定选择的存储引擎&#xff0c;如果没有指定将自动选择默认的存储引擎。…

【工具-PyCharm】

工具-PyCharm ■ PyCharm-简介■ PyCharm-安装■ PyCharm-使用■ 修改主题■ 设置字体■ 代码模板■ 解释器配置■ 文件默认编码■ 快捷键■ 折叠■ 移动■ 注释■ 编辑■ 删除■ 查看■ 缩进■ 替换 ■ PyCharm-简介 官方下载地址 Professional&#xff1a;专业版&#xff0…

图像哈希:DCT篇

Robust image hashing with dominant DCT coefficients 文章信息 作者&#xff1a;唐振军期刊&#xff1a;Optic&#xff08;Q2/3区&#xff09;题目&#xff1a;Robust image hashing with dominant DCT coefficients 目的、实验步骤及结论 目的&#xff1a;使用传统的DCT对…

ChatGPT如何助力科研创新,提升研究效率?

随着人工智能技术的快速发展&#xff0c;其在科研领域的应用也愈发广泛。AI不仅提升了科研创新的效率&#xff0c;还为科研人员带来了前所未有的便利。本文将从ChatGPT深度科研应用、数据分析及机器学习、AI绘图以及高效论文撰写等方面&#xff0c;综述AI如何助力科研创新与效率…

python--使用pika库操作rabbitmq实现需求

Author: wencoo Blog&#xff1a;https://wencoo.blog.csdn.net/ Date: 22/04/2024 Email: jianwen056aliyun.com Wechat&#xff1a;wencoo824 QQ&#xff1a;1419440391 Details:文章目录 目录正文 或 背景pika链接mqpika指定消费数量pika自动消费实现pika获取队列任务数量pi…

JavaScript(二)

JavaScript的语法 1.JavaScript的大小写 在JavaScript中&#xff0c;大小写是敏感的&#xff0c;这意味着大小写不同的标识符被视为不同的变量或函数。例如&#xff0c;myVariable 和 myvariable 被视为两个不同的变量。因此&#xff0c;在编写JavaScript代码时&#xff0c;必…

如何在PostgreSQL中创建并使用窗口函数来进行复杂的分析查询?

文章目录 解决方案1. 了解窗口函数的基本概念2. 常用的窗口函数3. 使用示例示例 1&#xff1a;计算每行销售数据的累计销售额示例 2&#xff1a;计算每行销售数据相对于前一行销售额的增长率 结论 PostgreSQL 提供了一套强大的窗口函数&#xff08;Window Functions&#xff09…

第四十六节 Java 8 Stream

Java 8 API添加了一个新的抽象称为流Stream&#xff0c;可以让你以一种声明的方式处理数据。 Stream 使用一种类似用 SQL 语句从数据库查询数据的直观方式来提供一种对 Java 集合运算和表达的高阶抽象。 Stream API可以极大提高Java程序员的生产力&#xff0c;让程序员写出高…

MQTT Broker 白皮书:全面实用的 MQTT Broker 选型指南

在智能数字化时代&#xff0c;家居设备、工厂传感器、智能汽车、能源电力计量表等各类设备都已变身为新型的智能终端。为了满足这些海量且持续增长的智能设备之间对于实时、可靠的消息传递的需求&#xff0c;MQTT Broker 消息代理或消息中间件扮演了至关重要的角色。作为新一代…

STM32H750外设ADC之模拟窗口看门狗

目录 概述 1 相关寄存器 2 功能描述 3 AWDx 标志和中断 4 模拟看门狗 1 4.1 模拟看门狗 1 说明 4.2 模拟看门狗通道选择 4.3 阀值选择 5 模拟看门狗 2和3 6 ADCx_AWDy_OUT 信号输出生成 6.1 功能介绍 6.2 输出信号案例 7 模拟看门狗 1、 2、 3 比较 概述 本文主…

GEE:直方图匹配

作者:CSDN @ _养乐多_ 本文将介绍如何在 Google Earth Engine (GEE) 平台中,进行直方图匹配的代码。本文以Sentinel2和Landsat9的影像为例,将Landsat9的影像的分布直方图转换到了Sentinel2中相同波段的直方图,并可视化了匹配前后效果。该方法也可以用于影像融合,将低分辨…

Opencv_3_图像对象的创建与赋值

ColorInvert.h 如下&#xff1a; #include <opencv.hpp> using namespace std; #include <opencv.hpp> using namespace cv; using namespace std; class ColorInvert{ public : void mat_creation(); }; ColorInvert.cpp 文件如下&#xff1a; #include &q…

从零手写 tomcat

从零手写例子 项目简介 /\_/\ ( o.o ) > ^ <mini-cat 是简易版本的 tomcat 实现。别称【嗅虎】(心有猛虎&#xff0c;轻嗅蔷薇。) 开源地址&#xff1a;https://github.com/houbb/minicat 特性 简单的启动实现/netty 支持 servlet 支持 静态网页支持 filter/list…

解决宝塔面板无法访问(无法访问或拒绝链接)

&#x1f40c;博主主页&#xff1a;&#x1f40c;​倔强的大蜗牛&#x1f40c;​ &#x1f4da;专栏分类&#xff1a;Linux ❤️感谢大家点赞&#x1f44d;收藏⭐评论✍️ 问题如下&#xff1a; 本人设置了授权IP&#xff0c;但是有些问题&#xff0c;所以是打算取消授权IP 重…

Spring Boot 自动装配执行流程

Spring Boot 自动装配执行流程 Spring Boot 自动装配执行流程如下&#xff1a; Spring Boot 启动时会创建一个 SpringApplication实例&#xff0c;该实例存储了应用相关信息&#xff0c;它负责启动并运行应用。实例化 SpringApplication 时&#xff0c;会自动装载META-INF/spr…