当前位置:   article > 正文

Unity 生成二维码 & 融合图片_融合图片生成

融合图片生成

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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146

效果图

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/98050
推荐阅读
相关标签
  

闽ICP备14008679号