如何裁剪捕获的图像--C#
是否可以根据我想要的形状裁剪捕获的图像?我正在使用原始图像+网络摄像头纹理来激活相机并保存图像。我使用UI图像覆盖方法作为遮罩来覆盖不需要的部分。我将在后面的部分将图片附加到char模型。对不起,我是unity的新手。谢谢你的帮助 以下是我代码中的内容:如何裁剪捕获的图像--C#,c#,unity3d,camera,unity2d,C#,Unity3d,Camera,Unity2d,是否可以根据我想要的形状裁剪捕获的图像?我正在使用原始图像+网络摄像头纹理来激活相机并保存图像。我使用UI图像覆盖方法作为遮罩来覆盖不需要的部分。我将在后面的部分将图片附加到char模型。对不起,我是unity的新手。谢谢你的帮助 以下是我代码中的内容: // start cam void Start () { devices = WebCamTexture.devices; background = GetComponent<RawImage> ();
// start cam
void Start () {
devices = WebCamTexture.devices;
background = GetComponent<RawImage> ();
devCam = new WebCamTexture ();
background.texture = devCam;
devCam.deviceName = devices [0].name;
devCam.Play ();
}
void OnGUI()
{
GUI.skin = skin;
//swap front and back camera
if (GUI.Button (new Rect ((Screen.width / 2) - 1200, Screen.height - 650, 250, 250),"", GUI.skin.GetStyle("btn1"))) {
devCam.Stop();
devCam.deviceName = (devCam.deviceName == devices[0].name) ? devices[1].name : devices[0].name;
devCam.Play();
}
//snap picture
if (GUI.Button (new Rect ((Screen.width / 2) - 1200, Screen.height - 350, 250, 250), "", GUI.skin.GetStyle ("btn2"))) {
OnSelectCapture ();
//freeze cam here?
}
}
public void OnSelectCapture()
{
imgID++;
string fileName = imgID.ToString () + ".png";
Texture2D snap = new Texture2D (devCam.width, devCam.height);
Color[] c;
c = devCam.GetPixels ();
snap.SetPixels (c);
snap.Apply ();
// Save created Texture2D (snap) into disk as .png
System.IO.File.WriteAllBytes (Application.persistentDataPath +"/"+ fileName, snap.EncodeToPNG ());
}
//启动凸轮
无效开始(){
设备=WebCamTexture.devices;
background=GetComponent();
devCam=新的WebCamTexture();
background.texture=devCam;
devCam.deviceName=设备[0]。名称;
Play();
}
void OnGUI()
{
GUI.skin=皮肤;
//更换前后摄像头
if(GUI.Button(新的Rect((Screen.width/2)-1200,Screen.height-650250250),“”,GUI.skin.GetStyle(“btn1”)){
devCam.Stop();
devCam.deviceName=(devCam.deviceName==设备[0]。名称)?设备[1]。名称:设备[0]。名称;
devCam.Play();
}
//快照
if(GUI.Button(新的Rect((Screen.width/2)-1200,Screen.height-350250250),“”,GUI.skin.GetStyle(“btn2”)){
OnSelectCapture();
//把摄像机停在这里?
}
}
公共void OnSelectCapture()
{
imgID++;
字符串文件名=imgID.ToString()+“.png”;
纹理2d捕捉=新纹理2d(devCam.width,devCam.height);
颜色[]c;
c=devCam.GetPixels();
snap.SetPixels(c);
snap.Apply();
//将创建的Texture2D(快照)保存到磁盘中为.png
System.IO.File.writealBytes(Application.persistentDataPath+“/”+文件名,snap.EncodeToPNG());
}
}除非我没有正确理解你的问题,否则你可以打电话给“devCam.pause!” 更新 你要找的基本上是在某种条件下把屏幕上的像素复制到一个单独的图像上。所以你可以用这样的东西: 我不是100%确定你到底想用它做什么,但如果你想有一个可以用作精灵的图像,例如,你可以扫描每个像素,如果像素的颜色值与蓝色背景相同,就把它换成100%透明的像素(在alpha通道中为0)。那只会给你一张黑发和耳朵的脸 更新2 我提到的链接复制了相机视图中的所有像素,因此您不必担心源图像。这是一种未经测试的方法,只要只有一种背景颜色,它就可以即插即用,否则您需要稍微修改以测试不同的颜色
IEnumerator GetPNG()
{
// Create a texture the size of the screen, RGB24 format
yield return new WaitForEndOfFrame();
int width = Screen.width;
int height = Screen.height;
Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
// Read screen contents into the texture
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
tex.Apply();
//Create second texture to copy the first texture into minus the background colour. RGBA32 needed for Alpha channel
Texture2D CroppedTexture = new Texture2D(tex.width, tex.height, TextureFormat.RGBA32, false);
Color BackGroundCol = Color.white;//This is your background colour/s
//Height of image in pixels
for(int y=0; y<tex.height; y++){
//Width of image in pixels
for(int x=0; x<tex.width; x++){
Color cPixelColour = tex.GetPixel(x,y);
if(cPixelColour != BackGroundCol){
CroppedTexture.SetPixel(x,y, cPixelColour);
}else{
CroppedTexture.SetPixel(x,y, Color.clear);
}
}
}
// Encode your cropped texture into PNG
byte[] bytes = CroppedTexture.EncodeToPNG();
Object.Destroy(CroppedTexture);
Object.Destroy(tex);
// For testing purposes, also write to a file in the project folder
File.WriteAllBytes(Application.dataPath + "/../CroppedImage.png", bytes);
}
IEnumerator GetPNG()
{
//创建屏幕大小的纹理,RGB24格式
返回新的WaitForEndOfFrame();
int width=屏幕宽度;
int height=屏幕高度;
Texture2D tex=新的Texture2D(宽度、高度、TextureFormat.RGB24,false);
//将屏幕内容读入纹理
tex.ReadPixels(新的矩形(0,0,宽度,高度),0,0);
tex.Apply();
//创建第二个纹理,将第一个纹理复制到减去背景色的位置。Alpha通道需要RGBA32
纹理2d裁剪纹理=新纹理2d(tex.width,tex.height,TextureFormat.RGBA32,false);
Color BackGroundCol=Color.white;//这是您的背景色/s
//以像素为单位的图像高度
对于(int y=0;实际上是我!对不起,我已经更新了与该主题相关的问题。我已经更新了我的答案,并说明了如何继续。你应该能够通过一些谷歌搜索找到如何完成具体操作的现有示例,但如果你陷入困境,我会看看是否可以为你编写一个起点。但我的设备存储中有完整的四幅图片在没有蓝色背景的情况下使用webcamtexture拍摄的照片,那么如何比较像素颜色值呢?此处查看:()谢谢你的代码。它可以工作,但问题是,我最初设置的颜色在设备上执行时找不到,可能是由于设备分辨率,因此颜色不匹配并被替换。此外,我的背景上有许多颜色(包括渐变色),所以我如何适应这方面?