Unity生成二维码,融合到图片中

一.工具

ZXing.Net
找到ZXing.Net/Clients/UnityDemo/Assets/zxing.unity.dll导入到Unity中(一般这种插件放到Plugins目录下)

二.生成二维码

直接上代码,三种类型
1.固定尺寸(256x256)

    /// 
    /// 生成固定大小的二维码(256,256)
    /// 
    /// 
    /// 
    /// 
    /// 
    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;
    }

2.任意正方形

    /// 
    /// 任意正方形
    /// 
    /// 
    /// 
    /// 
    /// 
    public static Texture2D GenerateQRImageFreeSize(string content, int width, int height)
    {
        // 编码成color32
        MultiFormatWriter writer = new MultiFormatWriter();
        Dictionary hints = new Dictionary();
        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);
        // 转成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.带有小图标

/// 
/// 生成中心带有小图标二维码
/// 
/// 
 /// 
 /// 
 /// 
/// 
public static Texture2D GenerateQRImageWithIcon(string content, int width, int height, Texture2D centerIcon)
    {
        // 编码成color32
        MultiFormatWriter writer = new MultiFormatWriter();
        Dictionary hints = new Dictionary();
        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.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;
    }
三.融合
/// 
    /// 融合图片和二维码,得到新图片 
    /// 
    /// 底图
    /// 二维码
    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;
    }

存储到本地

//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);
    }

你可能感兴趣的:(Unity生成二维码,融合到图片中)