CreatQR.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using ZXing;
  6. using ZXing.Common;
  7. using ZXing.QrCode;
  8. public class CreatQR : MonoBehaviour
  9. {
  10. //需要生产二维码的字符串数组
  11. string[] QrCodeStr = { "https://www.baidu.com/", "https://www.cnblogs.com/Mr-Miracle/", "https://unity3d.com/cn", "https://www.sogou.com/" };
  12. //在屏幕上显示二维码
  13. public RawImage image;
  14. //存放二维码
  15. Texture2D encoded;
  16. int Nmuber = 0;
  17. // Use this for initialization
  18. void Start()
  19. {
  20. encoded = new Texture2D(256, 256);
  21. // GenerateQRImage3("https://www.baidu.com/", 512, 512, centericon);
  22. //onlyQRCode("https://www.weimaqi.net/index_.aspx?device_name=WXR02153",512,512);
  23. }
  24. // [SerializeField]
  25. // Texture2D centericon;
  26. // Update is called once per frame
  27. // void Update()
  28. // {
  29. // if (Input.GetKeyDown(KeyCode.Space))
  30. // {
  31. // GenerateQRImage3("https://www.baidu.com/", 512, 512, centericon);
  32. // //image.texture = GenerateQRImageWithIcon("ytwjj", 512, 512, centericon);
  33. // // Btn_CreatQr();
  34. // //Nmuber++;
  35. // //if (Nmuber >= QrCodeStr.Length)
  36. // //{
  37. // // Nmuber = 0;
  38. // //}
  39. // }
  40. // }
  41. /// <summary>
  42. /// 定义方法生成二维码
  43. /// </summary>
  44. /// <param name="textForEncoding">需要生产二维码的字符串</param>
  45. /// <param name="width">宽</param>
  46. /// <param name="height">高</param>
  47. /// <returns></returns>
  48. private static Color32[] Encode(string textForEncoding, int width, int height)
  49. {
  50. var writer = new BarcodeWriter
  51. {
  52. Format = BarcodeFormat.QR_CODE,
  53. Options = new QrCodeEncodingOptions
  54. {
  55. Height = height,
  56. Width = width,
  57. Margin = 1
  58. }
  59. };
  60. return writer.Write(textForEncoding);
  61. }
  62. /// <summary>
  63. /// 生成二维码
  64. /// </summary>
  65. public void Btn_CreatQr()
  66. {
  67. if (QrCodeStr[Nmuber].Length > 1)
  68. {
  69. //二维码写入图片
  70. var color32 = Encode(QrCodeStr[Nmuber], encoded.width, encoded.height);
  71. encoded.SetPixels32(color32);
  72. encoded.Apply();
  73. //生成的二维码图片附给RawImage
  74. image.texture = encoded;
  75. }
  76. else
  77. {
  78. GameObject.Find("Text_1").GetComponent<Text>().text = "没有生成信息";
  79. }
  80. }
  81. /// <summary>
  82. /// 生成中心带有小图标二维码
  83. /// </summary>
  84. /// <param name="content"></param>
  85. /// <param name="width"></param>
  86. /// <param name="height"></param>
  87. /// <param name="centerIcon"></param>
  88. /// <returns></returns>
  89. public static Texture2D GenerateQRImageWithIcon(string content, int width, int height, Texture2D centerIcon)
  90. {
  91. // 编码成color32
  92. MultiFormatWriter writer = new MultiFormatWriter();
  93. Dictionary<EncodeHintType, object> hints = new Dictionary<EncodeHintType, object>();
  94. hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
  95. hints.Add(EncodeHintType.MARGIN, 1);
  96. hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H);
  97. BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
  98. // 转成texture2d
  99. int w = bitMatrix.Width;
  100. int h = bitMatrix.Height;
  101. Texture2D texture = new Texture2D(w, h);
  102. for (int x = 0; x < h; x++)
  103. {
  104. for (int y = 0; y < w; y++)
  105. {
  106. if (bitMatrix[x, y])
  107. {
  108. texture.SetPixel(y, x, Color.black);
  109. }
  110. else
  111. {
  112. texture.SetPixel(y, x, Color.white);
  113. }
  114. }
  115. }
  116. // 添加小图
  117. int halfWidth = texture.width / 2;
  118. int halfHeight = texture.height / 2;
  119. int halfWidthOfIcon = centerIcon.width / 4;
  120. int halfHeightOfIcon = centerIcon.height / 4;
  121. int centerOffsetX = 0;
  122. int centerOffsetY = 0;
  123. for (int x = 0; x < h; x++)
  124. {
  125. for (int y = 0; y < w; y++)
  126. {
  127. centerOffsetX = x - halfWidth;
  128. centerOffsetY = y - halfHeight;
  129. if (Mathf.Abs(centerOffsetX) <= halfWidthOfIcon && Mathf.Abs(centerOffsetY) <= halfHeightOfIcon)
  130. {
  131. texture.SetPixel(x, y, centerIcon.GetPixel(centerOffsetX + halfWidthOfIcon, centerOffsetY + halfHeightOfIcon));
  132. }
  133. }
  134. }
  135. texture.Apply();
  136. return texture;
  137. }
  138. /// <summary>
  139. /// 生成2维码 方法三
  140. /// 在方法二的基础上,添加小图标
  141. /// </summary>
  142. /// <param name="content"></param>
  143. /// <param name="width"></param>
  144. /// <param name="height"></param>
  145. public void GenerateQRImage3(string content, int width, int height, Texture2D centerIcon)
  146. {
  147. // 编码成color32
  148. MultiFormatWriter writer = new MultiFormatWriter();
  149. Dictionary<EncodeHintType, object> hints = new Dictionary<EncodeHintType, object>();
  150. hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
  151. hints.Add(EncodeHintType.MARGIN, 1);
  152. hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H);
  153. BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
  154. // 转成texture2d
  155. int w = bitMatrix.Width;
  156. int h = bitMatrix.Height;
  157. Texture2D texture = new Texture2D(w, h);
  158. for (int x = 0; x < h; x++)
  159. {
  160. for (int y = 0; y < w; y++)
  161. {
  162. if (bitMatrix[x, y])
  163. {
  164. texture.SetPixel(y, x, Color.blue);
  165. }
  166. else
  167. {
  168. texture.SetPixel(y, x, Color.white);
  169. }
  170. }
  171. }
  172. // 添加小图
  173. int halfWidth = texture.width / 2;
  174. int halfHeight = texture.height / 2;
  175. int halfWidthOfIcon = centerIcon.width / 2;
  176. int halfHeightOfIcon = centerIcon.height / 2;
  177. int centerOffsetX = 0;
  178. int centerOffsetY = 0;
  179. for (int x = 0; x < h; x++)
  180. {
  181. for (int y = 0; y < w; y++)
  182. {
  183. centerOffsetX = x - halfWidth;
  184. centerOffsetY = y - halfHeight;
  185. if (Mathf.Abs(centerOffsetX) <= halfWidthOfIcon && Mathf.Abs(centerOffsetY) <= halfHeightOfIcon)
  186. {
  187. texture.SetPixel(x, y, centerIcon.GetPixel(centerOffsetX + halfWidthOfIcon, centerOffsetY + halfHeightOfIcon));
  188. }
  189. }
  190. }
  191. texture.Apply();
  192. image.texture = texture;
  193. // 存储成文件
  194. //byte[] bytes = texture.EncodeToPNG();
  195. //string path = System.IO.Path.Combine(Application.dataPath, "qr.png");
  196. //System.IO.File.WriteAllBytes(path, bytes);
  197. }
  198. public void onlyQRCode(string content, int width, int height) {
  199. // 编码成color32
  200. MultiFormatWriter writer = new MultiFormatWriter();
  201. Dictionary<EncodeHintType, object> hints = new Dictionary<EncodeHintType, object>();
  202. hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
  203. hints.Add(EncodeHintType.MARGIN, 1);
  204. hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H);
  205. BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
  206. // 转成texture2d
  207. int w = bitMatrix.Width;
  208. int h = bitMatrix.Height;
  209. Texture2D texture = new Texture2D(w, h);
  210. for (int x = 0; x < h; x++)
  211. {
  212. for (int y = 0; y < w; y++)
  213. {
  214. if (bitMatrix[x, y])
  215. {
  216. texture.SetPixel(y, x, Color.blue);
  217. }
  218. else
  219. {
  220. texture.SetPixel(y, x, Color.white);
  221. }
  222. }
  223. }
  224. texture.Apply();
  225. image.texture = texture;
  226. }
  227. }