Vuforia+二维码识别
简单粗暴直接上脚本
using UnityEngine;
using System.Collections;
using System;
using Vuforia;
using System.Threading;
using ZXing;
using ZXing.QrCode;
using ZXing.Common;
using UnityEngine.SceneManagement;
public class CameraScanQRCode : MonoBehaviour
{
public static CameraScanQRCode cameraScanQRCodeInstance;
private Image.PIXEL_FORMAT m_PixelFormat = Image.PIXEL_FORMAT.GRAYSCALE;
private bool m_RegisteredFormat = false;
public bool reading;
public string QRMessage;
// public UnityEngine.UI.Text labelQrc;
Thread qrThread;
//bool isQuit;
private Color32[] c;
private int W, H;
Image QCARoutput;
bool updC;
bool gotResult = false;
//添加播放扫描成功音频特效
private bool isAleardyPlayAudio=false;
private void Awake()
{
cameraScanQRCodeInstance = this;
//Cursor.visible = false;
}
void Start()
{
//打包会报错
CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
}
public void ShowScannerInformation(bool IsShowInfo)
{
//Debug.LogWarning ("this is ShowScannerInformation"+IsShowInfo);
//labelQrc.enabled = IsShowInfo;
//labelQrc.text = QRMessage;
}
void SetCameraFocus ()
{
var isAutoFocus = CameraDevice.Instance.SetFocusMode (CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
if (!isAutoFocus) {
CameraDevice.Instance.SetFocusMode (CameraDevice.FocusMode.FOCUS_MODE_NORMAL);
//Debug.LogError ("*********Field to set fouce mode (unsupported mode )*****");
}
// Debug.LogError ("*********Field to set fouce mode (unsupported mode )*****");
}
//void OnApplicationQuit()
//{
//}
////private void OnApplicationPause(bool pause)
////{
//// if(pause)
//// {
//// Debug.LogError("pause");
//// reading = true;
//// }
////}
void OnEnable()
{
//1
//VuforiaBehaviour vuforiaBehaviour =(VuforiaBehaviour)FindObjectOfType(typeof(VuforiaBehaviour));
//1
//vuforiaBehaviour.RegisterTrackablesUpdatedCallback(OnTrackablesUpdated);
VuforiaARController.Instance.RegisterTrackablesUpdatedCallback (OnTrackablesUpdated);
SetCameraFocus ();
//Debug.Log(String.Format("AutoFocus : {0}", isAutoFocus));
// if (!m_RegisteredFormat)
// { //1
// Vuforia.CameraDevice.Instance.SetFrameFormat(m_PixelFormat, true);
//
// m_RegisteredFormat = true;
// }
//
qrThread = new Thread(DecodeQR);
qrThread.Start();
}
void OnDisable()
{
//Debug.LogError("OnDisable");
//VuforiaBehaviour vuforiaBehaviour = (VuforiaBehaviour)FindObjectOfType(typeof(VuforiaBehaviour));
//vuforiaBehaviour.UnregisterTrackablesUpdatedCallback(OnTrackablesUpdated);
VuforiaARController.Instance.RegisterTrackablesUpdatedCallback(OnTrackablesUpdated);
qrThread.Abort();
qrThread = null;
}
//private void OnDestroy()
//{
// Debug.LogError("OnDestroy");
//}
//private void OnApplicationPause(bool pause)
//{
// Debug.LogError("OnApplicationPause");
//}
//private void OnApplicationFocus(bool focus)
//{
// Debug.LogError("OnApplicationFocus");
// VuforiaARController.Instance.RegisterTrackablesUpdatedCallback(OnTrackablesUpdated);
// SetCameraFocus();
// //qrThread.Resume();
//}
public void OnTrackablesUpdated()
{
Vuforia.CameraDevice cam = Vuforia.CameraDevice.Instance;
//1
if (!m_RegisteredFormat)
{
Vuforia.CameraDevice.Instance.SetFrameFormat(m_PixelFormat, true);
m_RegisteredFormat = true;
}//
QCARoutput = cam.GetCameraImage(m_PixelFormat);
if (QCARoutput != null)
{
reading = true;
updC = true;
}
else
{
reading = false;
// Debug.Log(m_PixelFormat + " image is not available yet");
}
}
void Update()
{
// if (QRMessage!=""&& TaskAudioManager.TaskAudioManagerInstance.playTaskAudio.isPlaying == false)
{
// QRCodeScannerSuccess();
}
//Cursor.visible = false;
// CameraDevice.Instance.SetFocusMode (CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
//SetCameraFocus ();
if (reading)
{
if (QCARoutput != null)
{
if (updC)
{
updC = false;
Invoke("ForceUpdateC", 1f);//here we'll postpone next c update, this function literally just switches updC to true
if (QCARoutput == null)
{
return;
}
c = null;
c = ImageToColor32(QCARoutput);
if (W == 0 | H == 0)
{
W = QCARoutput.BufferWidth;
H = QCARoutput.BufferHeight;
}
QCARoutput = null;
}
}
}
// labelQrc.text = QRMessage;
ShowScannerInformation(true);
//if (gotResult) SceneManager.LoadScene(0);
}
void DecodeQR()
{
// create a reader with a custom luminance source
// var barcodeReader = new BarcodeReader { AutoRotate = false, TryHarder = false };
var barcodeReader = new BarcodeReader { AutoRotate = false, TryInverted = false };
barcodeReader.ResultFound += OnResultF;
while (true)
{
//if (isQuit)//This one is used to be true in OnApplicationQuit() See ZXing's tutorial for more info
// break;
if (reading && c != null)
{
try
{
// decode the current frame
ZXing.Result result = barcodeReader.Decode(c, W, H);
c = null;
if (result != null)
{
QRMessage = result.Text;
//download = true;
//reading = false;
Debug.Log(QRMessage);
}
}
catch(Exception e)
{
Debug.Log(e.Message);
}
}
else
{
}
Thread.Sleep(1);
}
}
//添加播放扫描成功音频特效.snail
private void QRCodeScannerSuccess()
{
if (!isAleardyPlayAudio)
{
isAleardyPlayAudio = true;
TaskAudioManager.TaskAudioManagerInstance.PlayGameAudioByID((int)GameAudioType.QRCodeScannerSuccess);
}
}
void OnResultF(Result result)
{
//Debug.Log(result.Text);
gotResult = true;
//SceneManager.LoadScene(0);
}
void ForceUpdateC()
{ //To set it to update later
updC = true;
}
Color32[] ImageToColor32(Vuforia.Image a)
{
if (!a.IsValid()) return null;
Color32[] r = new Color32[a.BufferWidth * a.BufferHeight];
for (int i = 0; i < r.Length; i++)
{
r[i].r = r[i].g = r[i].b = a.Pixels[i];
}
return r;
}
}

浙公网安备 33010602011771号