Unity-基于ZXing.Net实现二维码的制作

来源:互联网 发布:来自远古星星的你 知乎 编辑:程序博客网 时间:2024/06/05 02:57

转自:http://blog.csdn.net/hasion/article/details/44653453


在Unity中,我们也可以实现二维码的制作,这需要借助Zxing的帮助,首先下载ZXing.Net。我这边下载的是ZXing.Net 0.14.0.0 大家可以点击这个链接直接下载:http://zxingnet.codeplex.com/downloads/get/824664   下载解压之后可以看到有一个unity文件夹,里面包含了三个文件。把文件夹拖到Unity工程中,在空物体上挂上下面这个脚本,即可生成对应的二维码。

[csharp] view plain copy
 print?
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using ZXing;  
  4. using ZXing.QrCode;  
  5. using System;  
  6. using ZXing.Common;  
  7. using ZXing.Rendering;  
  8. using System.Collections.Generic;  
  9.   
  10. public class BarcodeCam : MonoBehaviour  
  11. {  
  12.     public Texture2D encoded;  
  13.   
  14.   
  15.   
  16.     void Start()  
  17.     {  
  18.         //设置二维码大小  
  19.         encoded = new Texture2D(512, 512);  
  20.         //二维码边框  
  21.         BitMatrix BIT;  
  22.         //设置二维码扫描结果  
  23.         string name = "http://www.baidu.com";  
  24.   
  25.         Dictionary<EncodeHintType, object> hints = new Dictionary<EncodeHintType, object>();  
  26.   
  27.         //设置编码方式  
  28.         hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");  
  29.   
  30.         BIT = new MultiFormatWriter().encode(name, BarcodeFormat.QR_CODE, 512, 512, hints);  
  31.         int width = BIT.Width;  
  32.         int height = BIT.Width;  
  33.   
  34.         for (int x = 0; x < height; x++)  
  35.         {  
  36.             for (int y = 0; y < width; y++)  
  37.             {  
  38.                 if (BIT[x, y])  
  39.                 {  
  40.                     encoded.SetPixel(y, x, Color.black);  
  41.                 }  
  42.                 else  
  43.                 {  
  44.                     encoded.SetPixel(y, x, Color.white);  
  45.                 }  
  46.   
  47.             }  
  48.         }  
  49.         encoded.Apply();  
  50.   
  51.     }  
  52.   
  53.     void OnGUI()  
  54.     {  
  55.         GUI.DrawTexture(new Rect(100, 100, 256, 256), encoded);  
  56.     }  
  57.   
  58. }  

对于其中的MultiFormatWriter,以及BitMatrix他们具体用法,我们在封装好的dll中都可以找到。下面贴出他们的具体实现方法:

[csharp] view plain copy
 print?
  1. // Generated by .NET Reflector from C:\Users\Win8_CX01\Desktop\dddd\Assets\unity\zxing.unity.dll  
  2. namespace ZXing  
  3. {  
  4.     using System;  
  5.     using System.Collections.Generic;  
  6.     using ZXing.Aztec;  
  7.     using ZXing.Common;  
  8.     using ZXing.Datamatrix;  
  9.     using ZXing.OneD;  
  10.     using ZXing.PDF417;  
  11.     using ZXing.QrCode;  
  12.       
  13.     public sealed class MultiFormatWriter : Writer  
  14.     {  
  15.         private static readonly IDictionary<BarcodeFormat, Func<Writer>> formatMap;  
  16.           
  17.         static MultiFormatWriter()  
  18.         {  
  19.             Dictionary<BarcodeFormat, Func<Writer>> dictionary = new Dictionary<BarcodeFormat, Func<Writer>>();  
  20.             dictionary.Add(BarcodeFormat.EAN_8, () => new EAN8Writer());  
  21.             dictionary.Add(BarcodeFormat.EAN_13, () => new EAN13Writer());  
  22.             dictionary.Add(BarcodeFormat.UPC_A, () => new UPCAWriter());  
  23.             dictionary.Add(BarcodeFormat.QR_CODE, () => new QRCodeWriter());  
  24.             dictionary.Add(BarcodeFormat.CODE_39, () => new Code39Writer());  
  25.             dictionary.Add(BarcodeFormat.CODE_128, () => new Code128Writer());  
  26.             dictionary.Add(BarcodeFormat.ITF, () => new ITFWriter());  
  27.             dictionary.Add(BarcodeFormat.PDF_417, () => new PDF417Writer());  
  28.             dictionary.Add(BarcodeFormat.CODABAR, () => new CodaBarWriter());  
  29.             dictionary.Add(BarcodeFormat.MSI, () => new MSIWriter());  
  30.             dictionary.Add(BarcodeFormat.PLESSEY, () => new PlesseyWriter());  
  31.             dictionary.Add(BarcodeFormat.DATA_MATRIX, () => new DataMatrixWriter());  
  32.             dictionary.Add(BarcodeFormat.AZTEC, () => new AztecWriter());  
  33.             formatMap = dictionary;  
  34.         }  
  35.           
  36.         public BitMatrix encode(string contents, BarcodeFormat format, int width, int height)  
  37.         {  
  38.             return this.encode(contents, format, width, height, null);  
  39.         }  
  40.           
  41.         public BitMatrix encode(string contents, BarcodeFormat format, int width, int height, IDictionary<EncodeHintType, object> hints)  
  42.         {  
  43.             if (!formatMap.ContainsKey(format))  
  44.             {  
  45.                 throw new ArgumentException("No encoder available for format " + format);  
  46.             }  
  47.             return formatMap[format]().encode(contents, format, width, height, hints);  
  48.         }  
  49.           
  50.         public static ICollection<BarcodeFormat> SupportedWriters  
  51.         {  
  52.             get  
  53.             {  
  54.                 return formatMap.Keys;  
  55.             }  
  56.         }  
  57.     }  
  58. }  
[csharp] view plain copy
 print?
  1. namespace ZXing.Common  
  2. {  
  3.     using System;  
  4.     using System.Reflection;  
  5.     using System.Text;  
  6.       
  7.     public sealed class BitMatrix  
  8.     {  
  9.         private readonly int[] bits;  
  10.         private readonly int height;  
  11.         private readonly int rowSize;  
  12.         private readonly int width;  
  13.           
  14.         public BitMatrix(int dimension) : this(dimension, dimension)  
  15.         {  
  16.         }  
  17.           
  18.         public BitMatrix(int width, int height)  
  19.         {  
  20.             if ((width < 1) || (height < 1))  
  21.             {  
  22.                 throw new ArgumentException("Both dimensions must be greater than 0");  
  23.             }  
  24.             this.width = width;  
  25.             this.height = height;  
  26.             this.rowSize = (width + 0x1f) >> 5;  
  27.             this.bits = new int[this.rowSize * height];  
  28.         }  
  29.           
  30.         private BitMatrix(int width, int height, int rowSize, int[] bits)  
  31.         {  
  32.             this.width = width;  
  33.             this.height = height;  
  34.             this.rowSize = rowSize;  
  35.             this.bits = bits;  
  36.         }  
  37.           
  38.         public void clear()  
  39.         {  
  40.             int length = this.bits.Length;  
  41.             for (int i = 0; i < length; i++)  
  42.             {  
  43.                 this.bits[i] = 0;  
  44.             }  
  45.         }  
  46.           
  47.         public object Clone()  
  48.         {  
  49.             return new BitMatrix(this.width, this.height, this.rowSize, (int[]) this.bits.Clone());  
  50.         }  
  51.           
  52.         public override bool Equals(object obj)  
  53.         {  
  54.             if (!(obj is BitMatrix))  
  55.             {  
  56.                 return false;  
  57.             }  
  58.             BitMatrix matrix = (BitMatrix) obj;  
  59.             if (((this.width != matrix.width) || (this.height != matrix.height)) || ((this.rowSize != matrix.rowSize) || (this.bits.Length != matrix.bits.Length)))  
  60.             {  
  61.                 return false;  
  62.             }  
  63.             for (int i = 0; i < this.bits.Length; i++)  
  64.             {  
  65.                 if (this.bits[i] != matrix.bits[i])  
  66.                 {  
  67.                     return false;  
  68.                 }  
  69.             }  
  70.             return true;  
  71.         }  
  72.           
  73.         public void flip(int x, int y)  
  74.         {  
  75.             int index = (y * this.rowSize) + (x >> 5);  
  76.             this.bits[index] ^= ((int) 1) << x;  
  77.         }  
  78.           
  79.         public int[] getBottomRightOnBit()  
  80.         {  
  81.             int index = this.bits.Length - 1;  
  82.             while ((index >= 0) && (this.bits[index] == 0))  
  83.             {  
  84.                 index--;  
  85.             }  
  86.             if (index < 0)  
  87.             {  
  88.                 return null;  
  89.             }  
  90.             int num2 = index / this.rowSize;  
  91.             int num3 = (index % this.rowSize) << 5;  
  92.             int num4 = this.bits[index];  
  93.             int num5 = 0x1f;  
  94.             while ((num4 >> num5) == 0)  
  95.             {  
  96.                 num5--;  
  97.             }  
  98.             num3 += num5;  
  99.             return new int[] { num3, num2 };  
  100.         }  
  101.           
  102.         public int[] getEnclosingRectangle()  
  103.         {  
  104.             int width = this.width;  
  105.             int height = this.height;  
  106.             int num3 = -1;  
  107.             int num4 = -1;  
  108.             for (int i = 0; i < this.height; i++)  
  109.             {  
  110.                 for (int j = 0; j < this.rowSize; j++)  
  111.                 {  
  112.                     int num7 = this.bits[(i * this.rowSize) + j];  
  113.                     if (num7 != 0)  
  114.                     {  
  115.                         if (i < height)  
  116.                         {  
  117.                             height = i;  
  118.                         }  
  119.                         if (i > num4)  
  120.                         {  
  121.                             num4 = i;  
  122.                         }  
  123.                         if ((j * 0x20) < width)  
  124.                         {  
  125.                             int num8 = 0;  
  126.                             while ((num7 << (0x1f - num8)) == 0)  
  127.                             {  
  128.                                 num8++;  
  129.                             }  
  130.                             if (((j * 0x20) + num8) < width)  
  131.                             {  
  132.                                 width = (j * 0x20) + num8;  
  133.                             }  
  134.                         }  
  135.                         if (((j * 0x20) + 0x1f) > num3)  
  136.                         {  
  137.                             int num9 = 0x1f;  
  138.                             while ((num7 >> num9) == 0)  
  139.                             {  
  140.                                 num9--;  
  141.                             }  
  142.                             if (((j * 0x20) + num9) > num3)  
  143.                             {  
  144.                                 num3 = (j * 0x20) + num9;  
  145.                             }  
  146.                         }  
  147.                     }  
  148.                 }  
  149.             }  
  150.             int num10 = num3 - width;  
  151.             int num11 = num4 - height;  
  152.             if ((num10 < 0) || (num11 < 0))  
  153.             {  
  154.                 return null;  
  155.             }  
  156.             return new int[] { width, height, num10, num11 };  
  157.         }  
  158.           
  159.         public override int GetHashCode()  
  160.         {  
  161.             int width = this.width;  
  162.             width = (0x1f * width) + this.width;  
  163.             width = (0x1f * width) + this.height;  
  164.             width = (0x1f * width) + this.rowSize;  
  165.             foreach (int num2 in this.bits)  
  166.             {  
  167.                 width = (0x1f * width) + num2.GetHashCode();  
  168.             }  
  169.             return width;  
  170.         }  
  171.           
  172.         public BitArray getRow(int y, BitArray row)  
  173.         {  
  174.             if ((row == null) || (row.Size < this.width))  
  175.             {  
  176.                 row = new BitArray(this.width);  
  177.             }  
  178.             else  
  179.             {  
  180.                 row.clear();  
  181.             }  
  182.             int num = y * this.rowSize;  
  183.             for (int i = 0; i < this.rowSize; i++)  
  184.             {  
  185.                 row.setBulk(i << 5, this.bits[num + i]);  
  186.             }  
  187.             return row;  
  188.         }  
  189.           
  190.         public int[] getTopLeftOnBit()  
  191.         {  
  192.             int index = 0;  
  193.             while ((index < this.bits.Length) && (this.bits[index] == 0))  
  194.             {  
  195.                 index++;  
  196.             }  
  197.             if (index == this.bits.Length)  
  198.             {  
  199.                 return null;  
  200.             }  
  201.             int num2 = index / this.rowSize;  
  202.             int num3 = (index % this.rowSize) << 5;  
  203.             int num4 = this.bits[index];  
  204.             int num5 = 0;  
  205.             while ((num4 << (0x1f - num5)) == 0)  
  206.             {  
  207.                 num5++;  
  208.             }  
  209.             num3 += num5;  
  210.             return new int[] { num3, num2 };  
  211.         }  
  212.           
  213.         public void rotate180()  
  214.         {  
  215.             int width = this.Width;  
  216.             int height = this.Height;  
  217.             BitArray row = new BitArray(width);  
  218.             BitArray array2 = new BitArray(width);  
  219.             for (int i = 0; i < ((height + 1) / 2); i++)  
  220.             {  
  221.                 row = this.getRow(i, row);  
  222.                 array2 = this.getRow((height - 1) - i, array2);  
  223.                 row.reverse();  
  224.                 array2.reverse();  
  225.                 this.setRow(i, array2);  
  226.                 this.setRow((height - 1) - i, row);  
  227.             }  
  228.         }  
  229.           
  230.         public void setRegion(int left, int top, int width, int height)  
  231.         {  
  232.             if ((top < 0) || (left < 0))  
  233.             {  
  234.                 throw new ArgumentException("Left and top must be nonnegative");  
  235.             }  
  236.             if ((height < 1) || (width < 1))  
  237.             {  
  238.                 throw new ArgumentException("Height and width must be at least 1");  
  239.             }  
  240.             int num = left + width;  
  241.             int num2 = top + height;  
  242.             if ((num2 > this.height) || (num > this.width))  
  243.             {  
  244.                 throw new ArgumentException("The region must fit inside the matrix");  
  245.             }  
  246.             for (int i = top; i < num2; i++)  
  247.             {  
  248.                 int num4 = i * this.rowSize;  
  249.                 for (int j = left; j < num; j++)  
  250.                 {  
  251.                     this.bits[num4 + (j >> 5)] |= ((int) 1) << j;  
  252.                 }  
  253.             }  
  254.         }  
  255.           
  256.         public void setRow(int y, BitArray row)  
  257.         {  
  258.             Array.Copy(row.Array, 0, this.bits, y * this.rowSize, this.rowSize);  
  259.         }  
  260.           
  261.         public override string ToString()  
  262.         {  
  263.             StringBuilder builder = new StringBuilder(this.height * (this.width + 1));  
  264.             for (int i = 0; i < this.height; i++)  
  265.             {  
  266.                 for (int j = 0; j < this.width; j++)  
  267.                 {  
  268.                     builder.Append(this[j, i] ? "X " : "  ");  
  269.                 }  
  270.                 builder.AppendLine("");  
  271.             }  
  272.             return builder.ToString();  
  273.         }  
  274.           
  275.         public int Dimension  
  276.         {  
  277.             get  
  278.             {  
  279.                 if (this.width != this.height)  
  280.                 {  
  281.                     throw new ArgumentException("Can't call getDimension() on a non-square matrix");  
  282.                 }  
  283.                 return this.width;  
  284.             }  
  285.         }  
  286.           
  287.         public int Height  
  288.         {  
  289.             get  
  290.             {  
  291.                 return this.height;  
  292.             }  
  293.         }  
  294.           
  295.         public bool this[int x, int y]  
  296.         {  
  297.             get  
  298.             {  
  299.                 int index = (y * this.rowSize) + (x >> 5);  
  300.                 return (((this.bits[index] >> x) & 1) != 0);  
  301.             }  
  302.             set  
  303.             {  
  304.                 if (value)  
  305.                 {  
  306.                     int index = (y * this.rowSize) + (x >> 5);  
  307.                     this.bits[index] |= ((int) 1) << x;  
  308.                 }  
  309.             }  
  310.         }  
  311.           
  312.         public int Width  
  313.         {  
  314.             get  
  315.             {  
  316.                 return this.width;  
  317.             }  
  318.         }  
  319.     }  
  320. }  
0 0
原创粉丝点击