Tanoda
QRCodeEncodeController.cs
Go to the documentation of this file.
1
5
6
7using System.Collections.Generic;
8using System.Text.RegularExpressions;
9using UnityEngine;
10using ZXing;
11using ZXing.Common;
12using ZXing.QrCode.Internal;
13
14public class QRCodeEncodeController : MonoBehaviour
15{
16 public enum CodeMode
17 {
18 QR_CODE,
19 CODE_39,
20 CODE_128,
21 EAN_8,
22 EAN_13,
23
24 //DATA_MATRIX,
25 NONE
26 }
27
28 private Texture2D m_EncodedTex;
29 public int e_QRCodeWidth = 512;
30 public int e_QRCodeHeight = 512;
31
32 public delegate void QREncodeFinished(Texture2D tex);
33
35 BitMatrix byteMatrix;
36 public CodeMode eCodeFormat = CodeMode.QR_CODE;
37 public Texture2D e_QRLogoTex;
38 Texture2D tempLogoTex;
39 public float e_EmbedLogoRatio = 0.2f;
40
41 void Start()
42 {
43 var targetWidth = Mathf.Min(e_QRCodeWidth, e_QRCodeHeight);
44 targetWidth = Mathf.Clamp(targetWidth, 128, 1024);
45 e_QRCodeWidth = e_QRCodeHeight = targetWidth;
46 }
47
48 void Update()
49 {
50 }
51
56 public int Encode(string valueStr)
57 {
58 // var writer = new QRCodeWriter();
59 var writer = new MultiFormatWriter();
60 var hints = new Dictionary<EncodeHintType, object>();
61 //set the code type
62 hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
63 hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
64
65 switch (eCodeFormat)
66 {
67 case CodeMode.QR_CODE:
68 byteMatrix = writer.encode(valueStr, BarcodeFormat.QR_CODE, e_QRCodeWidth, e_QRCodeHeight, hints);
69 break;
70 case CodeMode.EAN_13:
71 if ((valueStr.Length == 12 || valueStr.Length == 13) && bAllDigit(valueStr))
72 {
73 if (valueStr.Length == 13) valueStr = valueStr.Substring(0, 12);
74 byteMatrix = writer.encode(valueStr, BarcodeFormat.EAN_13, e_QRCodeWidth, e_QRCodeWidth / 2, hints);
75 }
76 else
77 {
78 return -13;
79 }
80
81 break;
82 case CodeMode.EAN_8:
83 if ((valueStr.Length == 7 || valueStr.Length == 8) && bAllDigit(valueStr))
84 {
85 if (valueStr.Length == 8) valueStr = valueStr.Substring(0, 7);
86 byteMatrix = writer.encode(valueStr, BarcodeFormat.EAN_8, e_QRCodeWidth, e_QRCodeWidth / 2, hints);
87 }
88 else
89 {
90 return -8;
91 }
92
93 break;
94 case CodeMode.CODE_128:
95 if (IsNumAndEnCh(valueStr) && valueStr.Length <= 80)
96 byteMatrix = writer.encode(valueStr, BarcodeFormat.CODE_128, e_QRCodeWidth, e_QRCodeWidth / 2,
97 hints);
98 else
99 return -128;
100 break;
101 case CodeMode.CODE_39:
102 if (bAllDigit(valueStr))
103 byteMatrix = writer.encode(valueStr, BarcodeFormat.CODE_39, e_QRCodeWidth, e_QRCodeHeight / 2,
104 hints);
105 else
106 return -39;
107
108 break;
109
110 case CodeMode.NONE:
111 return -1;
112 }
113
114 if (m_EncodedTex != null)
115 {
116 Destroy(m_EncodedTex);
117 m_EncodedTex = null;
118 }
119
120 m_EncodedTex = new Texture2D(byteMatrix.Width, byteMatrix.Height);
121
122 for (var i = 0; i != m_EncodedTex.width; i++)
123 for (var j = 0; j != m_EncodedTex.height; j++)
124 if (byteMatrix[i, j])
125 m_EncodedTex.SetPixel(i, j, Color.black);
126 else
127 m_EncodedTex.SetPixel(i, j, Color.white);
128
130 var pixels = m_EncodedTex.GetPixels32();
131 //pixels = RotateMatrixByClockwise(pixels, m_EncodedTex.width);
132 m_EncodedTex.SetPixels32(pixels);
133
134 m_EncodedTex.Apply();
135
136
137 if (eCodeFormat == CodeMode.QR_CODE) AddLogoToQRCode();
138
139 onQREncodeFinished(m_EncodedTex);
140 return 0;
141 }
142
149 static Color32[] RotateMatrixByClockwise(Color32[] matrix, int n)
150 {
151 var ret = new Color32[n * n];
152 for (var i = 0; i < n; ++i)
153 for (var j = 0; j < n; ++j)
154 ret[i * n + j] = matrix[(n - i - 1) * n + j];
155 return ret;
156 }
157
164 static Color32[] RotateMatrixByAnticlockwise(Color32[] matrix, int n)
165 {
166 var ret = new Color32[n * n];
167
168 for (var i = 0; i < n; ++i)
169 for (var j = 0; j < n; ++j)
170 ret[i * n + j] = matrix[(n - j - 1) * n + i];
171 return ret;
172 }
173
174
175 bool isContainDigit(string str)
176 {
177 for (var i = 0; i != str.Length; i++)
178 if (str[i] >= '0' && str[i] <= '9')
179 return true;
180 return false;
181 }
182
183 bool IsNumAndEnCh(string input)
184 {
185 var pattern = @"^[A-Za-z0-9-_!@# |+/*]+$";
186 var regex = new Regex(pattern);
187 return regex.IsMatch(input);
188 }
189
190
191 bool isContainChar(string str)
192 {
193 for (var i = 0; i != str.Length; i++)
194 if (str[i] >= 'a' && str[i] <= 'z')
195 return true;
196 return false;
197 }
198
199 bool bAllDigit(string str)
200 {
201 for (var i = 0; i != str.Length; i++)
202 if (str[i] >= '0' && str[i] <= '9')
203 {
204 }
205 else
206 {
207 return false;
208 }
209
210 return true;
211 }
212
213 public void AddLogoToQRCode()
214 {
215 if (e_QRLogoTex != null)
216 {
217 var maxLength = Mathf.Max(e_QRLogoTex.width, e_QRLogoTex.height);
218 if (maxLength > m_EncodedTex.width * e_EmbedLogoRatio)
219 {
220 if (tempLogoTex == null)
221 {
222 tempLogoTex = new Texture2D(e_QRLogoTex.width, e_QRLogoTex.height, TextureFormat.RGBA32, true);
223 tempLogoTex.SetPixels(e_QRLogoTex.GetPixels());
224 tempLogoTex.Apply();
225 }
226
227 var scaleRatio = m_EncodedTex.width * e_EmbedLogoRatio / maxLength * 1.0f;
228 var newLogoWidth = (int) (e_QRLogoTex.width * scaleRatio);
229 var newLogoHeight = (int) (e_QRLogoTex.height * scaleRatio);
230 TextureScale.Bilinear(tempLogoTex, newLogoWidth, newLogoHeight);
231 }
232 else
233 {
234 if (tempLogoTex == null)
235 {
236 tempLogoTex = new Texture2D(e_QRLogoTex.width, e_QRLogoTex.height, TextureFormat.RGBA32, true);
237 tempLogoTex.SetPixels(e_QRLogoTex.GetPixels());
238 tempLogoTex.Apply();
239 }
240 }
241 }
242 else
243 {
244 return;
245 }
246
247 var startX = (m_EncodedTex.width - tempLogoTex.width) / 2;
248 var startY = (m_EncodedTex.height - tempLogoTex.height) / 2;
249
250 for (var x = startX; x < tempLogoTex.width + startX; x++)
251 for (var y = startY; y < tempLogoTex.height + startY; y++)
252 {
253 var bgColor = m_EncodedTex.GetPixel(x, y);
254 var wmColor = tempLogoTex.GetPixel(x - startX, y - startY);
255 var finalColor = Color.Lerp(bgColor, wmColor, wmColor.a / 1.0f);
256 m_EncodedTex.SetPixel(x, y, finalColor);
257 }
258
259 Destroy(tempLogoTex);
260 tempLogoTex = null;
261
262 m_EncodedTex.Apply();
263 }
264}
UnityEngine.Color Color
Definition: TestScript.cs:32
delegate void QREncodeFinished(Texture2D tex)
int Encode(string valueStr)
Encode the specified string .
QREncodeFinished onQREncodeFinished
static void Bilinear(Texture2D tex, int newWidth, int newHeight)
Definition: TextureScale.cs:32