Unity 生成二维码和中间带图标的二维码、包括扫,识别二维码(Android 和 Windows亲测可用)
demo下载地址
https://download.csdn.net/download/qq_39735878/20328497
1,生成二维码(带图标)
/// <summary>
/// 生成普通二维码
/// </summary>
public void Btn_CreatQr()
{
if (QrCodeStr[Nmuber].Length > 1)
{
//二维码写入图片
var color32 = Encode(QrCodeStr[Nmuber], encoded.width, encoded.height);
encoded.SetPixels32(color32);
encoded.Apply();
//生成的二维码图片附给RawImage
image.texture = encoded;
}
else
{
GameObject.Find("Text_1").GetComponent<Text>().text = "没有生成信息";
}
}
/// <summary>
/// 定义方法生成二维码
/// </summary>
/// <param name="textForEncoding">需要生产二维码的字符串</param>
/// <param name="width">宽</param>
/// <param name="height">高</param>
/// <returns></returns>
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);
}
/// <summary>
/// 生成2维码,添加小图标
/// </summary>
/// <param name="content"></param>
/// <param name="width"></param>
/// <param name="height"></param>
void GenerateQRImage3(string content, int width, int height, Texture2D centerIcon)
{
// 编码成color32
MultiFormatWriter writer = new MultiFormatWriter();
Dictionary<EncodeHintType, object> hints = new Dictionary<EncodeHintType, object>();
hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.Add(EncodeHintType.MARGIN, 1);
hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H);
BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
// 转成texture2d
int w = bitMatrix.Width;
int h = bitMatrix.Height;
Texture2D texture = new Texture2D(w, h);
for (int x = 0; x < h; x++)
{
for (int y = 0; y < w; y++)
{
if (bitMatrix[x, y])
{
texture.SetPixel(y, x, Color.blue);
}
else
{
texture.SetPixel(y, x, Color.white);
}
}
}
// 添加小图
int halfWidth = texture.width / 2;
int halfHeight = texture.height / 2;
int halfWidthOfIcon = centerIcon.width / 2;
int halfHeightOfIcon = centerIcon.height / 2;
int centerOffsetX = 0;
int centerOffsetY = 0;
for (int x = 0; x < h; x++)
{
for (int y = 0; y < w; y++)
{
centerOffsetX = x - halfWidth;
centerOffsetY = y - halfHeight;
if (Mathf.Abs(centerOffsetX) <= halfWidthOfIcon && Mathf.Abs(centerOffsetY) <= halfHeightOfIcon)
{
texture.SetPixel(x, y, centerIcon.GetPixel(centerOffsetX + halfWidthOfIcon, centerOffsetY + halfHeightOfIcon));
}
}
}
texture.Apply();
image.texture = texture;
// 存储成文件
byte[] bytes = texture.EncodeToPNG();
string path = System.IO.Path.Combine(Application.dataPath, "qr.png");
System.IO.File.WriteAllBytes(path, bytes);
}
2,扫描,识别二维码
/// <summary>
/// 识别摄像机图片中的二维码信息
/// 打印二维码识别到的信息
/// </summary>
void ScanQRCode()
{
//7、获取摄像机画面的像素颜色数组信息
data = webCamTexture.GetPixels32();
//8、获取图片中的二维码信息
Result result = barcodeReader.Decode(data, webCamTexture.width, webCamTexture.height);
//如果获取到二维码信息了,打印出来
if (result != null)
{
Debug.Log(result.Text);//二维码识别出来的信息
text.text = result.Text;//显示扫描信息
//扫描成功之后的处理
IsScanning = false;
webCamTexture.Stop();
}
}
/// <summary>
/// 开启摄像机和准备工作
/// </summary>
void DeviceInit()
{
//1、获取所有摄像机硬件
WebCamDevice[] devices = WebCamTexture.devices;
//2、获取第一个摄像机硬件的名称
string deviceName = devices[0].name;//手机后置摄像机
//3、创建实例化一个摄像机显示区域
webCamTexture = new WebCamTexture(deviceName, 400, 300);
//4、显示的图片信息
cameraTexture.texture = webCamTexture;
//5、打开摄像机运行识别
webCamTexture.Play();
//6、实例化识别二维码信息存储对象
barcodeReader = new BarcodeReader();
}
3,项目下载地址 https://download.csdn.net/download/qq_39735878/20328497