236 lines
6.7 KiB
C#
236 lines
6.7 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
#if UNITY_WEBGL
|
|
using WeChatWASM;
|
|
#endif
|
|
using ZXing;
|
|
using ZXing.QrCode;
|
|
|
|
/// <summary>
|
|
/// 扫描图片
|
|
/// </summary>
|
|
public class ScanQRCode : MonoBehaviour
|
|
{
|
|
bool isOpen = true; //true当前开启扫描状态 false 当前是关闭扫描状态
|
|
|
|
Animator ani; //扫描动画
|
|
|
|
private WebCamTexture m_webCameraTexture;//摄像头实时显示的画面
|
|
private BarcodeReader m_barcodeRender; //申请一个读取二维码的变量
|
|
//存放二维码的纹理图片
|
|
Texture2D encoded;
|
|
|
|
[Header("显示摄像头画面的RawImage")]
|
|
public RawImage m_cameraTexture;
|
|
|
|
[Header("扫描间隔")]
|
|
public float m_delayTime = 3f;
|
|
|
|
[Header("开启扫描按钮")]
|
|
public Button openScanBtn;
|
|
|
|
[Header("扫描按钮")]
|
|
public Button ScanBtn;
|
|
|
|
void Start()
|
|
{
|
|
Debug.Log("ScanQRCode.Start()");
|
|
/*初始化纹理图片
|
|
* 注意:宽高度大小必须是256,
|
|
* 否则出现索引超出数组边界错误
|
|
*/
|
|
encoded = new Texture2D(256, 256);
|
|
CreatQr("http://192.168.1.88/T");
|
|
|
|
ScanBtn.onClick.AddListener(()=>{
|
|
StartScanQRCode((rs) =>
|
|
{
|
|
CreatQr(rs);
|
|
});
|
|
});
|
|
return;
|
|
try
|
|
{
|
|
//摄像机权限
|
|
#if UNITY_WEBGL
|
|
if (LoadDll.m_bInWX)
|
|
{
|
|
AppAuthorizeSetting au = WX.GetAppAuthorizeSetting();
|
|
if (au.cameraAuthorized != "authorized")
|
|
{//未授权
|
|
AuthorizeOption opt = new AuthorizeOption();
|
|
opt.complete = (rs) => { Debug.Log($"Cam Authorze complete {rs.errMsg}"); };
|
|
opt.success = (rs) => { Debug.Log($"Cam Authorze success {rs.errMsg}"); };
|
|
opt.fail = (rs) => { Debug.Log($"Cam Authorze fail {rs.errMsg}"); };
|
|
WX.Authorize(opt);
|
|
return;
|
|
}
|
|
}
|
|
else
|
|
#endif
|
|
{
|
|
Application.RequestUserAuthorization(UserAuthorization.WebCam);
|
|
}
|
|
|
|
InitQRScan();
|
|
}
|
|
catch(System.Exception e)
|
|
{
|
|
Debug.Log($"{e.Message}");
|
|
}
|
|
|
|
}
|
|
|
|
void InitQRScan()
|
|
{
|
|
WebCamDevice[] tDevices = WebCamTexture.devices; //获取所有摄像头
|
|
if (tDevices.Length > 0)
|
|
{
|
|
string tDeviceName = tDevices[0].name; //获取第一个摄像头,用第一个摄像头的画面生成图片信息
|
|
m_webCameraTexture = new WebCamTexture(tDeviceName, 400, 300);//名字,宽,高
|
|
m_cameraTexture.texture = m_webCameraTexture; //赋值图片信息
|
|
m_webCameraTexture.Play(); //开始实时显示
|
|
m_barcodeRender = new BarcodeReader();
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("No Camera!");
|
|
return;
|
|
}
|
|
ani = GetComponent<Animator>();
|
|
|
|
OpenScanQRCode(); //默认不扫描
|
|
//按钮监听
|
|
openScanBtn.onClick.AddListener(OpenScanQRCode);
|
|
}
|
|
|
|
#region 生成二维码
|
|
|
|
/// <summary>
|
|
/// 创建二维码
|
|
/// </summary>
|
|
public void CreatQr(string QrCodeStr)
|
|
{
|
|
if (QrCodeStr != string.Empty)
|
|
{
|
|
//二维码写入图片
|
|
var color32 = Encode(QrCodeStr, encoded.width, encoded.height);
|
|
encoded.SetPixels32(color32); //更改纹理的像素颜色
|
|
encoded.Apply();
|
|
//生成的二维码图片附给RawImage
|
|
m_cameraTexture.texture = encoded;
|
|
}
|
|
else
|
|
Debug.Log("没有生成信息");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 生成二维码
|
|
/// </summary>
|
|
/// <param name="textForEncoding">需要生产二维码的字符串</param>
|
|
/// <param name="width">宽</param>
|
|
/// <param name="height">高</param>
|
|
/// <returns></returns>
|
|
private static Color32[] Encode(string formatStr, int width, int height)
|
|
{
|
|
|
|
//绘制二维码前进行一些设置
|
|
QrCodeEncodingOptions options = new QrCodeEncodingOptions();
|
|
|
|
//设置字符串转换格式,确保字符串信息保持正确
|
|
options.CharacterSet = "UTF-8";
|
|
|
|
//设置绘制区域的宽度和高度的像素值
|
|
options.Width = width;
|
|
options.Height = height;
|
|
|
|
//设置二维码边缘留白宽度(值越大留白宽度大,二维码就减小)
|
|
options.Margin = 1;
|
|
|
|
/*实例化字符串绘制二维码工具
|
|
* BarcodeFormat:条形码格式
|
|
* Options: 编码格式(支持的编码格式)
|
|
*/
|
|
var barcodeWriter = new BarcodeWriter { Format = BarcodeFormat.QR_CODE, Options = options };
|
|
//进行二维码绘制并进行返回图片的颜色数组信息
|
|
return barcodeWriter.Write(formatStr);
|
|
|
|
}
|
|
#endregion
|
|
|
|
#region 扫描二维码
|
|
|
|
void StartScanQRCode(Action<string> callback)
|
|
{
|
|
#if UNITY_WEBGL
|
|
if (LoadDll.m_bInWX)
|
|
{
|
|
ScanCodeOption sco = new ScanCodeOption();
|
|
sco.success = (rs) => { Debug.Log($"Cam Scan success {rs.result}\n {rs.path}\n {rs.rawData}"); callback(rs.rawData); };
|
|
sco.fail = (rs) => { Debug.Log($"Cam Scan fail {rs.errMsg}"); callback(""); };
|
|
|
|
WX.ScanCode(sco);
|
|
Debug.Log("WX Scan ScanCode");
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("WebGL Scan Not Ready");
|
|
}
|
|
#endif
|
|
}
|
|
|
|
//开启关闭扫描二维码
|
|
void OpenScanQRCode()
|
|
{
|
|
if (isOpen)
|
|
{
|
|
//开启状态,需要关闭扫描
|
|
ani.Play("CloseScan", 0, 0);
|
|
//CancelInvoke("CheckQRCode");
|
|
Debug.Log("CloseScan ");
|
|
}
|
|
else
|
|
{
|
|
//关闭状态,需要开启扫描
|
|
|
|
//开始扫描
|
|
ani.Play("OpenScan", 0, 0);
|
|
|
|
//以秒为单位调用方法
|
|
//InvokeRepeating("CheckQRCode", 0, m_delayTime);
|
|
Debug.Log("OpenScan ");
|
|
}
|
|
isOpen = !isOpen;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 检索二维码方法
|
|
/// <summary>
|
|
/// 检索二维码方法
|
|
/// </summary>
|
|
public void CheckQRCode()
|
|
{
|
|
//存储摄像头画面信息贴图转换的颜色数组
|
|
Color32[] m_colorData = m_webCameraTexture.GetPixels32();
|
|
|
|
//将画面中的二维码信息检索出来
|
|
var tResult = m_barcodeRender.Decode(m_colorData, m_webCameraTexture.width, m_webCameraTexture.height);
|
|
|
|
if (tResult != null)
|
|
{
|
|
//Application.OpenURL(tResult.Text);
|
|
Debug.Log(tResult.Text);
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("CheckQRCode Error");
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
}
|