二维码的生成<一>

[+]
  1. 二维码识别和生成二维码
    1. 前言有的时候需要进行二维码扫描但是二维码是怎么在Unity中进行生成呢本文将介绍到同时还会涉及到另外一个插件可以进行对二维码的扫描
    2. 正文

二维码识别和生成二维码

 

     前言:有的时候需要进行二维码扫描,但是二维码是怎么在Unity中进行生成呢?本文将介绍到,同时还会涉及到另外一个插件,可以进行对二维码的扫描。

准备资料:

ZXing.Net.0.14.0.0.zip 作用:实现二维码生成的库


Easy Code Scanner.unitypackage 作用:实现二维码扫描

正文:

一:二维码生成

1:我们先实现二维码的生成吧,下载ZXing.Net.0.14.0.0.zip;

下载完毕后,解压会发现有一个Unity的文件夹,把Unity文件夹下的三个文件都拖放到Unity编辑器中,这三个文件是生成二维码的类库

2:创建一个名称为BarcodeCamC#脚本,然后把以下脚本拖放进去,把脚本拖放到摄像机运行即可,就可以根据参数生成二维码了。

脚本,如下:

using UnityEngine;
using System.Collections;
using ZXing;
using ZXing.QrCode;
using System;
using System.IO;
public class BarcodeCam : MonoBehaviour
{
    public Texture2D encoded;//生成的二维码为Texture2D类型  
    public string Lastresult;//二维码中所包含的内容信息,我是使用了GUID进行代替  
    public int count = 5;//生成几个二维码  
    void Start()
    {

        encoded = new Texture2D(256, 256);
        for (int i = 0; i < count; i++)
        {
            Guid idKey = Guid.NewGuid();  
            Lastresult = idKey.ToString();  //二维码中所包含的内容信息
            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 + "/Texture2DFolder"))//创建生成目录,如果不存在则创建目录  
                {
                    Directory.CreateDirectory(Application.dataPath + "/Texture2DFolder");
                }
                string fileName = Application.dataPath + "/Texture2DFolder/" + idKey + ".png";
                System.IO.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);
    }

    void OnGUI()
    {
        GUI.DrawTexture(new Rect(100, 100, 256, 256), encoded);
    }

}


二.二维码解析:
using UnityEngine;
using System.Collections;
using ZXing;
using ZXing.QrCode;
using System;
using System.IO;
using UnityEngine.UI;
using System.Threading;

public class BarcodeCam1 : MonoBehaviour
{

    public Texture2D encoded;
    public RawImage camsx;   //UI上用于显示摄像头显示的内容;
    public Text rsText;     //ZXing解析的结果
    //private string TestStr;   //网络返回的验证数据结果
    private int sw, sh;
    public static string driverID;  //设备唯一标识

    //代表着摄像机;
    private WebCamTexture camTexture;
    private Thread qrThread;

    private Color32[] c;
    private int cw, ch;
    private Rect screenRect;
    private bool isQuit;

    public string LastResult;
    private string QRScan;
    private bool shouldEncodNow;
    void OnGUI()
    {
        GUI.Label(screenRect,driverID);
    }
    void OnEnable()
    {
        if (camTexture != null)
        {
            camTexture.Play();
            cw = camTexture.width;
            ch = camTexture.height;
        }
    }
    void OnDisable()
    {
        if (camTexture != null)
        {
            camTexture.Pause();
        }
    }
    void OnDestroy()
    {
        qrThread.Abort();
        camTexture.Stop();
    }
    void OnApplicationQuit()
    {
        isQuit = true;
    }
    void Start()
    {
        driverID = SystemInfo.deviceUniqueIdentifier; //设备唯一性
        encoded = new Texture2D(256,256);
        LastResult = "WWW.baidu.com";
        shouldEncodNow = false;
        screenRect = new Rect(0,0,Screen.width,Screen.height);
        camTexture = new WebCamTexture();
        if (Screen.width < 1280)
        {
            sw = 640;
        }
        else {
            sw = 1280;
        }

        //在unity界面上显示扫描的内容;
        camTexture.requestedHeight = 720;
        camTexture.requestedWidth = sw;
        camsx.texture = camTexture;
        camsx.material.mainTexture = camTexture;
        OnEnable();

        qrThread = new Thread(DecodeQR);
        qrThread.Start();
    }
    void Update()
    {
        if (c == null & (camTexture.isPlaying))
        {
            c = camTexture.GetPixels32();
            cw = camTexture.width;
            ch = camTexture.height;
        }
        rsText.text = LastResult;
        camsx.enabled = false;

        camsx.enabled = true;
        var textForEncoding = LastResult;
        if (shouldEncodNow && textForEncoding != null)
        {
            var color32 = Encode(textForEncoding,encoded.width,encoded.height);
            encoded.SetPixels32(color32);
            encoded.Apply();
            shouldEncodNow = false;
            if (QRScan != LastResult)
            {
                QRScan = LastResult;
                //to do 如果遇到新的二维码那么就执行其他的操作(如:网络请求验证)
                //StartCoroutine(CheckCode(LastResult));
            }
        }
    }
    //解码扫描的内容
    void DecodeQR()
    {
        var barcodeReader = new BarcodeReader { AutoRotate = false, TryHarder = false };
        while (true)
        {
            if (isQuit) break;
            try
            {
                if (c != null)
                {
                    var result = barcodeReader.Decode(c, cw, ch);
                    if (result != null)
                    {
                        LastResult = result.Text;
                        shouldEncodNow = true;
                    }
                    Thread.Sleep(200);
                    c = null;
                }
            }
            catch { }
        }
    }
    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);
     }
}
未完待续...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值