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; // //} // } // } /// /// 定义方法生成二维码 /// /// 需要生产二维码的字符串 /// 宽 /// 高 /// 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); } /// /// 生成二维码 /// 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 = "没有生成信息"; } } /// /// 生成中心带有小图标二维码 /// /// /// /// /// /// public static Texture2D GenerateQRImageWithIcon(string content, int width, int height, Texture2D centerIcon) { // 编码成color32 MultiFormatWriter writer = new MultiFormatWriter(); Dictionary hints = new Dictionary(); 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; } /// /// 生成2维码 方法三 /// 在方法二的基础上,添加小图标 /// /// /// /// public void GenerateQRImage3(string content, int width, int height, Texture2D centerIcon) { // 编码成color32 MultiFormatWriter writer = new MultiFormatWriter(); Dictionary hints = new Dictionary(); 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 hints = new Dictionary(); 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; } }