using UnityEngine;
using System.Collections;
//using ZXing;
//using ZXing.QrCode;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class QRcode : MonoBehaviour
{
public GameObject rImage;
public GameObject musicRawImage;
public AudioSource AudioS;
public AudioClip[] AudioC;
public Text Text;
public Text MusicNameText;
//摄像头实时显示的画面
private WebCamTexture m_webCameraTexture;
//申请一个读取二维码的变量
private BarcodeReader m_barcodeRender = new BarcodeReader();
//多久检索一次二维码
private float m_delayTime = 3f;
public RawImage m_cameraTexture;
private void Awake()
{
rImage.SetActive(true);
musicRawImage.SetActive(false);
}
void Start()
{
//调用摄像头并将画面显示在屏幕RawImage上
WebCamDevice[] tDevices = WebCamTexture.devices; //获取所有摄像头
string tDeviceName = tDevices[0].name; //获取第一个摄像头,用第一个摄像头的画面生成图片信息
m_webCameraTexture = new WebCamTexture(tDeviceName, 400, 300); //名字,宽,高
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);
}
}