This is the seventh day of my participation in the August More text Challenge. For details, see:August is more challenging

An overview of the

The last article talked about creating qr codes using ZXing, so this article will focus on how to create qr codes without using DLLS. But the downside of this approach is that it can’t be used offline.

solution

The general idea of this method is to generate a website by visiting the TWO-DIMENSIONAL code, call the generation method of the website to create a two-dimensional code, and then use WWW or UnityWebRequest to download. By looking online, you can find a lot of TWO-DIMENSIONAL code generation website, and in this site, I only found a website API is open, can directly change the content of the WEBSITE for the generation of two-dimensional code. This website is a qr code

Forage TWO-DIMENSIONAL code API

API,

First of all, let’s take a look at the official API of the qr code, the website is here, save you find your own, the specific path is, the home page of more tools, and then slide to the bottom, there is an other – TWO-DIMENSIONAL code beautification API

Let’s take a look at the contents of the API, into the interface is easy to see what a URL https://cli.im/api/qrcode/code? Text = QR code content &mhid= beautify template ID there are two Chinese places, write very clearly one is the QR code content, one is beautify template ID. As you can see, we can generate a QR code as long as we change these two places into our own content. There is also an official instance of the API interface. I tried to content to https://cli.im/api/qrcode/code? Text = https://juejin.cn/user/4143372312773549&mhid=sELPDFnok80gPHovKdI display interface is as follows

If we press F12, we can see the structure of the page. The qr code picture address can also be seen, we copy the picture address, found that is a picture. This allows you to download the image directly from Unity.

Click on the image address to view

Obtain the QR code template ID

First of all, we need to log in, and then we click the adjust beautify template button

In the pop-up window, we can design the DESIRED TWO-DIMENSIONAL code style by ourselves, and click OK after the design is completed.The link on the left will change, as you can seemhid=What follows is the template ID, just copy it and replace it when you use it

Implemented in Unity

First, we visit https://cli.im/api/qrcode/code? in Unity Text = https://juejin.cn/user/4143372312773549&mhid=sELPDFnok80gPHovKdI if see pictures can directly return address. Im/API /qrcode/… The TWO-DIMENSIONAL code content can be changed to their own content, the back of the beautify template ID “sELPDFnok80gPHovKdI” needs to be in accordance with the above to obtain two-dimensional code template ID method after the replacement, you can also use the default black TWO-DIMENSIONAL code, use this word to fill in or empty can be old version

private IEnumerator LoadWWW() { WWW w = new WWW("https://cli.im/api/qrcode/code? text=https://juejin.cn/user/4143372312773549&mhid=sELPDFnok80gPHovKdI"); yield return w; Debug.Log(w.text); }Copy the code

A new version of

IEnumerator LoadQRCode() { UnityWebRequest uwr = UnityWebRequest.Get("https://cli.im/api/qrcode/code? text=https://juejin.cn/user/4143372312773549&mhid=sELPDFnok80gPHovKdI"); yield return uwr.SendWebRequest(); Debug.Log(uwr.downloadHandler.text); }Copy the code

We can find that the content of the entire web page is obtained, rather than a simple image address

From the above picture, we can see that it is very simple. There is an IMG tag in the body, and SRC represents the address of the picture. As long as we intercept the address, we can get our QR code. Intercept data:

string str = uwr.downloadHandler.text; String s = str.substring (str.indexof ("<img SRC =") + 12, str.Length - (str.IndexOf("<img src=") + 12)); String result = s.substring (0, s.indexof ("\"")); Debug.Log(result);Copy the code

We copied the connection and opened it in the browser. It was the QR code we had just created, so we just downloaded the QR code and displayed it in Unity

    IEnumerator LoadImage(string url)
    {
        WWW www = new WWW(url);
        yield return www;
        Texture2D tex2d = www.texture;
        Sprite m_sprite = Sprite.Create(tex2d, new Rect(0, 0, tex2d.width, tex2d.height), new Vector2(0, 0));
        image.sprite = m_sprite;
        image.SetNativeSize();
    }
Copy the code

A new version of

IEnumerator DownSprite(string url) { UnityWebRequest wr = new UnityWebRequest(url); DownloadHandlerTexture texD1 = new DownloadHandlerTexture(true); wr.downloadHandler = texD1; yield return wr.SendWebRequest(); int width = 400; int high = 400; if (! wr.isNetworkError) { Texture2D tex = new Texture2D(width, high); tex = texD1.texture; Sprite Sprite = sprite.create (Tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f)); image.sprite = sprite; image.SetNativeSize(); }}Copy the code

conclusion

The only advantage of this method is to be able to generate color TWO-DIMENSIONAL code, style, the disadvantage is that there is no way to use offline.

Write in the last

All the shared content are the author in the daily development process used a variety of small function points, share out also in a disguised review, if there is a bad place to write also please give advice. The source code of the Demo will be sorted out and shared later. Welcome to learn from each other and make progress. The next article will show you how to scan and identify qr codes in Unity.