当前位置:   article > 正文

Unity用ZXing插件生成二维码_zxing unity

zxing unity

现在ZXing插件并导入到Unity 中,在场景中加一个RawImage

1、使用ZXing生成二维码的第一种方法,新建脚本命名为QrCodeDraw并挂在RawImage上:

  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. /// <summary>
  9. /// 第一种生成二维码方法
  10. /// </summary>
  11. public class QrCodeDraw : MonoBehaviour
  12. {
  13. private Texture2D enCodedTexture;
  14. private string qrCodePath;
  15. public RawImage rawIMg;
  16. // Start is called before the first frame update
  17. void Start()
  18. {
  19. enCodedTexture = new Texture2D(512, 512);
  20. qrCodePath = "https://www.baidu.com/";
  21. Encode(qrCodePath, 512, 512);
  22. }
  23. void Encode(string content, int width, int height)
  24. {
  25. MultiFormatWriter writer = new MultiFormatWriter();
  26. Dictionary<EncodeHintType, object> hints = new Dictionary<EncodeHintType, object>();
  27. hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
  28. BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
  29. //转换成texture2d
  30. int w = bitMatrix.Width;
  31. int h = bitMatrix.Height;
  32. Texture2D texture = new Texture2D(w, h);
  33. for (int x = 0; x < h; x++)
  34. {
  35. for (int y = 0; y < w; y++)
  36. {
  37. if (bitMatrix[x, y])
  38. {
  39. texture.SetPixel(y, x, Color.black);
  40. }
  41. else
  42. {
  43. texture.SetPixel(y, x, Color.white);
  44. }
  45. }
  46. }
  47. texture.Apply();
  48. rawIMg.texture = texture;
  49. }
  50. }

 2、使用ZXing生成二维码的第二种方法,新建脚本命名为QrCodeDraw1并挂在RawImage上

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using ZXing;
  6. using ZXing.QrCode;
  7. public class QrCodeDraw1 : MonoBehaviour
  8. {
  9. private string qrCodePath = "https://cn.bing.com/?mkt=zh-CN";
  10. private Texture texture;
  11. public RawImage rawImg;
  12. // Start is called before the first frame update
  13. void Start()
  14. {
  15. texture= CreatQR_texture(qrCodePath);
  16. rawImg.texture = texture;
  17. }
  18. private static Color32[] Encode(string textForEncoding, int width, int height)
  19. {
  20. var writer = new BarcodeWriter
  21. {
  22. Format = BarcodeFormat.QR_CODE,
  23. Options = new QrCodeEncodingOptions
  24. {
  25. Height = height,
  26. Width = width
  27. }
  28. };
  29. return writer.Write(textForEncoding);
  30. }
  31. public static Texture CreatQR_texture(string message)
  32. {
  33. Texture2D encoded;
  34. encoded = new Texture2D(256, 256);
  35. if (message.Length > 1)
  36. {
  37. //二维码写入图片
  38. var color32 = Encode(message, encoded.width, encoded.height);
  39. encoded.SetPixels32(color32);
  40. encoded.Apply();
  41. }
  42. else
  43. {
  44. Debug.Log("生成二维码失败");
  45. }
  46. return encoded;
  47. }
  48. }

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

闽ICP备14008679号