生成和识别二维码U3d

转:http://www.manew.com/thread-47945-1-1.html?_dsign=3548f138

工作繁忙,很久没有更新,略表歉意。关于二维码的生成和识别网上泛泛看了一些,ZXing用的比较多。

ZXing的官网是http://zxingnet.codeplex.com/,它在github有源码,有兴趣的可以自己翻翻看看。


publicclass GenerateQRCode : MonoBehaviour {
 
    publicTexture2D _texture;
    publicstring content;
    Color32[] webCameraTextureData;
    privateBarcodeReader barcodeReader;
 
    voidStart()
    {
        _texture = newTexture2D(256, 256);
        content = "http://www.baidu.com";
        barcodeReader = newBarcodeReader();
        Generateimages();
        Recognition();
    }
 
    //生成图片
    voidGenerateimages()
    {
        var textForEncoding = content;
        if(textForEncoding != null)
        {
            var color32 = Encode(textForEncoding, _texture.width, _texture.height);
            _texture.SetPixels32(color32);
            _texture.Apply();
            //赋值给webCameraTextureData用于识别
            webCameraTextureData = color32;
        }
    }
    //生成二维码信息
    privatestatic Color32[] Encode(stringtextForEncoding, intwidth, intheight)
    {
        var writer = newBarcodeWriter
        {
            Format = BarcodeFormat.QR_CODE,
            Options = newQrCodeEncodingOptions
            {
                Height = height,
                Width = width
            }
        };
        returnwriter.Write(textForEncoding);
    }
     
    
    voidOnGUI()
    {
        GUI.DrawTexture(newRect(0, 0, 256, 256), _texture);
    }
 
    //识别二维码
    publicvoid Recognition()
    {
        var data = barcodeReader.Decode(webCameraTextureData, _texture.width, _texture.height);
        switch(data.BarcodeFormat)
        {
            caseBarcodeFormat.QR_CODE:
                break;
        }
        Debug.Log(data);   
    }
 
}







整体代码还是比较简单,为了方便,我识别直接用了生成的二维码,有需求的可以换成从摄像头获取。