生成和识别二维码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);   
    }
 
}







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

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值