屏幕快照后图像上的Unity水印 [英] Unity watermark on image after screenshot

查看:133
本文介绍了屏幕快照后图像上的Unity水印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在图像上添加水印,这是我用于截屏的代码.有人可以教我如何在图像中实现水印吗?我想要在图像的右上角有一个小徽标.

I am trying to add a watermark on my image, and this is the code I have for taking a screenshot. Can someone teach me how to implement watermark into my image? I want a small logo at the top right hand side of the image.

我正在尝试研究是否可以实现拍摄快照时我在画布上留下的东西(真实生活).但是,没有运气.如果有人可以在这里帮助我,我将不胜感激!

I am trying to research on maybe if I could implement what I have in the canvas to stay when a screenshot is taken ( real life ). But to no luck. Would really appreciate if someone could help me out here !

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:我发现这可能有用吗?

PS: I found this which might be useful?

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 and UI)(请参见精灵手册),然后点击应用

  1. Select the imported image in the ProjectsView and in the inspector set TextureType to Sprite (2D and UI) (see Sprites Manual) and hit Apply

为您的班级添加一个Sprite字段,例如

add a Sprite field for it to your class like

public Texture2D watermark;

  • 在检查器中引用watermark

    您只需在两个纹理的每个像素中添加Color值即可简单地将水印添加为叠加层(假设此处的大小相同!)

    You could simply add the watermark as overlay by adding the Color values from both textures for each pixel (assuming here they have the same size!)

    如果仅在纹理的特定区域中需要水印,则必须相应地缩放水印并使用 Texture2D.SetPixels(int x,int y,int blockWidth,int blockHeight,Color []颜色)(这假定水印图像的像素小于屏幕拍摄的像素!)喜欢

    If you want a watermark only in a certain rect of the texture you either have to scale it accordingly and use Texture2D.SetPixels(int x, int y, int blockWidth, int blockHeight, Color[] colors) (This assumes the watermark image is smaller in pixels than the screenShot!) like

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

  • 这篇关于屏幕快照后图像上的Unity水印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆