| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using ZXing;
- using ZXing.Common;
- using ZXing.QrCode;
- public class CreatQR : MonoBehaviour
- {
- //需要生产二维码的字符串数组
- string[] QrCodeStr = { "https://www.baidu.com/", "https://www.cnblogs.com/Mr-Miracle/", "https://unity3d.com/cn", "https://www.sogou.com/" };
- //在屏幕上显示二维码
- public RawImage image;
- //存放二维码
- Texture2D encoded;
- int Nmuber = 0;
- // Use this for initialization
- void Start()
- {
- encoded = new Texture2D(256, 256);
- // GenerateQRImage3("https://www.baidu.com/", 512, 512, centericon);
- //onlyQRCode("https://www.weimaqi.net/index_.aspx?device_name=WXR02153",512,512);
- }
- // [SerializeField]
- // Texture2D centericon;
- // Update is called once per frame
- // void Update()
- // {
- // if (Input.GetKeyDown(KeyCode.Space))
- // {
- // GenerateQRImage3("https://www.baidu.com/", 512, 512, centericon);
- // //image.texture = GenerateQRImageWithIcon("ytwjj", 512, 512, centericon);
- // // Btn_CreatQr();
- // //Nmuber++;
- // //if (Nmuber >= QrCodeStr.Length)
- // //{
- // // Nmuber = 0;
- // //}
- // }
- // }
- /// <summary>
- /// 定义方法生成二维码
- /// </summary>
- /// <param name="textForEncoding">需要生产二维码的字符串</param>
- /// <param name="width">宽</param>
- /// <param name="height">高</param>
- /// <returns></returns>
- private static Color32[] Encode(string textForEncoding, int width, int height)
- {
- var writer = new BarcodeWriter
- {
- Format = BarcodeFormat.QR_CODE,
- Options = new QrCodeEncodingOptions
- {
- Height = height,
- Width = width,
- Margin = 1
- }
- };
- return writer.Write(textForEncoding);
- }
- /// <summary>
- /// 生成二维码
- /// </summary>
- public void Btn_CreatQr()
- {
- if (QrCodeStr[Nmuber].Length > 1)
- {
- //二维码写入图片
- var color32 = Encode(QrCodeStr[Nmuber], encoded.width, encoded.height);
- encoded.SetPixels32(color32);
- encoded.Apply();
- //生成的二维码图片附给RawImage
- image.texture = encoded;
- }
- else
- {
- GameObject.Find("Text_1").GetComponent<Text>().text = "没有生成信息";
- }
- }
- /// <summary>
- /// 生成中心带有小图标二维码
- /// </summary>
- /// <param name="content"></param>
- /// <param name="width"></param>
- /// <param name="height"></param>
- /// <param name="centerIcon"></param>
- /// <returns></returns>
- public static Texture2D GenerateQRImageWithIcon(string content, int width, int height, Texture2D centerIcon)
- {
- // 编码成color32
- MultiFormatWriter writer = new MultiFormatWriter();
- Dictionary<EncodeHintType, object> hints = new Dictionary<EncodeHintType, object>();
- hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
- hints.Add(EncodeHintType.MARGIN, 1);
- hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H);
- BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
- // 转成texture2d
- int w = bitMatrix.Width;
- int h = bitMatrix.Height;
- Texture2D texture = new Texture2D(w, h);
- for (int x = 0; x < h; x++)
- {
- for (int y = 0; y < w; y++)
- {
- if (bitMatrix[x, y])
- {
- texture.SetPixel(y, x, Color.black);
- }
- else
- {
- texture.SetPixel(y, x, Color.white);
- }
- }
- }
- // 添加小图
- int halfWidth = texture.width / 2;
- int halfHeight = texture.height / 2;
- int halfWidthOfIcon = centerIcon.width / 4;
- int halfHeightOfIcon = centerIcon.height / 4;
- int centerOffsetX = 0;
- int centerOffsetY = 0;
- for (int x = 0; x < h; x++)
- {
- for (int y = 0; y < w; y++)
- {
- centerOffsetX = x - halfWidth;
- centerOffsetY = y - halfHeight;
- if (Mathf.Abs(centerOffsetX) <= halfWidthOfIcon && Mathf.Abs(centerOffsetY) <= halfHeightOfIcon)
- {
- texture.SetPixel(x, y, centerIcon.GetPixel(centerOffsetX + halfWidthOfIcon, centerOffsetY + halfHeightOfIcon));
- }
- }
- }
- texture.Apply();
- return texture;
- }
- /// <summary>
- /// 生成2维码 方法三
- /// 在方法二的基础上,添加小图标
- /// </summary>
- /// <param name="content"></param>
- /// <param name="width"></param>
- /// <param name="height"></param>
- public void GenerateQRImage3(string content, int width, int height, Texture2D centerIcon)
- {
- // 编码成color32
- MultiFormatWriter writer = new MultiFormatWriter();
- Dictionary<EncodeHintType, object> hints = new Dictionary<EncodeHintType, object>();
- hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
- hints.Add(EncodeHintType.MARGIN, 1);
- hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H);
- BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
- // 转成texture2d
- int w = bitMatrix.Width;
- int h = bitMatrix.Height;
- Texture2D texture = new Texture2D(w, h);
- for (int x = 0; x < h; x++)
- {
- for (int y = 0; y < w; y++)
- {
- if (bitMatrix[x, y])
- {
- texture.SetPixel(y, x, Color.blue);
- }
- else
- {
- texture.SetPixel(y, x, Color.white);
- }
- }
- }
- // 添加小图
- int halfWidth = texture.width / 2;
- int halfHeight = texture.height / 2;
- int halfWidthOfIcon = centerIcon.width / 2;
- int halfHeightOfIcon = centerIcon.height / 2;
- int centerOffsetX = 0;
- int centerOffsetY = 0;
- for (int x = 0; x < h; x++)
- {
- for (int y = 0; y < w; y++)
- {
- centerOffsetX = x - halfWidth;
- centerOffsetY = y - halfHeight;
- if (Mathf.Abs(centerOffsetX) <= halfWidthOfIcon && Mathf.Abs(centerOffsetY) <= halfHeightOfIcon)
- {
- texture.SetPixel(x, y, centerIcon.GetPixel(centerOffsetX + halfWidthOfIcon, centerOffsetY + halfHeightOfIcon));
- }
- }
- }
- texture.Apply();
- image.texture = texture;
- // 存储成文件
- //byte[] bytes = texture.EncodeToPNG();
- //string path = System.IO.Path.Combine(Application.dataPath, "qr.png");
- //System.IO.File.WriteAllBytes(path, bytes);
- }
- public void onlyQRCode(string content, int width, int height) {
- // 编码成color32
- MultiFormatWriter writer = new MultiFormatWriter();
- Dictionary<EncodeHintType, object> hints = new Dictionary<EncodeHintType, object>();
- hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
- hints.Add(EncodeHintType.MARGIN, 1);
- hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H);
- BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
- // 转成texture2d
- int w = bitMatrix.Width;
- int h = bitMatrix.Height;
- Texture2D texture = new Texture2D(w, h);
- for (int x = 0; x < h; x++)
- {
- for (int y = 0; y < w; y++)
- {
- if (bitMatrix[x, y])
- {
- texture.SetPixel(y, x, Color.blue);
- }
- else
- {
- texture.SetPixel(y, x, Color.white);
- }
- }
- }
- texture.Apply();
- image.texture = texture;
- }
- }
|