7using System.Collections.Generic;
8using System.Text.RegularExpressions;
12using ZXing.QrCode.Internal;
28 private Texture2D m_EncodedTex;
38 Texture2D tempLogoTex;
44 targetWidth = Mathf.Clamp(targetWidth, 128, 1024);
59 var writer =
new MultiFormatWriter();
60 var hints =
new Dictionary<EncodeHintType, object>();
62 hints.Add(EncodeHintType.CHARACTER_SET,
"UTF-8");
63 hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
71 if ((valueStr.Length == 12 || valueStr.Length == 13) && bAllDigit(valueStr))
73 if (valueStr.Length == 13) valueStr = valueStr.Substring(0, 12);
83 if ((valueStr.Length == 7 || valueStr.Length == 8) && bAllDigit(valueStr))
85 if (valueStr.Length == 8) valueStr = valueStr.Substring(0, 7);
95 if (IsNumAndEnCh(valueStr) && valueStr.Length <= 80)
102 if (bAllDigit(valueStr))
114 if (m_EncodedTex !=
null)
116 Destroy(m_EncodedTex);
120 m_EncodedTex =
new Texture2D(byteMatrix.Width, byteMatrix.Height);
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);
127 m_EncodedTex.SetPixel(i, j,
Color.white);
130 var pixels = m_EncodedTex.GetPixels32();
132 m_EncodedTex.SetPixels32(pixels);
134 m_EncodedTex.Apply();
149 static Color32[] RotateMatrixByClockwise(Color32[] matrix,
int n)
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];
164 static Color32[] RotateMatrixByAnticlockwise(Color32[] matrix,
int n)
166 var ret =
new Color32[n * n];
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];
175 bool isContainDigit(
string str)
177 for (var i = 0; i != str.Length; i++)
178 if (str[i] >=
'0' && str[i] <=
'9')
183 bool IsNumAndEnCh(
string input)
185 var pattern =
@"^[A-Za-z0-9-_!@# |+/*]+$";
186 var regex =
new Regex(pattern);
187 return regex.IsMatch(input);
191 bool isContainChar(
string str)
193 for (var i = 0; i != str.Length; i++)
194 if (str[i] >=
'a' && str[i] <=
'z')
199 bool bAllDigit(
string str)
201 for (var i = 0; i != str.Length; i++)
202 if (str[i] >=
'0' && str[i] <=
'9')
220 if (tempLogoTex ==
null)
228 var newLogoWidth = (int) (
e_QRLogoTex.width * scaleRatio);
229 var newLogoHeight = (int) (
e_QRLogoTex.height * scaleRatio);
234 if (tempLogoTex ==
null)
247 var startX = (m_EncodedTex.width - tempLogoTex.width) / 2;
248 var startY = (m_EncodedTex.height - tempLogoTex.height) / 2;
250 for (var x = startX; x < tempLogoTex.width + startX; x++)
251 for (var y = startY; y < tempLogoTex.height + startY; y++)
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);
259 Destroy(tempLogoTex);
262 m_EncodedTex.Apply();
delegate void QREncodeFinished(Texture2D tex)
int Encode(string valueStr)
Encode the specified string .
QREncodeFinished onQREncodeFinished
static void Bilinear(Texture2D tex, int newWidth, int newHeight)