Unity二维码插件 ZXing

1.二维码常见的生成与识别途径

1.草料二维码 https://cli.im/text

2.在软件中实现生成和扫描二维码 使用zxing实现

zxing是一个用java写的开源项目,zxing.net是移植到.net工程上的。

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

2.实现二维码的识别

 1.Unity工程

2.让RawImage显示摄像头内容

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using ZXing;

using UnityEngine.UI;

public class QRCodeTest : MonoBehaviour {

    public RawImage cameraTexture;

    private WebCamTexture webCamTexture;

    // Use this for initialization
    void Start () {
        WebCamDevice[] devices = WebCamTexture.devices;

        string deviceName = devices[0].name;

        webCamTexture = new WebCamTexture(deviceName, 400, 300);

        cameraTexture.texture = webCamTexture;

        webCamTexture.Play();
    }
    // Update is called once per frame
    void Update () {

    }

}

3.扫描功能实现代码(代码有点长,慢慢看)

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using ZXing;

using UnityEngine.UI;

public class QRCodeTest : MonoBehaviour {

    public RawImage cameraTexture;//存储摄像头拍到的内容

    private WebCamTexture webCamTexture;//摄像头的内容

    Color32[] data;

    BarcodeReader barcodeReader;//Zxing提供的读取摄像头内容的方法

    float interval = 0f;//做定时器用

    // Use this for initialization
    void Start () {
        //打开了摄像头
        WebCamDevice[] devices = WebCamTexture.devices;

        string deviceName = devices[0].name;

        webCamTexture = new WebCamTexture(deviceName, 400, 300);

        cameraTexture.texture = webCamTexture;

        webCamTexture.Play();

        barcodeReader = new BarcodeReader();
    }

    // Update is called once per frame
    void Update () {
        interval += Time.deltaTime;
        if (interval >= 3f) {
            ScanQRCode();

            interval = 0f;
        }
    }

    void ScanQRCode()
    {
        //GetPixels32是把格式转换为Color32的方法

        data = webCamTexture.GetPixels32();

        //result存储读取的内容

        var result = barcodeReader.Decode(data, webCamTexture.width, webCamTexture.height);

        if (result != null) {
            Debug.Log(result.Text);
        }
    }
}

3.实现二维码的生成 (注:我关掉网络也能成功识别生成的二维码,说明这东西是离线的)

1.新建一个RawImage存储生成的识别图

2.添加了生成二维码的方法:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using ZXing;

using UnityEngine.UI;


public class QRCodeTest : MonoBehaviour {

    public RawImage cameraTexture;//存储摄像头拍到的内容

    public RawImage QRCode;//存储生成的二维码

    private WebCamTexture webCamTexture;//摄像头的内容

    Color32[] data;

    BarcodeReader barcodeReader;//Zxing提供的读取摄像头内容的方法

    BarcodeWriter barcodeWriter;//Zxing提供的写内容的方法

    float interval = 0f;//做定时器用

    // Use this for initialization
    void Start () {

        //打开了摄像头

        WebCamDevice[] devices = WebCamTexture.devices;

        string deviceName = devices[0].name;

        webCamTexture = new WebCamTexture(deviceName, 400, 300);

        cameraTexture.texture = webCamTexture;

        webCamTexture.Play();

        barcodeReader = new BarcodeReader();
    }

    // Update is called once per frame
    void Update () {

        interval += Time.deltaTime;

        if (interval >= 3f) {

            ScanQRCode();

            interval = 0f;
        }

        //按下空格键生成二维码

        if (Input.GetKeyDown(KeyCode.Space))
        {
            //在这种写法里 只能填入256

            ShowQRCode("我爱学习", 256, 256);

            //如果想要其他大小的二维码呢?见文章底部链接
        }
    }

    //扫描二维码方法
    void ScanQRCode()
    {
        //GetPixels32是从一张图片上获取颜色的方法

        data = webCamTexture.GetPixels32();

        //result存储读取的内容

        var result = barcodeReader.Decode(data, webCamTexture.width, webCamTexture.height);

        if (result != null) {

            Debug.Log(result.Text);
        }
    }


    //显示生成的二维码

    void ShowQRCode(string str,int width,int height) {

        //定义texture2d并且填充

        Texture2D t = new Texture2D(width, height);

        t.SetPixels32(GeneQRCode(str, width, height));

        t.Apply();

        QRCode.texture = t;
    }

    //返回Color32图片颜色的方法

    Color32[] GeneQRCode(string formatStr,int width,int height) {

        ZXing.QrCode.QrCodeEncodingOptions options = new ZXing.QrCode.QrCodeEncodingOptions();

        options.CharacterSet = "UTF-8";//设置字符编码,否则中文不能显示

        options.Width = width;

        options.Height = width;

        options.Margin = 1;//二维码距离边缘的空白

        barcodeWriter = new BarcodeWriter { Format = ZXing.BarcodeFormat.QR_CODE, Options = options };

        return barcodeWriter.Write(formatStr);
    }
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity UnityWebSocket插件是一款用于在Unity项目中实现WebSocket通信的插件。WebSocket是一种新的网络通信协议,它建立在HTTP协议之上,可以提供全双工通信,使得客户端和服务器可以通过一次HTTP握手建立持久的连接,实现实时的双向通信。 Unity UnityWebSocket插件可以方便地在Unity中使用WebSocket协议进行网络通信。它提供了简洁易用的API接口,开发者可以轻松地实现连接、发送和接收消息等操作。通过该插件,我们可以构建实时的游戏功能,例如聊天系统、多人游戏和实时更新等。 使用Unity UnityWebSocket插件,开发者可以通过几行代码实现WebSocket的连接和消息处理。首先需要创建WebSocket连接,通过指定服务器地址和端口号等参数进行连接。连接建立后,可以通过发送消息来与服务器进行通信,并通过接收消息事件来处理服务器返回的数据。 Unity UnityWebSocket插件还提供了一些高级功能,例如心跳机制和断线重连。心跳机制可以保持连接的稳定性,防止连接断开。断线重连功能可以在网络连接断开后自动重新连接服务器,确保通信的连续性。 总之,Unity UnityWebSocket插件是一款强大的工具,可以帮助开发者在Unity中实现WebSocket通信。它提供了简单易用的接口,并支持一些高级功能,使得开发者可以轻松地构建实时的游戏功能。该插件的使用可以提高开发效率,为游戏开发带来更多可能性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值