码迷,mamicode.com
首页 > 其他好文 > 详细

EasyCodeScanner生成二维码

时间:2016-06-27 10:23:04      阅读:510      评论:0      收藏:0      [点我收藏+]

标签:

using UnityEngine;
using System.Collections;
using ZXing;
using ZXing.QrCode;
using System;
using System.IO;
public class BarcodeCam : MonoBehaviour
{
public Texture2D encoded;//生成的二维码为Texture2D类型
public string Lastresult;//二维码中所包含的内容信息,我是使用了GUID进行代替
public int count = 5;//生成几个二维码
void Start()
{

encoded = new Texture2D(256, 256);
for (int i = 0; i < count; i++)
{
Guid idKey = Guid.NewGuid();
Lastresult = idKey.ToString();
var textForEncoding = Lastresult;
if (textForEncoding != null)
{
var color32 = Encode(textForEncoding, encoded.width, encoded.height);
encoded.SetPixels32(color32);
encoded.Apply();
byte[] bytes = encoded.EncodeToPNG();//把二维码转成byte数组,然后进行输出保存为png图片就可以保存下来生成好的二维码
if (!Directory.Exists(Application.dataPath + "/AdamBieber"))//创建生成目录,如果不存在则创建目录
{
Directory.CreateDirectory(Application.dataPath + "/AdamBieber");
}
string fileName = Application.dataPath + "/AdamBieber/" + idKey + ".png";
System.IO.File.WriteAllBytes(fileName, bytes);
}
}
}


private static Color32[] Encode(string textForEncoding, int width, int height)
{
var writer = new BarcodeWriter
{
Format = BarcodeFormat.QR_CODE,
Options = new QrCodeEncodingOptions
{
Height = height,
Width = width
}
};
return writer.Write(textForEncoding);
}

void OnGUI()
{
GUI.DrawTexture(new Rect(100, 100, 256, 256), encoded);
}

}

EasyCodeScanner生成二维码

标签:

原文地址:http://www.cnblogs.com/ZeroMurder/p/5619229.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!