简易版生成二维码

先在Assets工程 里Plugins配置文件夹 放一个zxing.unity.dll动态链接库

/*              #########                       
              ############                     
              #############                    
             ##  ###########                   
            ###  ###### #####                  
            ### #######   ####                 
           ###  ########## ####                
          ####  ########### ####               
         ####   ###########  #####             
        #####   ### ########   #####           
       #####   ###   ########   ######         
      ######   ###  ###########   ######       
     ######   #### ##############  ######      
    #######  #####################  ######     
    #######  ######################  ######    
   #######  ###### #################  ######   
   #######  ###### ###### #########   ######   
   #######    ##  ######   ######     ######   
   #######        ######    #####     #####    
    ######        #####     #####     ####     
     #####        ####      #####     ###      
      #####       ###        ###      #        
        ###       ###        ###              
         ##       ###        ###               
__________#_______####_______####______________
    身是菩提树,心如明镜台,时时勤拂拭,勿使惹尘埃。
                我们的未来没有BUG              
* ==============================================================================
* Filename: Instering
* Created:  2017/8/1
* Author:   WYC
* Purpose:  生成的二维码为Texture2D类型
* ==============================================================================
*/
using System.IO;
using UnityEngine;
using ZXing;
using ZXing.QrCode;

public class BarcodeCamme : MonoBehaviour {

    public Texture2D encoded;//生成的二维码为Texture2D类型  
    public string Lastresult;//二维码中所包含的内容信息,我是使用了GUID进行代替  

    void Start()
    {

        encoded = new Texture2D(256, 256);
        Lastresult = "http://www.vrts.co/1.jpg";//根据这个网址生成二维码

        var textForEncoding = Lastresult;
            if (textForEncoding != null)
            {
                var color32 = Encode(textForEncoding, encoded.width, encoded.height);
                encoded.SetPixels32(color32);
                encoded.Apply();
            byte[] bytes = encoded.EncodeToPNG();//把二维码转成byte数组,然后进行输出保存为png图片就可以保存下来生成好的二维码  
            if (!Directory.Exists(Application.dataPath + "/AdamBieber"))//创建生成目录,如果不存在则创建目录  
            {
                Directory.CreateDirectory(Application.dataPath + "/AdamBieber");
            }
            string fileName = Application.dataPath + "/AdamBieber/二维码.png";
            File.WriteAllBytes(fileName, bytes);
        }
        
    }


    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
            }
        };
        return writer.Write(textForEncoding);
    }

  

}

 

posted @ 2017-11-10 14:23  一只默默奮鬥的程序菌  阅读(315)  评论(0)    收藏  举报