当前位置:   article > 正文

unity打印生成之后的条形码(二维码也行)_unity显示条形码

unity显示条形码

QR CodeBarcode Scanner and Generator Cross PlatformPro 4.3.unitypackage  +  LCPrinter.unitypackage (Unity二维码条形码生成工具+unity打印机)

稍微修改了一下里面的脚本改为CodeController。 

  1. /// <summary>
  2. /// write by 52cwalk,if you have some question ,please contract lycwalk@gmail.com
  3. /// </summary>
  4. ///
  5. using UnityEngine;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using ZXing;
  9. using ZXing.QrCode;
  10. using ZXing.QrCode.Internal;
  11. using ZXing.Common;
  12. public class CodeController : MonoBehaviour {
  13. public enum CodeMode
  14. {
  15. QR_CODE,
  16. EAN_13,
  17. EAN_8,
  18. CODE_128,
  19. //DATA_MATRIX,
  20. NONE
  21. }
  22. private Texture2D m_EncodedTex;
  23. public int e_QRCodeWidth = 400;
  24. public int e_QRCodeHeight = 400;
  25. public delegate void QREncodeFinished(Texture2D tex);
  26. public event QREncodeFinished onQREncodeFinished;
  27. BitMatrix byteMatrix;
  28. public CodeMode eCodeFormat = CodeMode.QR_CODE;
  29. public Texture2D e_QRLogoTex;
  30. Texture2D tempLogoTex = null;
  31. public float e_EmbedLogoRatio = 0.2f;
  32. void Start ()
  33. {
  34. int targetWidth = Mathf.Min(e_QRCodeWidth,e_QRCodeHeight);
  35. targetWidth = Mathf.Clamp (targetWidth, 128, 1024);
  36. e_QRCodeWidth = e_QRCodeHeight = targetWidth;
  37. }
  38. void Update ()
  39. {
  40. }
  41. /// <summary>
  42. /// Encode the specified string .
  43. /// </summary>
  44. /// <param name="valueStr"> content string.</param>
  45. public int Encode(string valueStr)
  46. {
  47. // var writer = new QRCodeWriter();
  48. var writer = new MultiFormatWriter();
  49. Dictionary<EncodeHintType, object> hints = new Dictionary<EncodeHintType, object>();
  50. //set the code type
  51. hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
  52. hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
  53. switch (eCodeFormat) {
  54. case CodeMode.QR_CODE:
  55. byteMatrix = writer.encode( valueStr, BarcodeFormat.QR_CODE, e_QRCodeWidth, e_QRCodeHeight,hints);
  56. break;
  57. case CodeMode.EAN_13:
  58. if ((valueStr.Length == 12 || valueStr.Length == 13) && bAllDigit(valueStr)) {
  59. if (valueStr.Length == 13) {
  60. valueStr = valueStr.Substring (0, 12);
  61. }
  62. byteMatrix = writer.encode( valueStr, BarcodeFormat.EAN_13, e_QRCodeWidth, e_QRCodeWidth/2,hints);
  63. } else {
  64. return -13;
  65. }
  66. break;
  67. case CodeMode.EAN_8:
  68. if ((valueStr.Length == 7 || valueStr.Length == 8) && bAllDigit(valueStr)) {
  69. if (valueStr.Length == 8) {
  70. valueStr = valueStr.Substring (0, 7);
  71. }
  72. byteMatrix = writer.encode( valueStr, BarcodeFormat.EAN_8, e_QRCodeWidth, e_QRCodeWidth/2,hints);
  73. } else {
  74. return -8;
  75. }
  76. break;
  77. case CodeMode.CODE_128:
  78. if (valueStr.Length <= 80) {
  79. byteMatrix = writer.encode( valueStr, BarcodeFormat.CODE_128, e_QRCodeWidth, e_QRCodeWidth/2,hints);
  80. } else {
  81. return -128;
  82. }
  83. break;
  84. /*
  85. case CodeMode.DATA_MATRIX:
  86. byteMatrix = writer.encode( valueStr, BarcodeFormat.DATA_MATRIX, e_QRCodeWidth, e_QRCodeHeight,hints);
  87. break;
  88. */
  89. case CodeMode.NONE:
  90. return -1;
  91. break;
  92. }
  93. if (m_EncodedTex != null) {
  94. Destroy (m_EncodedTex);
  95. m_EncodedTex = null;
  96. }
  97. m_EncodedTex = new Texture2D(byteMatrix.Width, byteMatrix.Height);
  98. for (int i =0; i!= m_EncodedTex.width; i++) {
  99. for(int j = 0;j!= m_EncodedTex.height;j++)
  100. {
  101. if(byteMatrix[i,j])
  102. {
  103. m_EncodedTex.SetPixel(i,j,Color.black);
  104. }
  105. else
  106. {
  107. m_EncodedTex.SetPixel(i,j,Color.white);
  108. }
  109. }
  110. }
  111. ///rotation the image
  112. Color32[] pixels = m_EncodedTex.GetPixels32();
  113. //pixels = RotateMatrixByClockwise(pixels, m_EncodedTex.width);
  114. m_EncodedTex.SetPixels32(pixels);
  115. m_EncodedTex.Apply ();
  116. if (eCodeFormat == CodeMode.QR_CODE) {
  117. AddLogoToQRCode ();
  118. }
  119. onQREncodeFinished (m_EncodedTex);
  120. return 0;
  121. }
  122. /// <summary>
  123. /// Encode the specified string .
  124. /// </summary>
  125. /// <param name="valueStr"> content string.</param>
  126. public Texture2D EncodeT2D(string valueStr)
  127. {
  128. // var writer = new QRCodeWriter();
  129. var writer = new MultiFormatWriter();
  130. Dictionary<EncodeHintType, object> hints = new Dictionary<EncodeHintType, object>();
  131. //set the code type
  132. hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
  133. hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
  134. switch (eCodeFormat)
  135. {
  136. case CodeMode.QR_CODE:
  137. byteMatrix = writer.encode(valueStr, BarcodeFormat.QR_CODE, e_QRCodeWidth, e_QRCodeHeight, hints);
  138. break;
  139. case CodeMode.EAN_13:
  140. if ((valueStr.Length == 12 || valueStr.Length == 13) && bAllDigit(valueStr))
  141. {
  142. if (valueStr.Length == 13)
  143. {
  144. valueStr = valueStr.Substring(0, 12);
  145. }
  146. byteMatrix = writer.encode(valueStr, BarcodeFormat.EAN_13, e_QRCodeWidth, e_QRCodeWidth / 2, hints);
  147. }
  148. else
  149. {
  150. //return -13;
  151. }
  152. break;
  153. case CodeMode.EAN_8:
  154. if ((valueStr.Length == 7 || valueStr.Length == 8) && bAllDigit(valueStr))
  155. {
  156. if (valueStr.Length == 8)
  157. {
  158. valueStr = valueStr.Substring(0, 7);
  159. }
  160. byteMatrix = writer.encode(valueStr, BarcodeFormat.EAN_8, e_QRCodeWidth, e_QRCodeWidth / 2, hints);
  161. }
  162. else
  163. {
  164. //return -8;
  165. }
  166. break;
  167. case CodeMode.CODE_128:
  168. if (valueStr.Length <= 80)
  169. {
  170. byteMatrix = writer.encode(valueStr, BarcodeFormat.CODE_128, e_QRCodeWidth, e_QRCodeWidth / 2, hints);
  171. }
  172. else
  173. {
  174. //return -128;
  175. }
  176. break;
  177. /*
  178. case CodeMode.DATA_MATRIX:
  179. byteMatrix = writer.encode( valueStr, BarcodeFormat.DATA_MATRIX, e_QRCodeWidth, e_QRCodeHeight,hints);
  180. break;
  181. */
  182. case CodeMode.NONE:
  183. //return -1;
  184. break;
  185. }
  186. if (m_EncodedTex != null)
  187. {
  188. Destroy(m_EncodedTex);
  189. m_EncodedTex = null;
  190. }
  191. m_EncodedTex = new Texture2D(byteMatrix.Width, byteMatrix.Height);
  192. for (int i = 0; i != m_EncodedTex.width; i++)
  193. {
  194. for (int j = 0; j != m_EncodedTex.height; j++)
  195. {
  196. if (byteMatrix[i, j])
  197. {
  198. m_EncodedTex.SetPixel(i, j, Color.black);
  199. }
  200. else
  201. {
  202. m_EncodedTex.SetPixel(i, j, Color.white);
  203. }
  204. }
  205. }
  206. ///rotation the image
  207. Color32[] pixels = m_EncodedTex.GetPixels32();
  208. //pixels = RotateMatrixByClockwise(pixels, m_EncodedTex.width);
  209. m_EncodedTex.SetPixels32(pixels);
  210. m_EncodedTex.Apply();
  211. if (eCodeFormat == CodeMode.QR_CODE)
  212. {
  213. AddLogoToQRCode();
  214. }
  215. //onQREncodeFinished(m_EncodedTex);
  216. //return 0;
  217. return m_EncodedTex;
  218. }
  219. /// <summary>
  220. /// Rotates the matrix.Clockwise
  221. /// </summary>
  222. /// <returns>The matrix.</returns>
  223. /// <param name="matrix">Matrix.</param>
  224. /// <param name="n">N.</param>
  225. static Color32[] RotateMatrixByClockwise(Color32[] matrix, int n) {
  226. Color32[] ret = new Color32[n * n];
  227. for (int i = 0; i < n; ++i) {
  228. for (int j = 0; j < n; ++j) {
  229. ret[i*n + j] = matrix[(n - i - 1) * n + j];
  230. }
  231. }
  232. return ret;
  233. }
  234. /// <summary>
  235. /// anticlockwise
  236. /// </summary>
  237. /// <returns>The matrix.</returns>
  238. /// <param name="matrix">Matrix.</param>
  239. /// <param name="n">N.</param>
  240. static Color32[] RotateMatrixByAnticlockwise(Color32[] matrix, int n) {
  241. Color32[] ret = new Color32[n * n];
  242. for (int i = 0; i < n; ++i) {
  243. for (int j = 0; j < n; ++j) {
  244. ret[i*n + j] = matrix[(n - j - 1) * n + i];
  245. }
  246. }
  247. return ret;
  248. }
  249. bool isContainDigit(string str)
  250. {
  251. for (int i = 0; i != str.Length; i++) {
  252. if (str [i] >= '0' && str [i] <= '9') {
  253. return true;
  254. }
  255. }
  256. return false;
  257. }
  258. bool isContainChar(string str)
  259. {
  260. for (int i = 0; i != str.Length; i++) {
  261. if (str [i] >= 'a' && str [i] <= 'z') {
  262. return true;
  263. }
  264. }
  265. return false;
  266. }
  267. bool bAllDigit(string str)
  268. {
  269. for (int i = 0; i != str.Length; i++) {
  270. if (str [i] >= '0' && str [i] <= '9') {
  271. } else {
  272. return false;
  273. }
  274. }
  275. return true;
  276. }
  277. public void AddLogoToQRCode()
  278. {
  279. if (e_QRLogoTex != null) {
  280. int maxLength = Mathf.Max (e_QRLogoTex.width, e_QRLogoTex.height);
  281. if (maxLength > (m_EncodedTex.width * e_EmbedLogoRatio)) {
  282. if (tempLogoTex == null) {
  283. tempLogoTex = new Texture2D (e_QRLogoTex.width, e_QRLogoTex.height, TextureFormat.RGBA32, true);
  284. tempLogoTex.SetPixels (e_QRLogoTex.GetPixels ());
  285. tempLogoTex.Apply ();
  286. }
  287. float scaleRatio = m_EncodedTex.width * e_EmbedLogoRatio / maxLength * 1.0f;
  288. int newLogoWidth = (int)(e_QRLogoTex.width * scaleRatio);
  289. int newLogoHeight = (int)(e_QRLogoTex.height * scaleRatio);
  290. TextureScale.Bilinear (tempLogoTex, newLogoWidth, newLogoHeight);
  291. } else {
  292. if (tempLogoTex == null) {
  293. tempLogoTex = new Texture2D (e_QRLogoTex.width, e_QRLogoTex.height, TextureFormat.RGBA32,true);
  294. tempLogoTex.SetPixels (e_QRLogoTex.GetPixels());
  295. tempLogoTex.Apply ();
  296. }
  297. }
  298. }
  299. else
  300. {
  301. return;
  302. }
  303. int startX = (m_EncodedTex.width - tempLogoTex.width)/2;
  304. int startY = (m_EncodedTex.height - tempLogoTex.height)/2;
  305. for (int x = startX; x < tempLogoTex.width + startX; x++) {
  306. for (int y = startY; y < tempLogoTex.height + startY; y++) {
  307. Color bgColor = m_EncodedTex.GetPixel (x, y);
  308. Color wmColor = tempLogoTex.GetPixel (x - startX, y - startY);
  309. Color finalColor = Color.Lerp (bgColor, wmColor, wmColor.a / 1.0f);
  310. m_EncodedTex.SetPixel (x, y, finalColor);
  311. }
  312. }
  313. Destroy (tempLogoTex);
  314. tempLogoTex = null;
  315. m_EncodedTex.Apply ();
  316. }
  317. }

再次修改一下LCPrinterScript 

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Diagnostics;
  4. using System;
  5. using System.IO;
  6. using LCPrinter;
  7. using UnityEngine.UI;
  8. public class LCPrinterScript : MonoBehaviour {
  9. public StorageScript storageScript;
  10. public Texture2D texture2D;
  11. public string printerName = "";
  12. public int copies = 1;
  13. public InputField inputField;
  14. public void printSmileButton()
  15. {
  16. if (storageScript.IsSingleNumber())
  17. {
  18. storageScript.Status_Color_Text(Color.blue, "入库条码打印成功");
  19. Print.PrintTexture(storageScript.PrintQrCreat().EncodeToPNG(), copies, printerName);
  20. //Print.PrintTexture(texture2D.EncodeToPNG(), copies, printerName);
  21. return;
  22. }
  23. storageScript.Status_Color_Text(Color.red, "没有有效的打印内容");
  24. }
  25. public void printByPathButton()
  26. {
  27. Print.PrintTextureByPath("D:\\pic.png", copies, printerName);
  28. //Print.PrintTextureByPath(inputField.text.Trim(), copies, printerName);
  29. }
  30. }

 

调用脚本

  1. /// <summary>
  2. /// 打印条码
  3. /// </summary>
  4. public Texture2D PrintQrCreat()
  5. {
  6. return e_qrController.EncodeT2D(InputField_Text[1].text);
  7. }

 就可以使用了。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/715851
推荐阅读
相关标签
  

闽ICP备14008679号