unityZXing二維碼的生成與掃描
本文實(shí)例為大家分享了unityZXing二維碼生成與掃描的具體代碼,供大家參考,具體內(nèi)容如下
借鑒自某位大佬不記得了
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ZXing;
using UnityEngine.UI;
/// <summary>
/// 二維碼掃描識(shí)別功能
/// </summary>
public class TestQRCodeScanning : MonoBehaviour {
[Header("攝像機(jī)檢測(cè)界面")]
public RawImage cameraTexture;//攝像機(jī)映射顯示區(qū)域
private WebCamTexture webCamTexture;//攝像機(jī)映射紋理
public Text text;//用來(lái)顯示掃描信息
//二維碼識(shí)別類
BarcodeReader barcodeReader;//庫(kù)文件的對(duì)象(二維碼信息保存的地方)
/// <summary>
/// 開啟攝像機(jī)和準(zhǔn)備工作
/// </summary>
void DeviceInit()
{
//1、獲取所有攝像機(jī)硬件
WebCamDevice[] devices = WebCamTexture.devices;
//2、獲取第一個(gè)攝像機(jī)硬件的名稱
string deviceName = devices[0].name;//手機(jī)后置攝像機(jī)
//3、創(chuàng)建實(shí)例化一個(gè)攝像機(jī)顯示區(qū)域
webCamTexture = new WebCamTexture(deviceName, 400, 300);
//4、顯示的圖片信息
cameraTexture.texture = webCamTexture;
//5、打開攝像機(jī)運(yùn)行識(shí)別
webCamTexture.Play();
//6、實(shí)例化識(shí)別二維碼信息存儲(chǔ)對(duì)象
barcodeReader = new BarcodeReader();
}
Color32[] data;//二維碼圖片信息以像素點(diǎn)顏色信息數(shù)組存放
/// <summary>
/// 識(shí)別攝像機(jī)圖片中的二維碼信息
/// 打印二維碼識(shí)別到的信息
/// </summary>
void ScanQRCode()
{
//7、獲取攝像機(jī)畫面的像素顏色數(shù)組信息
data = webCamTexture.GetPixels32();
//8、獲取圖片中的二維碼信息
Result result = barcodeReader.Decode(data,webCamTexture.width,webCamTexture.height);
//如果獲取到二維碼信息了,打印出來(lái)
if (result!=null)
{
Debug.Log(result.Text);//===》==》===》 這是從二維碼識(shí)別出來(lái)的信息
text.text = result.Text;//顯示掃描信息
//掃描成功之后的處理
IsScanning = false;
webCamTexture.Stop();
}
}
/// <summary>
/// Start 初始化函數(shù)
/// </summary>
private void Start()
{
scanningButton.onClick.AddListener(ScanningButtonClick);
}
bool IsScanning = false;
float interval = 3;//掃描識(shí)別時(shí)間間隔
[SerializeField] Button scanningButton;
void ScanningButtonClick()
{
DeviceInit();
IsScanning = true;
}
private void Update()
{
if (IsScanning)
{
//每隔一段時(shí)間進(jìn)行一次識(shí)別二維碼信息
interval += Time.deltaTime;
if (interval>=3)
{
interval = 0;
ScanQRCode();//開始掃描
}
}
}
}
ZXing:ZingNet
下載之后把zxing.unity.dll拷貝到Unity的Plugins文件夾下,
zxing.unity.dll下載之后的位置找到UnityDemo/Assets下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using ZXing;
//二維碼的生成
public class TestQRCodeDraw : MonoBehaviour {
[Header("繪制好的二維碼顯示界面")]
public RawImage QRCode;
//二維碼繪制類
BarcodeWriter barcodeWriter;
[SerializeField] Button drawbutton;
/// <summary>
/// 將制定字符串信息轉(zhuǎn)換成二維碼圖片信息
/// </summary>
/// <param name="formatStr">要生產(chǎn)二維碼的字符串信息</param>
/// <param name="width">二維碼的寬度</param>
/// <param name="height">二維碼的高度</param>
/// <returns>返回二維碼圖片的顏色數(shù)組信息</returns>
Color32[] GeneQRCode(string formatStr,int width,int height)
{
//繪制二維碼前進(jìn)行一些設(shè)置
ZXing.QrCode.QrCodeEncodingOptions options =
new ZXing.QrCode.QrCodeEncodingOptions();
//設(shè)置字符串轉(zhuǎn)換格式,確保字符串信息保持正確
options.CharacterSet = "UTF-8";
//設(shè)置繪制區(qū)域的寬度和高度的像素值
options.Width = width;
options.Height = height;
//設(shè)置二維碼邊緣留白寬度(值越大留白寬度大,二維碼就減小)
options.Margin = 1;
//實(shí)例化字符串繪制二維碼工具
barcodeWriter = new BarcodeWriter {Format=BarcodeFormat.QR_CODE,Options=options };
//進(jìn)行二維碼繪制并進(jìn)行返回圖片的顏色數(shù)組信息
return barcodeWriter.Write(formatStr);
}
/// <summary>
/// 根據(jù)二維碼圖片信息繪制指定字符串信息的二維碼到指定區(qū)域
/// </summary>
/// <param name="str">要生產(chǎn)二維碼的字符串信息</param>
/// <param name="width">二維碼的寬度</param>
/// <param name="height">二維碼的高度</param>
/// <returns>返回繪制好的圖片</returns>
Texture2D ShowQRCode(string str,int width,int height)
{
//實(shí)例化一個(gè)圖片類
Texture2D t = new Texture2D(width, height);
//獲取二維碼圖片顏色數(shù)組信息
Color32[] col32 = GeneQRCode(str, width, height);
//為圖片設(shè)置繪制像素顏色信息
t.SetPixels32(col32);
//設(shè)置信息更新應(yīng)用下
t.Apply();
//將整理好的圖片信息顯示到指定區(qū)域中
return t;
}
/// <summary>
/// 開始繪制指定信息的二維碼
/// </summary>
/// <param name="formatStr"></param>
void DrawQRCode(string formatStr)
{
//注意:這個(gè)寬高度大小256不要變。不然生成的信息不正確
//256有可能是這個(gè)ZXingNet插件指定大小的繪制像素點(diǎn)數(shù)值
Texture2D t = ShowQRCode(formatStr, 256, 256);
//顯示到UI界面的圖片上
QRCode.texture = t;
}
public string QRCodeText = "二維碼";
void DrawButtonClick()
{
DrawQRCode(QRCodeText);
}
private void Start()
{
drawbutton.onClick.AddListener(DrawButtonClick);
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
c# 使用Entity Framework操作Access數(shù)據(jù)庫(kù)的示例
本篇文章主要介紹了c# 使用Entity Framework操作Access數(shù)據(jù)庫(kù)的示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
關(guān)于C#10 新特性 Lambda 優(yōu)化
這篇文章主要介紹了C# 10 新特性 Lambda 優(yōu)化,C# 10 對(duì)于 Lambda 做了很多的優(yōu)化,我們可以在 C# 中更加方便地使用委托和 Lambda 了,下面就來(lái)看一些示例,需要的朋友也可以參考一下2021-11-11
基于AForge實(shí)現(xiàn)C#攝像頭視頻錄制功能
這篇文章主要為大家詳細(xì)介紹了基于AForge實(shí)現(xiàn)C#攝像頭視頻錄制功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
解析C#中[],List,Array,ArrayList的區(qū)別及應(yīng)用
本篇文章主要是對(duì)C#中[],List,Array,ArrayList的區(qū)別及應(yīng)用進(jìn)行了詳細(xì)的介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-01-01

