将二维码扫描到的信息 post 到服务器
/****************************************************************************
* 2021.3 DESKTOP-J98GMVJ
****************************************************************************/
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using ZXing;
using System.Collections;
using UnityEngine.Networking;
namespace QFramework.Example
{
public partial class LoginCamRaw : UIComponent
{
//摄像头实时显示的画面
private WebCamTexture m_webCameraTexture;
//申请一个读取二维码的变量
private BarcodeReader m_barcodeRender = new BarcodeReader();
//多久检索一次二维码
private float m_delayTime = 1f;
public RawImage m_cameraTexture;
private void Awake()
{
}
IEnumerator Start()
{
//yield return new WaitForSeconds(1f);
yield return new WaitForEndOfFrame();
//调用摄像头并将画面显示在屏幕RawImage上
WebCamDevice[] tDevices = WebCamTexture.devices; //获取所有摄像头
string tDeviceName = tDevices[0].name; //获取第一个摄像头,用第一个摄像头的画面生成图片信息
m_webCameraTexture = new WebCamTexture(tDeviceName, 1920, 1080); //名字,宽,高
if (m_cameraTexture == null) m_cameraTexture = GetComponent<RawImage>();
m_cameraTexture.texture = m_webCameraTexture; //赋值图片信息
m_webCameraTexture.Play(); //开始实时显示
InvokeRepeating("CheckQRCode", 0, m_delayTime);
}
/// <summary>
/// 检索二维码方法
/// </summary>
void CheckQRCode()
{
//存储摄像头画面信息贴图转换的颜色数组
Color32[] m_colorData = m_webCameraTexture.GetPixels32();
//将画面中的二维码信息检索出来
var tResult = m_barcodeRender.Decode(m_colorData, m_webCameraTexture.width, m_webCameraTexture.height);
if (tResult != null)
{
Debug.Log(tResult.Text);
// 发送到服务器进行比对
PostToServer(tResult.Text);
}
}
public string m_sServerAddress => "http://127.0.0.1/";
public string m_sPostMsg => "https://www.shengyinyouju.cn/cBBQE6uuDk4g=";
// 外部调用 post 到服务器方法
public void PostToServer(string m_sPostMsg)
{
StartCoroutine(Post(m_sPostMsg));
}
IEnumerator Post(string m_sPostMsg)
{
WWWForm form = new WWWForm();
//键值对
form.AddField("url", m_sPostMsg);
UnityWebRequest webRequest = UnityWebRequest.Post(m_sServerAddress, form);
yield return webRequest.SendWebRequest();
//异常处理,很多博文用了error!=null这是错误的,请看下文其他属性部分
if (webRequest.isHttpError || webRequest.isNetworkError)
Debug.Log(webRequest.error);
else
{
Debug.Log(webRequest.downloadHandler.text);
if(webRequest.downloadHandler.text == "1")
{
// 识别到正确的二维码 打开开始界面
UIKit.ClosePanel<UIQRCodeLoginPanel>();
UIKit.OpenPanel<UIStartPanel>();
}
}
}
protected override void OnBeforeDestroy()
{
StopAllCoroutines();
CancelInvoke();
if (m_webCameraTexture.isPlaying)
{
//Debug.LogError("关闭摄像头!");
m_webCameraTexture.Stop();
}
}
}
}
下面代码出处: https://blog.csdn.net/FransicZhang/article/details/94917700
using UnityEngine;
using UnityEngine.UI;
using ZXing;
using ZXing.QrCode;
public class QRCode : MonoBehaviour
{
//摄像头实时显示的画面
private WebCamTexture m_webCameraTexture;
//申请一个读取二维码的变量
private BarcodeReader m_barcodeRender = new BarcodeReader();
//多久检索一次二维码
private float m_delayTime = 1f;
public RawImage m_cameraTexture;
void Start()
{
//调用摄像头并将画面显示在屏幕RawImage上
WebCamDevice[] tDevices = WebCamTexture.devices; //获取所有摄像头
string tDeviceName = tDevices[0].name; //获取第一个摄像头,用第一个摄像头的画面生成图片信息
m_webCameraTexture = new WebCamTexture(tDeviceName, 1280, 720); //名字,宽,高
if (m_cameraTexture == null) m_cameraTexture = GetComponent<RawImage>();
m_cameraTexture.texture = m_webCameraTexture; //赋值图片信息
m_webCameraTexture.Play(); //开始实时显示
InvokeRepeating("CheckQRCode", 0, m_delayTime);
}
/// <summary>
/// 检索二维码方法
/// </summary>
void CheckQRCode()
{
//存储摄像头画面信息贴图转换的颜色数组
Color32[] m_colorData = m_webCameraTexture.GetPixels32();
//将画面中的二维码信息检索出来
var tResult = m_barcodeRender.Decode(m_colorData, m_webCameraTexture.width, m_webCameraTexture.height);
if (tResult != null)
{
Debug.Log(tResult.Text);
}
}
}