2017-05-06 136 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拍攝的完整四張圖片,那麼如何比較像素顏色值? –