Unity implementation generates local QR code and accesses QR code content

Posted by chwebdesigns on Tue, 18 Jan 2022 22:46:20 +0100

Unity version: April 36, 2018 visual studio version: 2020; Reference Library: zxing unity. dll
Recently, in a face changing project, baidu face fusion API was selected for face fusion, Specific implementation documents:
The merged pictures are saved to the local host, the host port is opened, and the pictures can be saved to other devices in the LAN by accessing IP + port + picture name in the LAN;
1: Open local host port: open control panel - all control panel items - Windows Defender Firewall - Advanced Settings - right click inbound rules - new rules - create rule type selection (port) - rule application selection TCP - select specific local port - allow link - and then continue to fill in the name and description. Next step is complete (open the ECS port and add rules to the console security group on the website where the server is purchased)

2: Open the local IISSevers manager. If it is not installed, it needs to be installed, Specific installation tutorial Baidu ; After opening, select the website and right-click - add website - fill in the name casually, and fill in the local specified folder for the path - then select the port just opened (Port 88 is set here), and then confirm to start the website; Check whether the web browser input is configured correctly http://localhost:88/ , the website is displayed as shown in the following figure, the website is configured normally, and other devices in the LAN can also access the address;


3: The new Unity project has two versions tested this time, Unity 2020 3.25 when using WebCamTexture API, the image will get stuck (I don't know why, no research), Unity2018 4.36 the effect is very good. This version is selected for this test; Next, import zxing Unity. DLL file, the camera is set as the following figure; Create a new Quad object with the size set to (x,y,z)=(16.9.1)

4: New script quadgettexture CS hang on the new Quad object; The code is as follows:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;   

public class QuadGetTexture : MonoBehaviour
{ 
    private WebCamTexture webCamTexture;//Camera content 
    void Start()
    {
        
        //Turned on the camera
        WebCamDevice[] devices = WebCamTexture.devices;
        string deviceName = devices[0].name;
        webCamTexture = new WebCamTexture(deviceName, 400, 300);
        GetComponent<Renderer>().material.mainTexture = webCamTexture;
        webCamTexture.Play();  
    } 
}

5: Screenshot: save the picture to the path of the website just set;

6: Call ZXINGdll file to generate QR code and display it on the interface: create a new script getqrcode cs; Used as tools;

 using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using ZXing;
using ZXing.QrCode;
using ZXing.QrCode.Internal;

public class GetQRCode 
{ 
    /// <summary>
    ///Scan QR code
    /// </summary>
    ///< param name = "webcamtexture" > < / param > get camera content
    /// <returns></returns>
    public static string ScanQRCode(WebCamTexture webCamTexture)
    {
        Color32[] data;
        //Zxing read content
        BarcodeReader readReaderCode=new BarcodeReader();
        //Get the color on the picture 
        data = webCamTexture.GetPixels32();
        // Store read content
        ZXing.Result result = readReaderCode.Decode(data, webCamTexture.width, webCamTexture.height);

        if (result != null)
            return result.Text; 
        return "data is error!";
    }
    /// <summary>
    ///Generated QR code
    /// </summary>
    ///< param name = "STR" > < / param > display content
    ///< param name = "width" > < / param > width of QR code
    ///< param name = "height" > < / param > height of QR code
    public static Texture2D CreatQRCode(string str, int width, int height)
    {
        //Define texture2d and fill
        Texture2D t = new Texture2D(width, height);
        t.SetPixels32(GetColor32(str, width, height));
        t.Apply();
        return t; 
    }
    /// <summary>
    ///Return Color32 picture color
    /// </summary>
    /// <param name="str"></param>
    /// <param name="width"></param>
    /// <param name="height"></param>
    /// <returns></returns>
    public static Color32[] GetColor32(string  str, int width, int height)
    {

        QrCodeEncodingOptions options = new QrCodeEncodingOptions();
        //The UTF-8 code may be set in Chinese,
        options.CharacterSet = "UTF-8"; 
        options.Width = width;
        options.Height = width;
        //Sets the margin of the QR code edge
        options.Margin = 1;

        BarcodeWriter writeCode = new BarcodeWriter { Format =  BarcodeFormat.QR_CODE, Options = options };

        return writeCode.Write(str);
    } 
} 

7: Call to generate QR Code:

        //Generate and display QR code RawImage showRawImage 
       //GameUtils.NET_PATH = "http://192.168.1.126:88/jingcha.jpg "Access address for generating pictures, IIS configured website"
        var t = GetQRCode.CreatQRCode(GameUtils.NET_PATH, 256, 256);
        showRawImage.texture = t;
        showRawImage.SetNativeSize();


8: Mobile phone access in LAN: the result of wechat scanning code;

Topics: C# Unity