/// <summary> /// This Update Loop is used to : /// * Wait the Camera is really ready /// * Bring back Callback to the main thread when using Background Thread /// * To execute image Decoding When not using the background Thread /// </summary> public void Update() { // If not ready, wait if (!Camera.IsReady()) { Debug.Log(this + " Camera Not Ready Yet ..."); if (status != BarcodeScannerStatus.Initialize) { Status = BarcodeScannerStatus.Initialize; } return; } // If the app start for the first time (select size & onReady Event) if (Status == BarcodeScannerStatus.Initialize) { if (WebcamInitialized()) { Debug.Log(this + " Camera is Ready "); Status = BarcodeScannerStatus.Paused; if (OnReady != null) { OnReady.Invoke(this, EventArgs.Empty); } } } if (Status == BarcodeScannerStatus.Running) { // Call the callback if a result is there if (Result != null) { // //Log.Info(Result); Callback(Result.Type, Result.Value); // clean and return Result = null; parserPixelAvailable = false; return; } // Get the image as an array of Color32 pixels = Camera.GetPixels(pixels); parserPixelAvailable = true; // If background thread OFF, do the decode main thread with 500ms of pause for UI if (!Settings.ScannerBackgroundThread && mainThreadLastDecode < Time.realtimeSinceStartup - Settings.ScannerDecodeInterval) { DecodeQR(); mainThreadLastDecode = Time.realtimeSinceStartup; } } }
public InitiateBarcodeScanner(ScannerSettings settings, IBarcodeParser parser, IWebcam webcam) { // Check Device Authorization if (!Application.HasUserAuthorization(UserAuthorization.WebCam)) { throw new Exception("This Webcam Library can't work without the webcam authorization"); } Status = BarcodeScannerStatus.Initialize; // Default Properties Settings = (settings == null) ? new ScannerSettings() : settings; Parser = (parser == null) ? new ZXingParser(Settings) : parser; Camera = (webcam == null) ? new UnityWebcam(Settings) : webcam; }
/// <summary> /// Used to start Scanning /// </summary> /// <param name="callback"></param> public void Scan(Action <string, string> callback) { if (Callback != null) { Debug.Log(this + " Already Scan"); return; } Callback = callback; Debug.Log(this + " SimpleScanner -> Start Scan"); Status = BarcodeScannerStatus.Running; #if !UNITY_WEBGL if (Settings.ScannerBackgroundThread) { CodeScannerThread = new Thread(ThreadDecodeQR); CodeScannerThread.Start(); } #endif }
/// <summary> /// Used to Stop Scanning internaly (can be forced) /// </summary> private void Stop(bool forced) { if (!forced && Callback == null) { //Log.Warning(this + " No Scan running"); return; } // Stop thread / Clean callback Debug.Log(this + " SimpleScanner -> Stop Scan"); // #if !UNITY_WEBGL // if (CodeScannerThread != null) // { // CodeScannerThread.Abort(); // } // #endif Callback = null; Status = BarcodeScannerStatus.Paused; }