【问题标题】:How to save a png using Unity WebCamTexture如何使用 Unity WebCamTexture 保存 png
【发布时间】:2020-02-15 16:41:49
【问题描述】:

我想使用设备本机相机保存图片。目前我无法将图像保存到文件中。我有一个原始图像,其纹理是本机设备相机图像。我正在从该 rawimage 中获取字节并编码为 png。然后我将 png 写入我的计算机上的文件。

    public WebCamTexture webCamTexture;
    public RawImage myImage;

    public void start () {
        webCamTexture = new WebCamTexture ();
        myImage.texture = webCamTexture;
        myImage.transform.localScale = new Vector3 (1,-1,1);
        webCamTexture.Play ();

        int width =  (int)GameObject.Find("myImage").GetComponent<Rect>      ().width;
        int height = (int)GameObject.Find("myImage").GetComponent<Rect>().height;
        Texture2D tex = new Texture2D (width, height, TextureFormat.RGB24, false);

        tex.ReadPixels (new Rect (0, 0, width, height), 0, 0);
        tex.Apply ();

        byte[] bytes = tex.EncodeToPNG ();
        System.IO.File.WriteAllBytes(Application.dataPath + "/"+"imgcap.png",bytes);

        Object.Destroy (tex);
    }

【问题讨论】:

    标签: c# unity5


    【解决方案1】:
    using System;
    using System.IO;
    using System.Runtime.InteropServices;
    using UnityEngine;
    
    public class WebCamScreenShot : MonoBehaviour
    {
        public static int fileCounter = 0;
    
        WebCamTexture webCamTexture;
    
        public string path = "C:/temp/UnityScreenShots";
    
        public string fileNamePrefix = "image_";
    
        void Start()
        {
            var devices = WebCamTexture.devices;
    
            if (devices.Length < 1) throw new System.Exception("No webcams was found");
    
            var device = devices[0];
    
            webCamTexture = new WebCamTexture(device.name);
            webCamTexture.requestedFPS = 30;
            webCamTexture.requestedWidth = 320;
            webCamTexture.requestedHeight = 240;
            webCamTexture.Play();
    
            if (webCamTexture.width < 1 || webCamTexture.height < 1) throw new System.Exception("Invalid resolution");
        }
    
        public void SaveToPNG()
        {
            string zeros =
                (fileCounter < 10000 ? "0000" :
                    (fileCounter < 1000 ? "000" :
                        (fileCounter < 100 ? "00" :
                            (fileCounter < 10 ? "0" : ""))));
    
            string image_path = path + $"/{ fileNamePrefix + zeros + fileCounter }.png";
    
            byte[] data = ScreenshotWebcam( webCamTexture );
    
            File.WriteAllBytes(image_path, data);
    
            fileCounter ++ ;
        }
        static byte[] ScreenshotWebcam(WebCamTexture wct)
        {
            Texture2D colorTex = new Texture2D(wct.width, wct.height, TextureFormat.RGBA32, false);
    
            byte[] colorByteData = Color32ArrayToByteArray(wct.GetPixels32());
    
            colorTex.LoadRawTextureData(colorByteData);
            colorTex.Apply();
    
            return colorTex.EncodeToPNG();
        }
    
        static byte[] Color32ArrayToByteArray(Color32[] colors)
        {
            // https://stackoverflow.com/a/21575147/2496170
    
            if (colors == null || colors.Length == 0) return null;
    
            int lengthOfColor32 = Marshal.SizeOf(typeof(Color32));
    
            int length = lengthOfColor32 * colors.Length;
    
            byte[] bytes = new byte[length];
    
            GCHandle handle = default(GCHandle);
    
            try
            {
                handle = GCHandle.Alloc(colors, GCHandleType.Pinned);
                IntPtr ptr = handle.AddrOfPinnedObject();
                Marshal.Copy(ptr, bytes, 0, length);
            }
            finally
            {
                if (handle != default(GCHandle)) handle.Free();
            }
    
            return bytes;
        }
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-27
      • 1970-01-01
      • 2021-09-20
      • 2011-09-24
      相关资源
      最近更新 更多