Unity插件系列之二维码
1.二维码常见的生成与识别途径
1.草料二维码 https://cli.im/text
2.在软件中实现生成和扫描二维码 使用zxing实现
zxing是一个用java写的开源项目,zxing.net是移植到.net工程上的。
https://github.com/micjahn/ZXing.Net
2.实现二维码的识别
1.Unity工程
2.让RawImage显示摄像头内容
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using ZXing;
- using UnityEngine.UI;
- public class QRCodeTest : MonoBehaviour {
- public RawImage cameraTexture;
- private WebCamTexture webCamTexture;
- // Use this for initialization
- void Start () {
- WebCamDevice[] devices = WebCamTexture.devices;
- string deviceName = devices[0].name;
- webCamTexture = new WebCamTexture(deviceName, 400, 300);
- cameraTexture.texture = webCamTexture;
- webCamTexture.Play();
- }
- // Update is called once per frame
- void Update () {
- }
- }
3.扫描功能实现代码(代码有点长,慢慢看)
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using ZXing;
- using UnityEngine.UI;
- public class QRCodeTest : MonoBehaviour {
- public RawImage cameraTexture;//存储摄像头拍到的内容
- private WebCamTexture webCamTexture;//摄像头的内容
- Color32[] data;
- BarcodeReader barcodeReader;//Zxing提供的读取摄像头内容的方法
- float interval = 0f;//做定时器用
- // Use this for initialization
- void Start () {
- //打开了摄像头
- WebCamDevice[] devices = WebCamTexture.devices;
- string deviceName = devices[0].name;
- webCamTexture = new WebCamTexture(deviceName, 400, 300);
- cameraTexture.texture = webCamTexture;
- webCamTexture.Play();
- barcodeReader = new BarcodeReader();
- }
- // Update is called once per frame
- void Update () {
- interval += Time.deltaTime;
- if (interval >= 3f) {
- ScanQRCode();
- interval = 0f;
- }
- }
- void ScanQRCode()
- {
- //GetPixels32是把格式转换为Color32的方法
- data = webCamTexture.GetPixels32();
- //result存储读取的内容
- var result = barcodeReader.Decode(data, webCamTexture.width, webCamTexture.height);
- if (result != null) {
- Debug.Log(result.Text);
- }
- }
- }
3.实现二维码的生成 (注:我关掉网络也能成功识别生成的二维码,说明这东西是离线的)
1.新建一个RawImage存储生成的识别图
2.添加了生成二维码的方法:
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using ZXing;
- using UnityEngine.UI;
- public class QRCodeTest : MonoBehaviour {
- public RawImage cameraTexture;//存储摄像头拍到的内容
- public RawImage QRCode;//存储生成的二维码
- private WebCamTexture webCamTexture;//摄像头的内容
- Color32[] data;
- BarcodeReader barcodeReader;//Zxing提供的读取摄像头内容的方法
- BarcodeWriter barcodeWriter;//Zxing提供的写内容的方法
- float interval = 0f;//做定时器用
- // Use this for initialization
- void Start () {
- //打开了摄像头
- WebCamDevice[] devices = WebCamTexture.devices;
- string deviceName = devices[0].name;
- webCamTexture = new WebCamTexture(deviceName, 400, 300);
- cameraTexture.texture = webCamTexture;
- webCamTexture.Play();
- barcodeReader = new BarcodeReader();
- }
- // Update is called once per frame
- void Update () {
- interval += Time.deltaTime;
- if (interval >= 3f) {
- ScanQRCode();
- interval = 0f;
- }
- //按下空格键生成二维码
- if (Input.GetKeyDown(KeyCode.Space))
- {
- //在这种写法里 只能填入256
- ShowQRCode("我爱学习", 256, 256);
- //如果想要其他大小的二维码呢?见文章底部链接
- }
- }
- //扫描二维码方法
- void ScanQRCode()
- {
- //GetPixels32是从一张图片上获取颜色的方法
- data = webCamTexture.GetPixels32();
- //result存储读取的内容
- var result = barcodeReader.Decode(data, webCamTexture.width, webCamTexture.height);
- if (result != null) {
- Debug.Log(result.Text);
- }
- }
- //显示生成的二维码
- void ShowQRCode(string str,int width,int height) {
- //定义texture2d并且填充
- Texture2D t = new Texture2D(width, height);
- t.SetPixels32(GeneQRCode(str, width, height));
- t.Apply();
- QRCode.texture = t;
- }
- //返回Color32图片颜色的方法
- Color32[] GeneQRCode(string formatStr,int width,int height) {
- ZXing.QrCode.QrCodeEncodingOptions options = new ZXing.QrCode.QrCodeEncodingOptions();
- options.CharacterSet = "UTF-8";//设置字符编码,否则中文不能显示
- options.Width = width;
- options.Height = width;
- options.Margin = 1;//二维码距离边缘的空白
- barcodeWriter = new BarcodeWriter { Format = ZXing.BarcodeFormat.QR_CODE, Options = options };
- return barcodeWriter.Write(formatStr);
- }
- }
更多:
ZXing 二维码生成的坑 http://blog.sina.com.cn/s/blog_6ad33d350102xj8l.html
zxing.net读取和保存二维码,设置中文字符集,读取中文二维码 https://bbs.csdn.net/topics/391950715?page=1
Unity插件系列之二维码的更多相关文章
- jquery.qrcode二维码插件生成彩色二维码
jquery.qrcode.js 是居于jquery类库的绘制二维码的插件,用它来实现二维码图形渲染支持canvas和table两种绘图方式. (jquery.qrcode.js 设置显示方式为tab ...
- 草料Chrome浏览器插件,让二维码更好用
安装插件草料chrome插件,是专为chrome核心的浏览器开发的一个二维码应用增强工具插件. 自动将地址栏链接生成二维码 以谷歌原生的chrome浏览器为例,插件安装成功后会在浏览器地址栏旁边出现一 ...
- 使用jQuery的插件qrcode生成二维码(静态+动态生成)及常见问题解决方法
一.简介 1.说明 qrcode其实是通过使用jQuery实现图形渲染,画图,支持canvas(HTML5)和table两种方式,您可以到https://github.com/jeromeetienn ...
- jquery插件生成简单二维码
除了利用第三方网站生成二维码外,这是一个比较简单的办法吧. <script src="/Scripts/jquery.qrcode.min.js" type="te ...
- jQuery.qrcode二维码插件生成网页二维码
如果是一个固定的二维码,我们只需要在网上找个地方生成图片,然后放上图片就可以了.但如果是地址不固定需要根据页面来生成的话.就有两种做法,一个是后端根据页面做一个动态的二维码.一种是前端使用插件生成. ...
- CorelDRAW 二维码插件
随着智能手机的流行,二维码在各个领域大量应用,这个插件在补CorelDRAW这方面的不足: 这个插件是 cpg 格式,安装请看这篇博客:http://www.cnblogs.com/o594cql/p ...
- Unity生成简易二维码
最近项目需求,需要在Unity中动态生成二维码.所以就研究了一下,下面把动态生成二维码的方法向大家分享一下. 第一种方法 需要一个 ZXing.dll文件. 下载地址我会在文章结尾给出. 直接将下载好 ...
- C#运用GmaQrCode生成二维码
项目中需要生成二维码,方法比较多,可以采用JS插件,也可以采用第三方插件后台生成二维码,在后台方法中可以采用QRCode或者GmaQrCode,现在介绍一种C#在后台生成二维码的方法: /// < ...
- 为jquery qrcode生成的二维码嵌入图片
在一次微信项目中,需要实现通过扫描二维码来进行会议签到,二维码的生成选择了qrcode.js的版本,然后使用jquery.qrcode.js插件来绘制二维码. <script type=&quo ...
随机推荐
- Python编程从入门到实践笔记——操作列表
Python编程从入门到实践笔记——操作列表 #coding=utf-8 magicians = ['alice','david','carolina'] #遍历整个列表 for magician i ...
- Mac实用技巧之:访达/Finder
更多Mac实用技巧系列文章请访问我的博客:Mac实用技巧系列文章 Finder就相当于windows XP系统的『我的电脑』或win7/win10系统里的『计算机』(打开后叫资源管理器),find是查 ...
- java基础(五):谈谈java中的多线程
1.多线程 1.1.多线程介绍 学习多线程之前,我们先要了解几个关于多线程有关的概念. 进程:正在运行的程序.确切的来说,当一个程序进入内存运行,即变成一个进程,进程是处于运行过程中的程序,并且具有一 ...
- mybatis 参数格式异常-- Error querying database. Cause: java.lang.NumberFormatException: For input string
mybatis中 <if></if>标签中进行判断时,如果传入的时字符格式和数字进行判断需要将数字进行转译,否则默认是数字和数字进行比较,这是就会出现参数格式异常如<if ...
- 包(package)以及面向对象三个基本特征(继承)的介绍
1.包(package) 包(package) 用于管理程序中的类,主要用于解决类的同名问题.包也可以看成一个目录. 包的作用 [1] 防止命名冲突. [2] 允许类组成一个单元(模块),便于管理和维 ...
- ubuntu 安装vm-tool
1.“虚拟机”->“安装vmware tools”VMware tools 2. 新建一个文件夹 ,打开vmware tools安装介质.右键选择vmwaretools的gz压缩包,选择“提取到 ...
- React 16.x 新特性思维导图
React 16版本相对于以前的版本做了很大的改动,下面是我整理的React 16.x 新特性的思维导图文件,欢迎围观和指导:
- Dynamics 365 Customer Engagement中插件的调试
微软动态CRM专家罗勇 ,回复319或者20190319可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me . 本文主要根据官方的教 ...
- jmeter接口测试实战-创建用户
jmeter接口测试实战-创建用户 相信大多数看到标题的同学都会有疑问, 创建用户不是很简单吗, 调用一下创建用户接口, 传入指定入参, 用户即可创建成功, 今天我们的实战来讲讲创建场景.通过接口创建 ...
- Swift中 删除Array的元素对象
Swift中Array的删除对象 在Swift中数组Array没有removeObject的方法 1.找到下标 let model_index = selectedArray.index(where: ...