当前位置:网站首页>Unity production QR code scanning
Unity production QR code scanning
2022-07-23 17:21:00 【Dog tortured by code】
One 、 design sketch

Two 、dll download
To generate a QR code, you need to use zxing.unity.dll
link :https://pan.baidu.com/s/166klhdbIi3mAesdp4JEn9A?pwd=syq1
Extraction code :syq1
stay unity Created in Plugins Folder , take dll Put in this folder

I used unity The version is 2019.4.32
3、 ... and . Create QR code
There are many cases about the generation and creation of QR code effects on the Internet. If you don't understand them, you can also check others , The basic code is almost the same , I sort it out here , The script added more comments .
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using ZXing;
using ZXing.QrCode;
/// <summary>
/// Create QR code
/// </summary>
public class CreatQR : MonoBehaviour {
// Store texture pictures of QR code
Texture2D encoded;
[Header(" Characters that need to produce QR code ")]
public string QrCodeStr = "https://www.baidu.com/" ;
[Header(" Display QR code on the screen ")]
public RawImage rawImg;
void Start()
{
/* Initialize texture image
* Be careful : The width and height must be 256,
* Otherwise, an error occurs when the index exceeds the array boundary
*/
encoded = new Texture2D(256, 256);
CreatQr(); // Create and generate QR code
}
#region Generate qr code
/// <summary>
/// Create QR code
/// </summary>
public void CreatQr()
{
if (QrCodeStr != string.Empty)
{
// Write the QR code into the picture
var color32 = Encode(QrCodeStr, encoded.width, encoded.height);
encoded.SetPixels32(color32); // Change the pixel color of the texture
encoded.Apply();
// The generated QR code picture is attached to RawImage
rawImg.texture = encoded;
}
else
Debug.Log(" No information generated ");
}
/// <summary>
/// Generate qr code
/// </summary>
/// <param name="textForEncoding"> The string that needs to produce QR code </param>
/// <param name="width"> wide </param>
/// <param name="height"> high </param>
/// <returns></returns>
private static Color32[] Encode(string formatStr, int width, int height)
{
// Make some settings before drawing QR code
QrCodeEncodingOptions options = new QrCodeEncodingOptions();
// Set the format of string conversion , Ensure that the string information remains correct
options.CharacterSet = "UTF-8";
// Set the pixel values of the width and height of the drawing area
options.Width = width;
options.Height = height;
// Set the margin width of QR code ( The larger the value is, the wider the white space is , The QR code is reduced )
options.Margin = 1;
/* Instantiate string drawing QR code tool
* BarcodeFormat: Barcode format
* Options: Coding format ( Supported encoding formats )
*/
var barcodeWriter = new BarcodeWriter { Format = BarcodeFormat.QR_CODE, Options = options };
// Draw the QR code and return the color array information of the picture
return barcodeWriter.Write(formatStr);
}
#endregion
}
Scene layout
Create a RawImage, Add Button Components and new CreatQR Script

Effect after operation :

Scanning this QR code with a browser or other scannable software can jump to Baidu page
Four .unity Scan QR code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using ZXing;
/// <summary>
/// Scan the picture
/// </summary>
public class ScanQRCode : MonoBehaviour
{
bool isOpen = true; //true The scanning status is currently enabled false Currently, the scanning status is off
Animator ani; // Scanning animation
private WebCamTexture m_webCameraTexture;// The picture displayed by the camera in real time
private BarcodeReader m_barcodeRender; // Apply for a variable to read the QR code
[Header(" Showing the camera image RawImage")]
public RawImage m_cameraTexture;
[Header(" Scan interval ")]
public float m_delayTime = 3f;
[Header(" Turn on the scan button ")]
public Button openScanBtn;
void Start()
{
// Call up the camera and display the picture on the screen RawImage On
WebCamDevice[] tDevices = WebCamTexture.devices; // Get all cameras
string tDeviceName = tDevices[0].name; // Get the first camera , Use the picture of the first camera to generate picture information
m_webCameraTexture = new WebCamTexture(tDeviceName, 400, 300);// name , wide , high
m_cameraTexture.texture = m_webCameraTexture; // Assign image information
m_webCameraTexture.Play(); // Start real-time display
m_barcodeRender = new BarcodeReader();
ani = GetComponent<Animator>();
OpenScanQRCode(); // Default not to scan
// Button monitoring
openScanBtn.onClick.AddListener(OpenScanQRCode);
}
#region Scan QR code
// Turn on and off scanning QR code
void OpenScanQRCode()
{
if (isOpen)
{
// On state , Scanning needs to be turned off
ani.Play("CloseScan", 0, 0);
//CancelInvoke("CheckQRCode");
}
else
{
// The closed position , Scanning needs to be turned on
// Start scanning
ani.Play("OpenScan", 0, 0);
// Call the method in seconds
//InvokeRepeating("CheckQRCode", 0, m_delayTime);
}
isOpen = !isOpen;
}
#endregion
#region Method of retrieving QR code
/// <summary>
/// Method of retrieving QR code
/// </summary>
public void CheckQRCode()
{
// Store the color array of camera image information map conversion
Color32[] m_colorData = m_webCameraTexture.GetPixels32();
// Retrieve the QR code information in the picture
var tResult = m_barcodeRender.Decode(m_colorData, m_webCameraTexture.width, m_webCameraTexture.height);
if (tResult != null)
{
Application.OpenURL(tResult.Text);
Debug.Log(tResult.Text);
}
}
#endregion
}
Scene layout :

Here we want to record animation , It may be more troublesome , There are packages below , You can download it to have a look
5、 ... and 、 Package download
link :https://pan.baidu.com/s/1EXpW5CUISI6Khk3dQfaeLw?pwd=syq1
Extraction code :syq1
unity The version is 2019.4.32
边栏推荐
- Failure analysis and solution of vscode PIO creation project
- Sprintf and cv:: puttext
- C语言基础篇 —— 2-4 指针的数据类型含义和强制类型转换的解析
- Récursion des bosses 1: formule récursive
- MySQL:不是MySQL问题的MySQL问题
- Vscode - code and file changes cannot be saved
- IR drop, EM, noise and antenna
- Nodejs implements token login registration (koa2)
- 考过PMP对实际工作帮助大吗?
- SQL报错盲注详解
猜你喜欢
随机推荐
食品安全|吃皮蛋会铅中毒?了解这几点后放心吃
[web vulnerability exploration] SQL injection vulnerability
OpenIM重大升级-群聊读扩散模型发布 群管理功能升级
How many common SQL misuses are there in MySQL?
General paging implementation
零基础怎么自学软件测试?十年测试老鸟最强软件测试学习路线图
VScode——代码、文件改动无法保存
Investment and finance report this week: Web3 game bear market attracts gold
【flask高级】从源码深入理解flask路由之endpoint
线程池,我是谁?我在哪儿?
Scene notes
Explication détaillée de l'injection aveugle d'erreur SQL
使用 PreparedStatement 的 JDBC 程序示例
oracle 数据库 11C 之后版本使用 memory_target 自动内存管理
Object.defineproperty method, data agent
Preliminary understanding of string
Ros2 self study notes: rqt visualization tool
考过PMP对实际工作帮助大吗?
MySQL:不是MySQL问题的MySQL问题
unity之制作二维码扫描









