1

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

在此处输入图像描述

以下是我的代码中的内容:

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

}

4

1 回答 1

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);
}
于 2017-05-06T18:12:03.083 回答