一.工具
ZXing.Net
找到ZXing.Net/Clients/UnityDemo/Assets/zxing.unity.dll導(dǎo)入到Unity中(一般這種插件放到Plugins目錄下)
二.生成二維碼
直接上代碼,三種類型
1.固定尺寸(256x256)
/// <summary>
/// 生成固定大小的二維碼(256,256)
/// </summary>
/// <param name="content"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static Texture2D GenerateQRImageConstantSize(string content, int width, int height)
{
// 編碼成color32
EncodingOptions options = null;
BarcodeWriter writer = new BarcodeWriter();
options = new EncodingOptions
{
Width = width,
Height = height,
Margin = 1,
};
options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
writer.Format = BarcodeFormat.QR_CODE;
writer.Options = options;
Color32[] colors = writer.Write(content);
// 轉(zhuǎn)成texture2d
Texture2D texture = new Texture2D(width, height);
texture.SetPixels32(colors);
texture.Apply();
return texture;
}
2.任意正方形
/// <summary>
/// 任意正方形
/// </summary>
/// <param name="content"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static Texture2D GenerateQRImageFreeSize(string content, int width, int height)
{
// 編碼成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.M);
BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
// 轉(zhuǎn)成texture2d
int w = bitMatrix.Width;
int h = bitMatrix.Height;
print(string.Format("w={0},h={1}", w, h));
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.black);
}
else
{
texture.SetPixel(y, x, Color.white);
}
}
}
texture.Apply();
return texture;
}
3.帶有小圖標(biāo)
/// <summary>
/// 生成中心帶有小圖標(biāo)二維碼
/// </summary>
/// <param name="content"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="centerIcon"></param>
/// <returns></returns>
public static Texture2D GenerateQRImageWithIcon(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);
// 轉(zhuǎn)成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.black);
}
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();
return texture;
}
三.融合
/// <summary>
/// 融合圖片和二維碼,得到新圖片
/// </summary>
/// <param name="tex_base">底圖</param>
/// <param name="tex_code">二維碼</param>
public static Texture2D MixImagAndQRCode(Texture2D tex_base, Texture2D tex_code)
{
Texture2D newTexture = Instantiate(tex_base) as Texture2D; ;
Vector2 uv = new Vector2((tex_base.width - tex_code.width) / tex_base.width, (tex_base.height - tex_code.height) / tex_base.height);
for (int i = 0; i < tex_code.width; i++)
{
for (int j = 0; j < tex_code.height; j++)
{
float w = uv.x * tex_base.width - tex_code.width + i;
float h = uv.y * tex_base.height - tex_code.height + j;
//從底圖圖片中獲取到(w,h)位置的像素
Color baseColor = newTexture.GetPixel((int)w, (int)h);
//從二維碼圖片中獲取到(i,j)位置的像素
Color codeColor = tex_code.GetPixel(i, j);
//融合
newTexture.SetPixel((int)w, (int)h, codeColor);
}
}
newTexture.Apply();
return newTexture;
}
存儲(chǔ)到本地
//fileNameUrl例:(UnityEngine.Application.persistentDataPath+"/GameData/Textures/QRCode.png")
public static void SaveTextureCodeToPng(string fileNameUrl, Texture2D tex)
{
byte[] bytes = tex.EncodeToJPG();
string folderUrl = Path.GetDirectoryName(fileNameUrl);
if (DirectoryExists(folderUrl))
{
CreateDirectory(folderUrl);
}
File.WriteAllBytes(fileNameUrl, bytes);
}