unityZXing二維碼的生成與掃描
阿新 • • 發佈:2020-12-02
此文轉載自:https://blog.csdn.net/weixin_41995872/article/details/85989478
借鑑自某位大佬不記得了
using System.Collections; using System.Collections.Generic; using UnityEngine; using ZXing; using UnityEngine.UI; /// <summary> /// 二維碼掃描識別功能 /// </summary> public class TestQRCodeScanning : MonoBehaviour { [Header("攝像機檢測介面")] public RawImage cameraTexture;//攝像機對映顯示區域 private WebCamTexture webCamTexture;//攝像機對映紋理 public Text text;//用來顯示掃描資訊 //二維碼識別類 BarcodeReader barcodeReader;//庫檔案的物件(二維碼資訊儲存的地方) /// <summary> /// 開啟攝像機和準備工作 /// </summary> void DeviceInit() { //1、獲取所有攝像機硬體 WebCamDevice[] devices = WebCamTexture.devices; //2、獲取第一個攝像機硬體的名稱 string deviceName = devices[0].name;//手機後置攝像機 //3、建立例項化一個攝像機顯示區域 webCamTexture = new WebCamTexture(deviceName, 400, 300); //4、顯示的圖片資訊 cameraTexture.texture = webCamTexture; //5、開啟攝像機執行識別 webCamTexture.Play(); //6、例項化識別二維碼資訊儲存物件 barcodeReader = new BarcodeReader(); } Color32[] data;//二維碼圖片資訊以畫素點顏色資訊陣列存放 /// <summary> /// 識別攝像機圖片中的二維碼資訊 /// 列印二維碼識別到的資訊 /// </summary> void ScanQRCode() { //7、獲取攝像機畫面的畫素顏色陣列資訊 data = webCamTexture.GetPixels32(); //8、獲取圖片中的二維碼資訊 Result result = barcodeReader.Decode(data,webCamTexture.width,webCamTexture.height); //如果獲取到二維碼資訊了,打印出來 if (result!=null) { Debug.Log(result.Text);//===》==》===》 這是從二維碼識別出來的資訊 text.text = result.Text;//顯示掃描資訊 //掃描成功之後的處理 IsScanning = false; webCamTexture.Stop(); } } /// <summary> /// Start 初始化函式 /// </summary> private void Start() { scanningButton.onClick.AddListener(ScanningButtonClick); } bool IsScanning = false; float interval = 3;//掃描識別時間間隔 [SerializeField] Button scanningButton; void ScanningButtonClick() { DeviceInit(); IsScanning = true; } private void Update() { if (IsScanning) { //每隔一段時間進行一次識別二維碼資訊 interval += Time.deltaTime; if (interval>=3) { interval = 0; ScanQRCode();//開始掃描 } } } }
ZXing:https://archive.codeplex.com/?p=zxingnet
下載之後把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> /// 將制定字串資訊轉換成二維碼圖片資訊 /// </summary> /// <param name="formatStr">要生產二維碼的字串資訊</param> /// <param name="width">二維碼的寬度</param> /// <param name="height">二維碼的高度</param> /// <returns>返回二維碼圖片的顏色陣列資訊</returns> 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 = height; //設定二維碼邊緣留白寬度(值越大留白寬度大,二維碼就減小) options.Margin = 1; //例項化字串繪製二維碼工具 barcodeWriter = new BarcodeWriter {Format=BarcodeFormat.QR_CODE,Options=options }; //進行二維碼繪製並進行返回圖片的顏色陣列資訊 return barcodeWriter.Write(formatStr); } /// <summary> /// 根據二維碼圖片資訊繪製指定字串資訊的二維碼到指定區域 /// </summary> /// <param name="str">要生產二維碼的字串資訊</param> /// <param name="width">二維碼的寬度</param> /// <param name="height">二維碼的高度</param> /// <returns>返回繪製好的圖片</returns> Texture2D ShowQRCode(string str,int width,int height) { //例項化一個圖片類 Texture2D t = new Texture2D(width, height); //獲取二維碼圖片顏色陣列資訊 Color32[] col32 = GeneQRCode(str, width, height); //為圖片設定繪製畫素顏色資訊 t.SetPixels32(col32); //設定資訊更新應用下 t.Apply(); //將整理好的圖片資訊顯示到指定區域中 return t; } /// <summary> /// 開始繪製指定資訊的二維碼 /// </summary> /// <param name="formatStr"></param> void DrawQRCode(string formatStr) { //注意:這個寬高度大小256不要變。不然生成的資訊不正確 //256有可能是這個ZXingNet外掛指定大小的繪製畫素點數值 Texture2D t = ShowQRCode(formatStr, 256, 256); //顯示到UI介面的圖片上 QRCode.texture = t; } public string QRCodeText = "二維碼"; void DrawButtonClick() { DrawQRCode(QRCodeText); } private void Start() { drawbutton.onClick.AddListener(DrawButtonClick); } }