ZXing for Net
记录学习内容
生成的代码部分
注意事项
- 这里需要注意使用BarcodeWriter的Write的方法只能设置大小为256x256
///生成二维码的方法,使用unity开发的
public void Generate(string info)
{
BarcodeWriter bw = new BarcodeWriter();
bw.Format = BarcodeFormat.QR_CODE;
//需要引入QrCode命名空间
QrCodeEncodingOptions options = new QrCodeEncodingOptions
{
//设置optinos
DisableECI = true,
//这里需要注意使用BarcodeWriter的Write的方法只能设置大小为256x256
Height = 256,
Width= 256,
Margin=2,
CharacterSet="UTF-8",
};
bw.Options = options;
Color32[] code= bw.Write(info);
Texture2D texture2D = new Texture2D(256,256);
texture2D.SetPixels32(code);
texture2D.Apply();
codeImage.texture = texture2D;
}
/// <summary>
/// 生成条形码
/// </summary>
/// <param name="info"></param>
public void Generate2(string info)
{
QrCodeEncodingOptions myOptions = new QrCodeEncodingOptions
{
Width = 256,
Height= 128,
Margin=2
};
BarcodeWriter bw = new BarcodeWriter
{
Format = BarcodeFormat.CODE_128,//关键部分
Options = myOptions
};
//
Color32[] code = bw.Write(info);
Texture2D texture = new Texture2D(256,128);
texture.SetPixels32(code);
texture.Apply();
codeImage.texture = texture;
}
void Start () {
WebCamDevice[] devices = WebCamTexture.devices;//获取设备相机集合
string webCamName = devices[0].name;//
webCam = new WebCamTexture(webCamName, 400,300);//相机的Texture
rawImage.texture = webCam;//将texture显示在界面。
//开始获取
webCam.Play();
}
/// <summary>
/// 将拍摄的二维码解码 获取其中的信息
/// </summary>
public void Decode()
{
BarcodeReader br = new BarcodeReader();
Color32[] data = webCam.GetPixels32();
Result result= br.Decode(data, 400, 300);
Debug.Log(result.Text);
}