/* * Copyright 2012 ZXing.Net authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System.Threading; using UnityEngine; using ZXing; using ZXing.QrCode; public class BarcodeCam : MonoBehaviour { public BarcodeCam self; // Texture for encoding test public Texture2D encoded; private WebCamTexture camTexture; private Thread qrThread; private Color32[] color32; private int W, H; private Rect screenRect; private bool isQuit; public string LastResult; private bool shouldEncodeNow; public BarcodeCam() { self = this; } void Start() { encoded = new Texture2D(256, 256); LastResult = "http://www.google.com"; shouldEncodeNow = true; screenRect = new Rect(Screen.width * 0.1f/2, -Screen.height * 0.2f/2, Screen.width*0.9f, Screen.height*0.8f); camTexture = new WebCamTexture(); camTexture.requestedHeight = Screen.height; // 480; camTexture.requestedWidth = Screen.width; //640; OnEnable(); qrThread = new Thread(DecodeQR); qrThread.Start(); } void OnGUI() { GUI.DrawTexture(screenRect, camTexture, ScaleMode.ScaleToFit); } void OnEnable() { if (camTexture != null) { camTexture.Play(); W = camTexture.width; H = camTexture.height; if (color32 == null) { color32 = camTexture.GetPixels32(); } } } void OnDisable() { if (camTexture != null) { camTexture.Pause(); } } void OnDestroy() { if(qrThread != null) qrThread.Abort(); if(camTexture != null) camTexture.Stop(); } // It's better to stop the thread by itself rather than abort it. void OnApplicationQuit() { isQuit = true; } //void Update() //{ // encode the last found // var textForEncoding = LastResult; // if (shouldEncodeNow && // textForEncoding != null) // { // var c = Encode(textForEncoding, encoded.width, encoded.height); // encoded.SetPixels32(c); // encoded.Apply(); // shouldEncodeNow = false; // } //} void DecodeQR() { // create a reader with a custom luminance source var barcodeReader = new BarcodeReader { AutoRotate = false, TryHarder = false }; while (true) { if (isQuit) break; try { if (color32 != null) { // decode the current frame var result = barcodeReader.Decode(color32, W, H); if (result != null) { LastResult = result.Text; shouldEncodeNow = true; print(result.Text); } } // Sleep a little bit and set the signal to get the next frame Thread.Sleep(200); color32 = null; } catch(System.Exception e) { Debug.LogError(e); } } } public static Color32[] Encode(string textForEncoding, int width, int height) { var writer = new BarcodeWriter { Format = BarcodeFormat.QR_CODE, Options = new QrCodeEncodingOptions { Height = height, Width = width } }; return writer.Write(textForEncoding); } }