1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 using ZXing;
5 using UnityEngine.UI;
6 /// <summary>
7 /// 二维码扫描识别功能
8 /// </summary>
9 public class TestQRCodeScanning : MonoBehaviour {
10
11 [Header("摄像机检测界面")]
12 public RawImage cameraTexture;//摄像机映射显示区域
13
14 private WebCamTexture webCamTexture;//摄像机映射纹理
15 public Text text;//用来显示扫描信息
16 //二维码识别类
17 BarcodeReader barcodeReader;//库文件的对象(二维码信息保存的地方)
18
19 /// <summary>
20 /// 开启摄像机和准备工作
21 /// </summary>
22 void DeviceInit()
23 {
24
25
26 //1、获取所有摄像机硬件
27 WebCamDevice[] devices = WebCamTexture.devices;
28 //2、获取第一个摄像机硬件的名称
29 string deviceName = devices[0].name;//手机后置摄像机
30 //3、创建实例化一个摄像机显示区域
31 webCamTexture = new WebCamTexture(deviceName, 400, 300);
32 //4、显示的图片信息
33 cameraTexture.texture = webCamTexture;
34 //5、打开摄像机运行识别
35 webCamTexture.Play();
36
37 //6、实例化识别二维码信息存储对象
38 barcodeReader = new BarcodeReader();
39 }
40
41 Color32[] data;//二维码图片信息以像素点颜色信息数组存放
42
43 /// <summary>
44 /// 识别摄像机图片中的二维码信息
45 /// 打印二维码识别到的信息
46 /// </summary>
47 void ScanQRCode()
48 {
49 //7、获取摄像机画面的像素颜色数组信息
50 data = webCamTexture.GetPixels32();
51 //8、获取图片中的二维码信息
52 Result result = barcodeReader.Decode(data,webCamTexture.width,webCamTexture.height);
53 //如果获取到二维码信息了,打印出来
54 if (result!=null)
55 {
56 Debug.Log(result.Text);//===》==》===》 这是从二维码识别出来的信息
57 text.text = result.Text;//显示扫描信息
58
59 //扫描成功之后的处理
60 IsScanning = false;
61 webCamTexture.Stop();
62 }
63 }
64
65
66 /// <summary>
67 /// Start 初始化函数
68 /// </summary>
69 private void Start()
70 {
71 scanningButton.onClick.AddListener(ScanningButtonClick);
72 }
73
74
75 bool IsScanning = false;
76 float interval = 3;//扫描识别时间间隔
77 [SerializeField] Button scanningButton;
78 void ScanningButtonClick()
79 {
80 DeviceInit();
81 IsScanning = true;
82 }
83
84 private void Update()
85 {
86 if (IsScanning)
87 {
88 //每隔一段时间进行一次识别二维码信息
89 interval += Time.deltaTime;
90 if (interval>=3)
91 {
92 interval = 0;
93 ScanQRCode();//开始扫描
94 }
95 }
96 }
97 }
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 using UnityEngine.UI;
5 using ZXing;
6 //二维码的生成
7 public class TestQRCodeDraw : MonoBehaviour {
8
9 [Header("绘制好的二维码显示界面")]
10 public RawImage QRCode;
11 //二维码绘制类
12 BarcodeWriter barcodeWriter;
13 [SerializeField] Button drawbutton;
14 /// <summary>
15 /// 将制定字符串信息转换成二维码图片信息
16 /// </summary>
17 /// <param name="formatStr">要生产二维码的字符串信息</param>
18 /// <param name="width">二维码的宽度</param>
19 /// <param name="height">二维码的高度</param>
20 /// <returns>返回二维码图片的颜色数组信息</returns>
21 Color32[] GeneQRCode(string formatStr,int width,int height)
22 {
23 //绘制二维码前进行一些设置
24 ZXing.QrCode.QrCodeEncodingOptions options =
25 new ZXing.QrCode.QrCodeEncodingOptions();
26 //设置字符串转换格式,确保字符串信息保持正确
27 options.CharacterSet = "UTF-8";
28 //设置绘制区域的宽度和高度的像素值
29 options.Width = width;
30 options.Height = height;
31 //设置二维码边缘留白宽度(值越大留白宽度大,二维码就减小)
32 options.Margin = 1;
33
34 //实例化字符串绘制二维码工具
35 barcodeWriter = new BarcodeWriter {Format=BarcodeFormat.QR_CODE,Options=options };
36 //进行二维码绘制并进行返回图片的颜色数组信息
37 return barcodeWriter.Write(formatStr);
38
39 }
40
41 /// <summary>
42 /// 根据二维码图片信息绘制指定字符串信息的二维码到指定区域
43 /// </summary>
44 /// <param name="str">要生产二维码的字符串信息</param>
45 /// <param name="width">二维码的宽度</param>
46 /// <param name="height">二维码的高度</param>
47 /// <returns>返回绘制好的图片</returns>
48 Texture2D ShowQRCode(string str,int width,int height)
49 {
50 //实例化一个图片类
51 Texture2D t = new Texture2D(width, height);
52 //获取二维码图片颜色数组信息
53 Color32[] col32 = GeneQRCode(str, width, height);
54 //为图片设置绘制像素颜色信息
55 t.SetPixels32(col32);
56 //设置信息更新应用下
57 t.Apply();
58 //将整理好的图片信息显示到指定区域中
59 return t;
60 }
61
62 /// <summary>
63 /// 开始绘制指定信息的二维码
64 /// </summary>
65 /// <param name="formatStr"></param>
66 void DrawQRCode(string formatStr)
67 {
68 //注意:这个宽高度大小256不要变。不然生成的信息不正确
69 //256有可能是这个ZXingNet插件指定大小的绘制像素点数值
70 Texture2D t = ShowQRCode(formatStr, 256, 256);
71
72 //显示到UI界面的图片上
73 QRCode.texture = t;
74 }
75
76
77 public string QRCodeText = "二维码";
78 void DrawButtonClick()
79 {
80 DrawQRCode(QRCodeText);
81 }
82
83 private void Start()
84 {
85 drawbutton.onClick.AddListener(DrawButtonClick);
86 }
87 }