在截图后图像统一水印

问题描述 投票:1回答:1

我想我的图像上添加水印,这是我的代码有采取截图。谁能教我如何实现水印到我的形象?我想在图像的顶部右侧的小图标。

我想,也许在研究,如果我能实现我在画布上停留时的截图取(现实生活)。但没有运气。会很感激,如果有人可以帮助我在这里!

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;
}
c# unity3d
1个回答
2
投票
  1. ProjectsView和检查员选择导入的图像设置TextureTypeSprite (2D and UI)(见Sprites Manual)并且点击应用
  2. 为它添加一个Sprite场到您的类象 public Texture2D whatermark;
  3. 引用watermark在检查
  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);
© www.soinside.com 2019 - 2024. All rights reserved.