This is the 8th day of my participation in the August More Text Challenge. For details, see:August is more challenging

An overview of the

The last two articles focused on two different ways to create qr codes, but it’s not enough to just create them. You also need to be able to read and parse qr codes. There are many ways to identify the TWO-DIMENSIONAL code, some directly call the camera scan code, and some directly identify the picture. The second method is to omit the scanning step. Let’s take a look at that. How to retrieve cameras and recognize QR codes in Unity.

The preparatory work

In this case, we use the BarcodeReader Decode in Zxing.dll. So no matter which method is used to create a TWO-DIMENSIONAL code, the final is the need to use zxing.dll (→_→). Qr Code We use the qr code created in the previous article.

Thought analysis

To identify the TWO-DIMENSIONAL code, first of all, you need to get the two-dimensional code picture. There are two ways:

  1. Use the camera to scan
  2. Direct long press picture recognition

When we get the image, we get the pixel color block in Color32 format by GetPixels32(). Then get the content in the QR code through Decode

Function implementation

Unity retrieves the camera and displays it on the screen

To use the camera in Unity, you need to use the WebCamTexture to get the camera data, and then assign the data to the UGUI RawImage to display the camera’s content on the screen. First define two parameters: WebCamTexture and RawImage\

  • private WebCamTexture mWebCamTexture;
  • public RawImage rawImage;

Next write the create camera method. Note: When creating a camera, pass the name of the camera into the method. Sometimes computers have multiple cameras and phones have front and rear cameras, so we need to know which camera to use

<param name="deviceName"></param> private void CreateWebcamTex(string) deviceName) { mWebCamTexture = new WebCamTexture(deviceName, 1280, 720); rawImage.texture = mWebCamTexture; Mwebcamtexture.play (); }Copy the code

The next step is to initialize the camera method, which can be started as soon as the program is running or written to a button click event

WebCamDevice[] Devices = WebCamTexture.devices; foreach (var item in devices) { if (! item.isFrontFacing) { CreateWebcamTex(item.name); break; }}Copy the code

So the camera is successfully tuned up, you can happily scan the QR code.

Qr code recognition

Decode(T rawRGB, int width, int height); (Since the computer does not have a camera, we will directly recognize the created QR code instead of the one captured by the camera. In fact, they are the same, but the sources of the images are different, one is captured, the other is created by ourselves.)

Let’s start with reading qr code data

Private BarcodeReader mReader = new BarcodeReader(); private BarcodeReader mReader = new BarcodeReader(); Public string Decode(Color32[] colors, int width, int height) {var result = mreader.decode (colors, width, height); if (result ! = null) { return result.Text; } return null; }Copy the code

We can call this method directly and pass in Color32[] and the width and height of the image to get Color32[]

Color32[] m_colorData = (rawimage.texture as Texture2D).getPixels32 ();Copy the code

Since the RawImage attribute is Texture, and the GetPixels32 method is Texture2D, you need to convert the image and then call the QR code read method directly

string result = Decode(m_colorData, rawImage.texture.width, rawImage.texture.height); if (! String.IsNullOrEmpty(result)) {// Perform corresponding operations after successful identification debug.log (" identification result: "+ result); }Copy the code

The camera recognition method is the same as above, that is, there is an extra step for camera recognition. Since it is camera scanning, we need to scan all the time, and scan once every one or two seconds. After successful scanning, the camera can be turned off to scan.

/// <returns></returns> Private IEnumerator Scan() {yield return new WaitForSeconds (0.2 F); yield return new WaitForEndOfFrame(); if (mWebCamTexture ! = null && mWebCamTexture. Width > 100) {/ / call recognition method string result = Decode (mWebCamTexture. GetPixels32 (), mWebCamTexture.width, mWebCamTexture.height); if (! string.IsNullOrEmpty(result)) { mWebCamTexture.Stop(); Debug.log (" result: "+ result); } else { StartCoroutine(Scan()); }}}Copy the code

Then let’s take a look at the scanning effect. The example is to create a QR code and then read the content of the QR code

Write in the last

In this article, the method of retrieving the camera has not been verified when writing the article, because the computer is not connected to the camera, so you should pay attention to see if there is a problem when using the method of retrieving the camera, but normally it is impossible to make mistakes (#^ ^ ^#). 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.