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;//庫檔案的對象(二維碼資訊儲存的地方)
/// 開啟錄影機和準備工作
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;//二維碼圖檔資訊以像素點顔色資訊數組存放
/// 識别錄影機圖檔中的二維碼資訊
/// 列印二維碼識别到的資訊
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();