我正在尝试在图像上添加水印,这是我用于截屏的代码。有人可以教我如何在图像中实现水印吗?我想要在图片的右上角有一个小徽标。
我正在尝试研究是否可以实现拍摄快照时在画布上保留的内容(真实生活)。但是,没有运气。如果有人可以在这里帮助我,我将不胜感激!
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;
}
答案 0 :(得分:1)
在ProjectsView
中选择导入的图像,然后在检查器中将TextureType
设置为Sprite (2D and UI)
(请参阅Sprites Manual),然后点击 Apply
为您的班级添加一个Sprite
字段,例如
public Sprite whatermark;
在检查器中引用精灵
您可以通过在两个纹理中为每个像素添加Color
值来简单地将whatermark添加为叠加层(假定此处的大小相同!)
如果仅在纹理的衬色区域中要使用标记,则必须对其进行相应缩放并使用Texture2D.SetPixels(int x, int y, int blockWidth, int blockHeight, Color[] colors)(这是假设标记图像的像素小于屏幕截图!)喜欢
private static void AddWhaterMark(Texture2D texture, Texture2D whatermarkTexture)
{
int whatermarkWidth = whatermarkTexture.width;
int whatermarkHeight = whatermarkTexture.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 - whatermarkWidth;
// optionally you could also still leave a border of e.g. 10 pixels by using
// int startx = texture.width - whatermarkWidth - 10;
// same for the y axis
int starty = texture.height - whatermarkHeight;
Color[] whatermarkPixels = whatermarkTexture.GetPixels();
// get the texture pixels for the given rect
Color[] originalPixels = texture.GetPixels(startx, starty, whatermarkWidth, whatermarkHeight);
for(int i = 0; i < whatermarkPixels.Length; i++)
{
var pixel = whatermarkPixels[i];
// adjust the alpha value of the whatermark
pixel.a *= 0.5f;
// add whatermark pixel to original pixel
originalPixels[i] += pixel;
}
// write back the changed texture data
texture.SetPixels(startx, starty, whatermarkWidth, whatermarkHeight, originalPixels);
texture.Apply();
}
称呼它
screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
AddWhaterMark(screenShot, whatermark.texture);