孙广东 2016.7.9
http://blog.csdn.net/u010019717
扫描二维码 需要类库 zxing.unity.dll
http://zxingnet.codeplex.com/ Unity可以使用!
普通的 Unity 不会用到扫描二维码的功能, 但是在 AR 产品上有应用了!!!!
参考这个AR产品 :https://detail.tmall.com/item.htm?spm=a220m.1000858.1000725.1.a7gnRz&id=529056403159&areaId=110100&cat_id=2&rn=22493d7ebf3a196f610ce18fa2504dc0&user_id=2090287044&is_b=1
购买完内容(AR需要识别的图案卡片 和 激活卡) 要使用通过下载APP扫描(手机,平板都行) 激活卡的二维码, 设备(安装APP的手机或者平板)才能被解锁!
APP升级不用重新扫描激活卡,说明是激活的设备不是APP.
http://blog.csdn.net/u010019717
using System.Threading; using UnityEngine.UI; using UnityEngine; using System.Collections; using ZXing; using ZXing.QrCode; public class BarcodeCam : MonoBehaviour { public Texture2D encoded; public RawImage camsx; // UI上用于显示摄像头显示的内容 public Text Rstext; // ZXing解析的 结果 //private string TESTstr; // 网络返回的验证数据结果 private int sw, sh; public static string driverID; // 设备唯一标识 // 代表着摄像机 private WebCamTexture camTexture; private Thread qrThread; private Color32[] c; private int cw, ch; private Rect screenRect; private bool isQuit; public string LastResult; private string QRscan; private bool shouldEncodeNow; void OnGUI() { GUI.Label(screenRect, driverID); } void OnEnable() { // 开启相机 if (camTexture != null) { camTexture.Play(); cw = camTexture.width; ch = camTexture.height; } } void OnDisable() { // 暂停 相机 if (camTexture != null) { camTexture.Pause(); } } void OnDestroy() { qrThread.Abort(); camTexture.Stop(); } void OnApplicationQuit() { isQuit = true; } void Start() { // 设备唯一 ID driverID = SystemInfo.deviceUniqueIdentifier; encoded = new Texture2D(256, 256); LastResult = "www.baidu.com"; shouldEncodeNow = false; screenRect = new Rect(0, 0, Screen.width, Screen.height); camTexture = new WebCamTexture(); if (Screen.width < 1280) { sw = 640; } else { sw = 1280; } // 在 Unity界面上显示 扫描的内容 camTexture.requestedHeight = 720; camTexture.requestedWidth = sw; camsx.texture = camTexture; camsx.material.mainTexture = camTexture; OnEnable(); qrThread = new Thread(DecodeQR); qrThread.Start(); } void Update() { // 获取扫描的内容 if ((c == null) && (camTexture.isPlaying)) { c = camTexture.GetPixels32(); cw = camTexture.width; ch = camTexture.height; } Rstext.text = LastResult; camsx.enabled = false; camsx.enabled = true; var textForEncoding = LastResult; if (shouldEncodeNow && textForEncoding != null) { var color32 = Encode(textForEncoding, encoded.width, encoded.height); encoded.SetPixels32(color32); encoded.Apply(); shouldEncodeNow = false; if (QRscan != LastResult) { QRscan = LastResult; // todo 如果遇到新的 二维码那么就执行 其他的操作(网络请求验证啊等等) //StartCoroutine(CheckCode(LastResult)); } } } // 解码 扫描的内容 void DecodeQR() { var barcodeReader = new BarcodeReader { AutoRotate = false, TryHarder = false }; while (true) { if (isQuit) break; try { if (c != null) { var result = barcodeReader.Decode(c, cw, ch); if (result != null) { LastResult = result.Text; shouldEncodeNow = true; } Thread.Sleep(200); c = null; } } catch { } } } private 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); } }