unity调用摄像头 截屏并且图片保存本地

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using UnityEngine;
using UnityEngine.UI;
public class Photo : MonoBehaviour {
    public string deviceName;
    //接收返回的图片数据
    WebCamTexture tex;
     
    public RawImage rawImage;
    public static string filename;
    void OnGUI()
    {
        if (GUI.Button(new Rect(10, 20, 100, 40), "start"))
        {
            // 调用摄像头
            StartCoroutine(start());
        }

        if (GUI.Button(new Rect(10, 70, 100, 40), "Pause"))
        {
            //捕获照片
            tex.Pause();
            StartCoroutine(getTexture());
        }

        if (GUI.Button(new Rect(10, 120, 100, 40), "replay"))
        {
            //重新开始
            tex.Play();
        }

        if (GUI.Button(new Rect(120, 20, 80, 40), "record"))
        {
            //录像
            StartCoroutine(SeriousPhotoes());
        }

        if (GUI.Button(new Rect(10, 170, 100, 40), "stop"))
        {
            //停止捕获镜头
            tex.Stop();
            StopAllCoroutines();
        }

        if (tex != null)
        {
            // 捕获截图大小   —距X左屏距离   |   距Y上屏距离 
            //GUI.DrawTexture(new Rect(Screen.width / 2 - 150, Screen.height / 2 - 190, 280, 200), tex);
        }

    }

    private void Update()
    {
        rawImage.texture = tex;
    }
    private void Start()
    {
        StartCoroutine(start());
    }
    /// 
    /// 捕获窗口位置
    /// 
    public IEnumerator start()
    {
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
        if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            WebCamDevice[] devices = WebCamTexture.devices;
            deviceName = devices[0].name;
            tex = new WebCamTexture(deviceName, 1980/2, 1020/2, 20);
            tex.Play(); 
        }
    }

    /// 
    /// 获取截图
    /// 
    /// The texture.
    public IEnumerator getTexture()
    {
        yield return new WaitForEndOfFrame();
        yield return new WaitForEndOfFrame(); 
        Texture2D screenShot = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false); 
        screenShot.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0, true); 
        yield return screenShot; 
        byte[] bytes = screenShot.EncodeToJPG();
        screenShot.Compress(false);
        //获取系统时间  
        System.DateTime now = new System.DateTime();
        now = System.DateTime.Now;
        filename = string.Format("IMG_{0}{1}{2}_{3}{4}{5}.jpg", now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second);
        Debug.Log(filename);
        if (!Directory.Exists("C:/Photos"))
        {
            Directory.CreateDirectory("C:/Photos");
        }
        File.WriteAllBytes("C:/Photos/" + filename, bytes);
        print("成功拍了一张照片 查看E/Photos文件夹下");

        //  System.Diagnostics.Process.Start("mspaint.exe", "/pt E:\\Photos\\" + filename);//调用画图工具

        tex.Play();
    } 
    /// 
    /// 连续捕获照片
    /// 
    /// The photoes.
    public IEnumerator SeriousPhotoes()
    {
        while (true)
        {
            yield return new WaitForEndOfFrame();
            Texture2D t = new Texture2D(400, 300, TextureFormat.RGB24, true);
            t.ReadPixels(new Rect(Screen.width / 2 - 180, Screen.height / 2 - 50, 360, 300), 0, 0, false);
            t.Apply();
            print(t);
            byte[] byt = t.EncodeToPNG();
            File.WriteAllBytes(Application.dataPath + "/MulPhotoes/" + Time.time.ToString().Split('.')[0] + "_" + Time.time.ToString().Split('.')[1] + ".png", byt);
            Thread.Sleep(300);
        }
    }

}

 

你可能感兴趣的:(Unity,3D,C#)