Unity結合ZXing生成中間帶圖示的二維碼並儲存
阿新 • • 發佈:2018-12-15
1、環境
Win10
Unity3d 2017.1.0f3
ZXing.Net ZXing.Net.0.16.0.0
下載:http://zxingnet.codeplex.com/
2、效果截圖
3、程式碼
using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using ZXing; using ZXing.Common; public class Demo : MonoBehaviour { public RawImage img1; public RawImage img2; public RawImage img3; public Texture2D icon; // Use this for initialization void Start () { GenerateQRImage3("http://baidu.com", 256, 256, icon); } /// <summary> /// 生成2維碼 方法一 /// 經測試:只能生成256x256的 /// </summary> /// <param name="content"></param> /// <param name="width"></param> /// <param name="height"></param> void GenerateQRImage1(string content, int width, int height) { // 編碼成color32 EncodingOptions options = null; BarcodeWriter writer = new BarcodeWriter(); options = new EncodingOptions { Width = width, Height = height, Margin = 1, }; options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); writer.Format = BarcodeFormat.QR_CODE; writer.Options = options; Color32[] colors = writer.Write(content); // 轉成texture2d Texture2D texture = new Texture2D(width, height); texture.SetPixels32(colors); texture.Apply(); img1.texture = texture; //// 儲存成檔案 //byte[] bytes = texture.EncodeToPNG(); //string path = System.IO.Path.Combine(Application.dataPath, "qr.png"); //System.IO.File.WriteAllBytes(path, bytes); } /// <summary> /// 生成2維碼 方法二 /// 經測試:能生成任意尺寸的正方形 /// </summary> /// <param name="content"></param> /// <param name="width"></param> /// <param name="height"></param> void GenerateQRImage2(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.M); BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints); // 轉成texture2d int w = bitMatrix.Width; int h = bitMatrix.Height; print(string.Format("w={0},h={1}", w, h)); 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.red); } else { texture.SetPixel(y, x, Color.white); } } } texture.Apply(); img2.texture = texture; //// 儲存成檔案 //byte[] bytes = texture.EncodeToPNG(); //string path = System.IO.Path.Combine(Application.dataPath, "qr.png"); //System.IO.File.WriteAllBytes(path, bytes); } /// <summary> /// 生成2維碼 方法三 /// 在方法二的基礎上,新增小圖示 /// </summary> /// <param name="content"></param> /// <param name="width"></param> /// <param name="height"></param> 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(); img3.texture = texture; // 儲存成檔案 byte[] bytes = texture.EncodeToPNG(); string path = System.IO.Path.Combine(Application.dataPath, "qr.png"); System.IO.File.WriteAllBytes(path, bytes); } }