gpt4 book ai didi

c# - 屏幕截图后图像上的 Unity 水印

转载 作者:行者123 更新时间:2023-12-05 03:02:39 28 4
gpt4 key购买 nike

我想在我的图片上添加水印,这是我用来截图的代码。有人可以教我如何在图像中添加水印吗?我想在图片的右上角有一个小 Logo 。

我正在尝试研究是否可以实现我在 Canvas 中保留的内容,以便在截取屏幕截图时保留(现实生活)。但没有运气。如果有人能在这里帮助我,我将不胜感激!

public string MakePhoto(bool openIt)
{
int resWidth = Screen.width;
int resHeight = Screen.height;

Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false); //Create new texture
RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);

// hide the info-text, if any
if (infoText)
{
infoText.text = string.Empty;
}
// render background and foreground cameras
if (backroundCamera && backroundCamera.enabled)
{
backroundCamera.targetTexture = rt;
backroundCamera.Render();
backroundCamera.targetTexture = null;
}

if (backroundCamera2 && backroundCamera2.enabled)
{
backroundCamera2.targetTexture = rt;
backroundCamera2.Render();
backroundCamera2.targetTexture = null;
}

if (foreroundCamera && foreroundCamera.enabled)
{
foreroundCamera.targetTexture = rt;
foreroundCamera.Render();
foreroundCamera.targetTexture = null;
}

// get the screenshot
RenderTexture prevActiveTex = RenderTexture.active;
RenderTexture.active = rt;

screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);

// clean-up
RenderTexture.active = prevActiveTex;
Destroy(rt);

byte[] btScreenShot = screenShot.EncodeToJPG();
Destroy(screenShot);

#if !UNITY_WSA
// save the screenshot as jpeg file
string sDirName = Application.persistentDataPath + "/Screenshots";
if (!Directory.Exists(sDirName))
Directory.CreateDirectory (sDirName);

string sFileName = sDirName + "/" + string.Format ("{0:F0}", Time.realtimeSinceStartup * 10f) + ".jpg";
File.WriteAllBytes(sFileName, btScreenShot);

Debug.Log("Photo saved to: " + sFileName);
if (infoText)
{
infoText.text = "Saved to: " + sFileName;
}

// open file
if(openIt)
{
System.Diagnostics.Process.Start(sFileName);
}

return sFileName;

PS:我发现这个可能有用吗?

public Texture2D AddWatermark(Texture2D background, Texture2D watermark)
{

int startX = 0;
int startY = background.height - watermark.height;

for (int x = startX; x < background.width; x++)
{

for (int y = startY; y < background.height; y++)
{
Color bgColor = background.GetPixel(x, y);
Color wmColor = watermark.GetPixel(x - startX, y - startY);

Color final_color = Color.Lerp(bgColor, wmColor, wmColor.a / 1.0f);

background.SetPixel(x, y, final_color);
}
}

background.Apply();
return background;
}

最佳答案

  1. ProjectsView 中选择导入的图像,并在检查器中将 TextureType 设置为 Sprite(2D 和 UI)(参见 Sprites Manual ) 并点击应用

  2. 为它添加一个 Sprite 字段到您的类中

    public Texture2D watermark;
  3. 在检查器中引用水印

  4. 您可以通过为每个像素添加两个纹理的 Color 值来简单地将水印添加为叠加层(这里假设它们具有相同的大小!)

    如果您只想在纹理的某个矩形中添加水印,则必须相应地缩放它并使用 Texture2D.SetPixels(int x, int y, int blockWidth, int blockHeight, Color[] colors)(这假设水印图像的像素小于屏幕截图!),例如

    private static void AddWaterMark(Texture2D texture, Texture2D watermarkTexture)
    {
    int watermarkWidth = watermarkTexture.width;
    int watermarkHeight = watermarkTexture.height;

    // In Unity differrent to most expectations the pixel corrdinate
    // 0,0 is not the top-left corner but instead the bottom-left
    // so since you want the whatermark in the top-right corner do
    int startx = texture.width - watermarkWidth;
    // optionally you could also still leave a border of e.g. 10 pixels by using
    // int startx = texture.width - watermarkWidth - 10;

    // same for the y axis
    int starty = texture.height - watermarkHeight;

    Color[] watermarkPixels = watermarkTexture.GetPixels();
    // get the texture pixels for the given rect
    Color[] originalPixels = texture.GetPixels(startx, starty, watermarkWidth, watermarkHeight);

    for(int i = 0; i < watermarkPixels.Length; i++)
    {
    var pixel = watermarkPixels[i];
    // adjust the alpha value of the whatermark
    pixel.a *= 0.5f;
    // add watermark pixel to original pixel
    originalPixels[i] += pixel;
    }

    // write back the changed texture data
    texture.SetPixels(startx, starty, watermarkWidth, watermarkHeight, originalPixels);
    texture.Apply();
    }

    这样称呼

    screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
    AddWaterMark(screenShot, watermark);

关于c# - 屏幕截图后图像上的 Unity 水印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54629898/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com