unity里怎么使用zxing

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了unity里怎么使用zxing相关的知识,希望对你有一定的参考价值。

参考技术A Deploying the project to a S60 3rd edition requires one more step (executed only once).After the installation of OpenC/C++ plug-in, the installation files contained in the following foldershave to be installed to the device:S60 SDK root folder #92; nokia_plugin #92; openc #92; s60opencsisandS60 SDK root folder #92; nokia_plugin #92; opencpp #92; s60opencppsisIf this step is not taken, the application will be able to be installed to the device but it WILL NOT RUN!

Unity插件系列之二维码

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);
    }
}

 

更多:

ZXing 二维码生成的坑 http://blog.sina.com.cn/s/blog_6ad33d350102xj8l.html

zxing.net读取和保存二维码,设置中文字符集,读取中文二维码  https://bbs.csdn.net/topics/391950715?page=1

 

以上是关于unity里怎么使用zxing的主要内容,如果未能解决你的问题,请参考以下文章

Unity插件系列之二维码

zxing.unity.dll下载之后的位置找到UnityDemo/Assets下

Unity中Zxing生成二维码只能生成256大小图片的解决方案

unity里C#脚本的velocity怎么用

unity怎么在game视图里画线

话说Unity里一般怎么创建billboard plane