1. Common generation and identification of QR codes
1. Forage Two-dimensional code Https://cli.im/text
2. Realization of generating and scanning QR code using zxing in software
Zxing is an open source project written in Java, and Zxing.net is ported to. NET Engineering.
Https://github.com/micjahn/ZXing.Net
2. realize the identification of two-dimensional code
1.Unity Engineering
2. Let rawimage display the camera content
Using system.collections;using system.collections.generic;using unityengine;using zxing;using UnityEngine.UI;public Class Qrcodetest:monobehaviour {public rawimage cameratexture; Private Webcamtexture webcamtexture;//Use this for initializationvoid Start () { webcamdevice[] devices = Webcamtextu re.devices; string devicename = Devices[0].name; Webcamtexture = new Webcamtexture (DeviceName, N.); Cameratexture.texture = webcamtexture; Webcamtexture.play ();} Update is called once per framevoid update () {}}
3. Scan function Implementation code (code a bit long, look slowly)
Using system.collections;using system.collections.generic;using unityengine;using zxing;using UnityEngine.UI;public Class Qrcodetest:monobehaviour {public rawimage cameratexture;//storage camera captured content private Webcamtexture webcamtexture;// The contents of the camera color32[] data; Barcodereader Barcodereader;//zxing provides a way to read the camera contents float interval = 0f;//do the timer with//use the For Initializationvoid Start () {//Open camera webcamdevice[] devices = webcamtexture.devices; string devicename = Devices[0].name; Webcamtexture = new Webcamtexture (DeviceName, 400, 300); Cameratexture.texture = webcamtexture; Webcamtexture.play (); Barcodereader = new Barcodereader ();} Update is called once per framevoid update () {interval + = Time.deltatime; if (interval >= 3f) {scanqrcode (); Interval = 0f; }} void Scanqrcode () {//getpixels32 is the way to convert the format to COLOR32 data = Webcamtexture.getpixels32 (); Result store reads the content var result = Barcodereader.decode (data, webcamtexture.width, webcamtexture.height); if (result! = null) {Debug.Log (result). Text); } }}
3. realize the generation of two-dimensional code (note: I can also successfully identify the generated QR code when I turn off the network, which means it is offline)
1. Create a new rawimage storage generated identification map
2. Added a method to generate a QR code:
Using system.collections;using system.collections.generic;using unityengine;using zxing;using UnityEngine.UI;public Class Qrcodetest:monobehaviour {public rawimage cameratexture;//storage camera captured content public Rawimage qrcode;//store generated QR code p Rivate webcamtexture webcamtexture;//camera content color32[] data; Barcodereader Barcodereader;//zxing provides methods for reading the contents of the camera Barcodewriter Barcodewriter;//zxing provides methods for writing content float interval = 0f;// Do the timer with//use the For Initializationvoid Start () {//Open the camera webcamdevice[] devices = webcamtexture.devices; string devicename = Devices[0].name; Webcamtexture = new Webcamtexture (DeviceName, 400, 300); Cameratexture.texture = webcamtexture; Webcamtexture.play (); Barcodereader = new Barcodereader ();} Update is called once per framevoid update () {interval + = Time.deltatime; if (interval >= 3f) {scanqrcode (); Interval = 0f; }//Press SPACEBAR to generate two-dimensional code if (Input.getkeydown(Keycode.space)) {//In this notation can only fill in the Showqrcode ("I love Learning", 256, 256); What if you want a QR code of another size? See the article at the bottom of the link}}//Scan the QR code method void Scanqrcode () {//getpixels32 is the way to get the color from a picture data = webcamtexture.ge TPixels32 (); Result store reads the contents of var result = Barcodereader.decode (data, webcamtexture.width, webcamtexture.height); if (result! = null) {Debug.Log (result). Text); }}//show generated QR code void Showqrcode (string str,int width,int height) {//define TEXTURE2D and fill texture2d t = n EW texture2d (width, height); T.SETPIXELS32 (Geneqrcode (str, width, height)); T.apply (); Qrcode.texture = t; }//return Color32 Image Color method color32[] Geneqrcode (string formatstr,int width,int height) {ZXing.QrCode.QrCodeEncodin GOptions options = new ZXing.QrCode.QrCodeEncodingOptions (); Options. CharacterSet = "UTF-8";//Set character encoding, otherwise Chinese cannot display options. width = width; Options. Height = width; Options. margin = 1;//Two-dimensional code distance Edge Barcodewriter = new Barcodewriter {Format = ZXing.BarcodeFormat.QR_CODE, options = Options }; Return Barcodewriter.write (FORMATSTR); }}
More:
Pit http://blog.sina.com.cn/s/blog_6ad33d350102xj8l.html generated by ZXing two-dimensional code
zxing.net Read and save QR Code, set Chinese character sets, read Chinese QR code https://bbs.csdn.net/topics/391950715?page=1
The two-dimensional Code of Unity plugin series