小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。
Unity 生成二维码,并将二维码和已有背景图片融合,保存到本地。
1、场景的搭建
2、代码的编写(参考链接)
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using ZXing;
using ZXing.Common;
public class CreateCode_2 : MonoBehaviour
{
//生成二维码指定RawImage
public RawImage Raw_QRCode;
//要和二维码融合的背景图
public RawImage Raw_Bg;
//和融合后的结果图 (保存到本地的图)
public RawImage Raw_Result;
void Start()
{
//生成二维码
Raw_QRCode.texture = GenerateQRImageConstantSize("https://blog.csdn.net/czhenya", 256, 256);
//融合
Raw_Result.texture = MixImagAndQRCode(TextureToTexture2D(Raw_Bg.texture), GenerateQRImageConstantSize("http://www.baidu.com", 256, 256));
//保存知道本地 ... 在本地的路径 C:\Users\Administrator\AppData\LocalLow\DefaultCompany\DefaultCompany
//Application.persistentDataPath + "/ShotShare.png" 直接设置此路径为分享图片路径
Debug.Log(saveMainTextureToPng((Application.persistentDataPath + "/ShotShare.png"), Raw_Result.texture));
}
/// <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);
// 转成texture2d
Texture2D texture = new Texture2D(width, height);
texture.SetPixels32(colors);
texture.Apply();
return texture;
}
/// <summary>
/// 融合图片和二维码,得到新Texture2D ...
/// 根据背景图的大小比例
/// </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;
}
/// <summary>
/// Texture转换成Texture2D...
/// </summary>
/// <param name="texture"></param>
/// <returns></returns>
Texture2D TextureToTexture2D(Texture texture)
{
Texture2D texture2D = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false);
RenderTexture currentRT = RenderTexture.active;
RenderTexture renderTexture = RenderTexture.GetTemporary(texture.width, texture.height, 32);
Graphics.Blit(texture, renderTexture);
RenderTexture.active = renderTexture;
texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
texture2D.Apply();
RenderTexture.active = currentRT;
RenderTexture.ReleaseTemporary(renderTexture);
return texture2D;
}
/// <summary>
/// 将Texture转为本地PNG ...
/// </summary>
/// <param name="filePath">保存路径,(分享时直接使用)</param>
/// <param name="teture">要保存的Texture</param>
/// <returns></returns>
public static bool saveMainTextureToPng(string filePath, Texture teture)
{
if (teture.GetType() != typeof(Texture2D))
{
return false;
}
Texture2D savedTexture = (Texture2D)teture;
try
{
Texture2D newTexture = new Texture2D(savedTexture.width, savedTexture.height, TextureFormat.RGBA32, false);
newTexture.SetPixels(0, 0, savedTexture.width, savedTexture.height, savedTexture.GetPixels());
newTexture.Apply();
byte[] bytes = newTexture.EncodeToPNG();
if (bytes != null && bytes.Length > 0)
{
if (File.Exists(filePath))
{
File.Delete(filePath);
}
System.IO.File.WriteAllBytes(filePath, bytes);
}
}
catch (IOException ex)
{
return false;
}
return true;
}
}