利用ZXing.Net生成和识别二维码

ZXing.Net:ZXing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库。

github:https://github.com/micjahn/ZXing.Net

直接将字符串生成的二维码输出在Image上,要引用ZXing,ZXing.Common,ZXing.QrCode三个命名空间

 public void ShowQR(string str)
    {
        //设置二维码大小  
        Texture2D encoded = new Texture2D(512, 512);  
        //二维码边框  
        BitMatrix BIT;  

        Dictionary<EncodeHintType, object> hints = new Dictionary<EncodeHintType, object>();  
  
        //设置编码方式  
        hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
        BIT = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, 512, 512, hints);  
        int width = BIT.Width;  
        int height = BIT.Width;  
  
        for (int x = 0; x < height; x++)  
        {  
            for (int y = 0; y < width; y++)  
            {  
                if (BIT[x, y])  
                {  
                    encoded.SetPixel(y, x, Color.black);  
                }  
                else  
                {  
                    encoded.SetPixel(y, x, Color.white);  
                }  
  
            }  
        }  
        encoded.Apply();  
        qrImage.sprite = Sprite.Create(encoded,new Rect(0,0,encoded.width,encoded.height), Vector2.zero);
    }

posted on 2020-01-17 14:07  奇个旦Game  阅读(484)  评论(0编辑  收藏  举报

导航