In unity, the function of scanning QR codes is used. Although there are plug-ins, the mobile UI cannot be customized, so I have already made one, scan and parse it directly on the C# layer, and you can play it yourself on the UI.
On code:
This is the script that calls zxing.
using UnityEngine; using ; using ZXing; using ; public class QR { /// <summary> /// parse QR code /// </summary> /// <param name="tex"></param> /// <returns></returns> public static string Decode(Texture2D tex) { return DecodeColData(tex.GetPixels32(), , ); //Decode through reader } public static string DecodeColData(Color32[] data, int w, int h) { BarcodeReader reader = new BarcodeReader(); Result result = (data, w, h); //Decode through reader //(); if (result == null) return ""; else { return ; } } /// <summary> /// Generate QR code /// </summary> /// <param name="content"></param> /// <param name="len"></param> /// <returns></returns> public static Texture2D GetQRTexture(string content, int len = 256) { var bw = new BarcodeWriter(); = BarcodeFormat.QR_CODE; = new () { Height = len, Width = len }; var cols = (content); Texture2D t = new Texture2D(len, len); t.SetPixels32(cols); (); return t; } }
Then there is the encapsulation:
using UnityEngine; using ; using System; using ; using ; /// <summary> /// QR code analysis tool/// Key functions:/// public static QRHelper GetInst() --get a single case/// public event Action<string> OnQRScanned; --Scan the callback/// public void StartCamera(int index) --Start the camera/// public void StopCamera() --Stop the camera/// public void SetToUI(RawImage raw,int UILayoutW,int UILayoutH) --Set the camera screen to a rawimage and make it display full screen/// </summary> public class QRHelper { public event Action<string> OnQRScanned; private static QRHelper _inst; public static QRHelper GetInst() { if (_inst == null) { _inst = new QRHelper(); } return _inst; } private int reqW = 640; private int reqH = 480; private WebCamTexture webcam; Timer timer_in, timer_out; /// <summary> /// Start the camera /// </summary> /// <param name="index">The rear of the phone is 0 and the front is 1</param> public void StartCamera(int index) { StopCamera(); lock (mutex) { buffer = null; tbuffer = null; } var dev = ; webcam = new WebCamTexture(dev[index].name); = reqW; = reqH; (); stopAnalysis = false; InitTimer(); timer_in.Start(); timer_out.Start(); } /// <summary> /// stop /// </summary> public void StopCamera() { if (webcam!=null) { (); (webcam); (); webcam = null; stopAnalysis = true; timer_in.Stop(); timer_out.Start(); timer_in = null; timer_out = null; } } /// <summary> /// Set the camera screen to a rawimage and make it display full screen /// </summary> /// <param name="raw">rawimage</param> /// <param name="UILayoutW">Width when UI layout</param> /// <param name="UILayoutH">Height when UI layout</param> public void SetToUI(RawImage raw,int UILayoutW,int UILayoutH){ <RectTransform>().sizeDelta = GetWH(UILayoutW,UILayoutH); int d = -1; if () { d = 1; } <RectTransform>().localRotation *= (, ); float scaleY = ? -1.0f : 1.0f; = new Vector3(1, scaleY * 1, 0.0f); = webcam; = ; } // Calculate the width and height of the UI when considering possible rotation private Vector2 GetWH(int UILayoutW, int UILayoutH) { int Angle = ; Vector2 init = new Vector2(reqW, reqH); if ( Angle == 90 || Angle == 270 ) { var tar = (new Vector2(UILayoutH,UILayoutW)); return tar; } else { var tar = (new Vector2(UILayoutW, UILayoutH)); return tar; } } private void InitTimer() { timer_in = new Timer(500); timer_in.AutoReset = true; timer_in.Elapsed += (a,b) => { (WriteDataBuffer); }; timer_out = new Timer(900); timer_out.AutoReset = true; timer_out.Elapsed += (a,b)=>{ Analysis(); }; } private Color32[] buffer = null; private Color32[] tbuffer = null; private object mutex = new object(); private bool stopAnalysis = false; int dw, dh; private void WriteDataBuffer() { lock (mutex) { if (buffer == null && webcam!=null) { buffer = webcam.GetPixels32(); dw = ; dh = ; } } } //Analyze QR code private void Analysis() { if (!stopAnalysis) { lock (mutex) { tbuffer = buffer; buffer = null; } if (tbuffer == null) { ; } else { string str = (tbuffer, dw, dh); tbuffer = null; if (!() && OnQRScanned != null) { (() => { if (OnQRScanned!=null) OnQRScanned(str); }); } } } tbuffer = null; } }
The call method is as follows. When pureMVC is used, it may be a bit messy and cannot be used directly in your project. It mainly depends on how OnRegister and OnRemove start and stop, as well as how RegQRCB, RemoveQRCB, and OnQRSCcanned register, remove and respond to events scanned to QR code. In onregister, since the picture on iOS has a mirror, the y of the scale of rawimage is set to -1 to eliminate the mirror:
using UnityEngine; using ; using ; using ; using ; /// <summary> /// Scan the QR code interface logic/// </summary> public class ScanQRMediator : Mediator { AudioProxy audio; public QRView TarView { get { return as QRView; } } public ScanQRMediator() : base("ScanQRMediator") { } string NextView = ""; bool isInitOver = false; int cameraDelay = 1; public override void OnRegister() { (); if ( == ) { cameraDelay = 5; } else { cameraDelay = 15; } audio = <AudioProxy>("AudioProxy"); (BtnEscClick); ().StartCamera(0); = ; (cameraDelay, () => { RegQRCB(); ().SetToUI(, 1536, 2048); if ( == ) { = new Vector3(1, -1, 0); } isInitOver = true; }); (TarView); //Pause background music (false); } public override void OnRemove() { (); (BtnEscClick); if (NextView != "UnlockView") { (true); } NextView = ""; isInitOver = false; } bool isEsc = false; void BtnEscClick() { if (isEsc || !isInitOver) { return; } isEsc = true; = null; = ; RemoveQRCB(); ().StopCamera(); (cameraDelay, () => { isEsc = false; if ( == ) { ToUserInfoView(); } else { string origin = <string>("origin"); if (origin == "ARView") { ToARView(); } else if (origin == "UserInfoView") { ToUserInfoView(); } else { ToARView(); } } }); } void ToARView() { (); ().ShowView(TarView, "ARView", null); } void ToUserInfoView() { (); ().ShowView(TarView, "UserInfoView", null); var v = ().PeekTop(); var vc = new UserInfoMediator(); = v; (vc); } int reg = 0; void RegQRCB() { if (reg == 0) { ().OnQRScanned += OnQRScanned; reg = 1; } } void RemoveQRCB() { if (reg == 1) { ().OnQRScanned -= OnQRScanned; reg = 0; } } bool isQRJump = false; void OnQRScanned(string qrStr) { if (isQRJump) { return; } isQRJump = true; = null; = ; RemoveQRCB(); ().StopCamera(); NextView = "UnlockView"; (cameraDelay, () => { isQRJump = false; (); (); #if YX_DEBUG ("qr is :"+qrStr); (qrStr,1.5f); #endif ().ShowView(TarView, "UnlockView", ("QRCode", qrStr, "origin", <string>("origin"))); var v = ().PeekTop(); var vc = new UnlockMediator(); = v; (vc); }); } }
Finally, put it on and put it in the plugins.
The above code 5.1.2 test is available.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.