2017-05-06 131 views
0

是否可以根据所需的形状裁剪捕获的图像?我使用原始图像+网络摄像头纹理来激活相机并保存图像。并且我使用UI图像叠加方法作为掩码来覆盖不需要的部分。我将在后半部分将图片附加到char模型中。对不起,我是新来的团结。感谢您的帮助!如何裁剪捕获的图像? --C#

enter image description here

下面是我在我的代码:

// 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()); 
} 

}

回答

1

除非我不理解正确你的问题,你可以叫`devCam.pause!

更新

什么你要找的是基本的像素从画面一定条件下复制到一个单独的图像。所以你可以使用这样的东西:https://docs.unity3d.com/ScriptReference/Texture2D.EncodeToPNG.html

我不是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); 
} 
+0

傻我!对不起,我已经更新了与该主题相关的问题。 –

+0

我已经更新了我的答案,并提供了有关如何继续的说明。你应该能够找到现有的一些例子,说明如何使用一些谷歌搜索的具体细节,但如果你卡住了,我会看看我是否可以编码起点。 –

+0

但我在我的设备存储器中是使用没有蓝色背景的webcamtexture拍摄的完整四张图片,那么如何比较像素颜色值? –