SoFunction
Updated on 2025-03-07

Generation and scanning of unityZXing QR code

This article shares the specific codes for unityZXing QR code generation and scanning for your reference. The specific content is as follows

I don't remember it from a boss

using ;
using ;
using UnityEngine;
using ZXing;
using ;
/// <summary>
/// QR code scanning and recognition function/// </summary>
public class TestQRCodeScanning : MonoBehaviour {
 
 [Header("Camera Detection Interface")]
 public RawImage cameraTexture;//Camera map display area 
 private WebCamTexture webCamTexture;//Camera mapping texture public Text text;// Used to display scan information //QR code identification BarcodeReader barcodeReader;//The object of the library file (where QR code information is saved) 
 /// <summary>
 /// Turn on the camera and prepare for it /// </summary>
 void DeviceInit()
 {
  
 
  //1. Obtain all camera hardware  WebCamDevice[] devices = ;
  //2. Get the name of the first camera hardware  string deviceName = devices[0].name;//The rear camera of the mobile phone  //3. Create and instantiate a camera display area  webCamTexture = new WebCamTexture(deviceName, 400, 300);
  //4. Displayed picture information   = webCamTexture;
  //5. Turn on the camera to run and identify  ();
 
  //6. Instant identification of QR code information storage object  barcodeReader = new BarcodeReader();
 }
 
 Color32[] data;//The QR code image information is stored in a pixel color information array 
 /// <summary>
 /// Identify the QR code information in the camera picture /// Print the QR code to identify the information /// </summary>
 void ScanQRCode()
 {
  //7. Obtain the pixel color array information of the camera screen  data = webCamTexture.GetPixels32();
  //8. Get the QR code information in the picture  Result result = (data,,);
  //If you have obtained the QR code information, print it out  if (result!=null)
  {
   ();//===》==》===》This is the information identified from the QR code    = ;//Show scan information 
   //Processing after successful scanning   IsScanning = false;
   ();
  }
 }
 
 
 /// <summary>
 /// Start initialization function /// </summary>
 private void Start()
 {
  (ScanningButtonClick);
 }
 
 
 bool IsScanning = false;
 float interval = 3;//Scan recognition time interval [SerializeField] Button scanningButton;
 void ScanningButtonClick()
 {
  DeviceInit();
  IsScanning = true;
 }
 
 private void Update()
 {
  if (IsScanning)
  {
   //Identify the QR code information every once in a while   interval += ;
   if (interval>=3)
   {
    interval = 0;
    ScanQRCode();//Start scanning   }
  }
 }
}

ZXing:ZingNet

After downloading, copy it to Unity's Plugins folder.

After downloading, find the location under UnityDemo/Assets

using ;
using ;
using UnityEngine;
using ;
using ZXing;
//Generation of QR codepublic class TestQRCodeDraw : MonoBehaviour {
 
 [Header("Drawn QR code display interface")]
 public RawImage QRCode;
 //QR code drawing class BarcodeWriter barcodeWriter;
 [SerializeField] Button drawbutton;
 /// <summary>
 /// Convert the formulated string information into QR code image information /// </summary>
 /// <param name="formatStr">String information to produce QR code</param> /// <param name="width">Width of QR code</param> /// <param name="height">High of QR code</param> /// <returns>Return the color array information of the QR code image</returns> Color32[] GeneQRCode(string formatStr,int width,int height)
 {
  // Make some settings before drawing the QR code   options =
   new ();
  //Set the string conversion format to ensure that the string information remains correct   = "UTF-8";
  //Set the pixel value of the width and height of the drawing area   = width;
   = height;
  //Set the width of the QR code edge blank (the larger the value, the larger the width of the blank, the smaller the QR code)   = 1;
 
  //Instantiated string drawing QR code tool  barcodeWriter = new BarcodeWriter {Format=BarcodeFormat.QR_CODE,Options=options };
  //Draw the QR code and return the color array information of the picture  return (formatStr); 
 
 }
 
 /// &lt;summary&gt;
 ///Draw the QR code of the specified string information into the specified area based on the QR code picture information /// &lt;/summary&gt;
 /// <param name="str">String information to produce QR code</param> /// <param name="width">Width of QR code</param> /// <param name="height">High of QR code</param> /// <returns>Return to the drawn picture</returns>  Texture2D ShowQRCode(string str,int width,int height)
 {
  //Instantiate an image class  Texture2D t = new Texture2D(width, height);
  //Get the QR code image color array information  Color32[] col32 = GeneQRCode(str, width, height);
  //Set drawing pixel color information for the picture  t.SetPixels32(col32);
  //Set information update application  ();
  //Show the sorted picture information to the specified area  return t;
 }
 
 /// &lt;summary&gt;
 /// Start drawing the QR code for the specified information /// &lt;/summary&gt;
 /// &lt;param name="formatStr"&gt;&lt;/param&gt;
 void DrawQRCode(string formatStr)
 {
  //Note: This width, height, size 256 should not change.  Otherwise, the generated information is incorrect  //256 It is possible that the ZXingNet plugin has a size of the drawn pixel value specified by the ZXingNet plugin  Texture2D t = ShowQRCode(formatStr, 256, 256);
 
  //Show to the picture on the UI interface   = t;
 }
 
 
 public string QRCodeText = "QR code";
 void DrawButtonClick()
 {
  DrawQRCode(QRCodeText);
 }
 
 private void Start()
 {
  (DrawButtonClick);
 }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.