├── README.md └── zxing.unity.dll /README.md: -------------------------------------------------------------------------------- 1 | # qr-code-unity-3d-read-generate 2 | Generating a QR code / Scanning a QR code in Unity 3D 3 | 4 | At least at the time when I needed this feature, there were barely any usable tutorials or QR libraries ready to use for Unity (given the older .net framework it uses). 5 | 6 | Took me a few days of trial and error until finding and properly building the ZXing library DLL which you can find in the repo. 7 | 8 | # Read a QR 9 | ``` 10 | using ZXing; 11 | using ZXing.QrCode; 12 | 13 | private WebCamTexture camTexture; 14 | private Rect screenRect; 15 | void Start() { 16 | screenRect = new Rect(0, 0, Screen.width, Screen.height); 17 | camTexture = new WebCamTexture(); 18 | camTexture.requestedHeight = Screen.height; 19 | camTexture.requestedWidth = Screen.width; 20 | if (camTexture != null) { 21 | camTexture.Play(); 22 | } 23 | } 24 | 25 | void OnGUI () { 26 | // drawing the camera on screen 27 | GUI.DrawTexture (screenRect, camTexture, ScaleMode.ScaleToFit); 28 | // do the reading — you might want to attempt to read less often than you draw on the screen for performance sake 29 | try { 30 | IBarcodeReader barcodeReader = new BarcodeReader (); 31 | // decode the current frame 32 | var result = barcodeReader.Decode(camTexture.GetPixels32(), camTexture.width , camTexture.height); 33 | if (result != null) { 34 | Debug.Log(“DECODED TEXT FROM QR: “ + result.Text); 35 | } 36 | } catch(Exception ex) { Debug.LogWarning (ex.Message); } 37 | } 38 | ``` 39 | 40 | 41 | # Generate one 42 | ``` 43 | private static Color32[] Encode(string textForEncoding, int width, int height) { 44 | var writer = new BarcodeWriter { 45 | Format = BarcodeFormat.QR_CODE, 46 | Options = new QrCodeEncodingOptions { 47 | Height = height, 48 | Width = width 49 | } 50 | }; 51 | return writer.Write(textForEncoding); 52 | } 53 | 54 | public Texture2D generateQR(string text) { 55 | var encoded = new Texture2D (256, 256); 56 | var color32 = Encode(text, encoded.width, encoded.height); 57 | encoded.SetPixels32(color32); 58 | encoded.Apply(); 59 | return encoded; 60 | } 61 | 62 | Texture2D myQR = generateQR("test"); 63 | if (GUI.Button (new Rect (300, 300, 256, 256), myQR, GUIStyle.none)) {} 64 | ``` 65 | 66 | 67 | I am using Unity 5.3.5 68 | -------------------------------------------------------------------------------- /zxing.unity.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nenuadrian/qr-code-unity-3d-read-generate/cef5224b90c331fd15cc52d44f4bcf860e0b49dc/zxing.unity.dll --------------------------------------------------------------------------------