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
