拷贝下来 直接用。
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 CameraImageAccess : MonoBehaviour { 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; void Start() { } //void OnApplicationQuit() //{ //} void OnEnable() { VuforiaBehaviour vuforiaBehaviour = (VuforiaBehaviour)FindObjectOfType(typeof(VuforiaBehaviour)); if (vuforiaBehaviour) { vuforiaBehaviour.RegisterTrackablesUpdatedCallback(OnTrackablesUpdated); } var isAutoFocus = CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO); if (!isAutoFocus) { CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_NORMAL); } //Debug.Log(String.Format("AutoFocus : {0}", isAutoFocus)); if (!m_RegisteredFormat) { Vuforia.CameraDevice.Instance.SetFrameFormat(m_PixelFormat, true); m_RegisteredFormat = true; } qrThread = new Thread(DecodeQR); qrThread.Start(); } void OnDisable() { Debug.Log("OnDisable"); VuforiaBehaviour vuforiaBehaviour = (VuforiaBehaviour)FindObjectOfType(typeof(VuforiaBehaviour)); if (vuforiaBehaviour) { vuforiaBehaviour.UnregisterTrackablesUpdatedCallback(OnTrackablesUpdated); } qrThread.Abort(); qrThread = null; } public void OnTrackablesUpdated() { Vuforia.CameraDevice cam = Vuforia.CameraDevice.Instance; 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 (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; //if (gotResult) SceneManager.LoadScene(0); } void DecodeQR() { // create a reader with a custom luminance source var barcodeReader = new BarcodeReader { AutoRotate = false, TryHarder = 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.LogError(e.Message); } } else { } Thread.Sleep(200); } } 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; } }
时间: 2024-10-05 05:06:54