码迷,mamicode.com
首页 > 编程语言 > 详细

Unity 拍照并生成图片保存

时间:2019-06-04 13:10:01      阅读:286      评论:0      收藏:0      [点我收藏+]

标签:byte   sharp   tst   string   new   width   hmm   system   etop   

 private void CapturePhoto()
    {
        //图片存储的位置
        string rootPath = @"d:\photo-whzq\";
        if (!Directory.Exists(rootPath))
        {
            Directory.CreateDirectory(rootPath);
        }     
        string filename = System.DateTime.Now.ToString("yyyyMMddHHmmssms") + ".png";    
        capturePicPng(rootPath + filename);
        string filefullname = rootPath + filename;

 ;
        PlayerPrefs.SetString(IMAGEFILE, filefullname);


    }
public void capturePicPng(string _filePath)
    {
        FacePhoto.GetComponent<FaceDetection>().capturePicPng(_filePath);
}

 //保存图片
    private IEnumerator getTexture(string _filePath, byte _type)
    {
        yield return new WaitForEndOfFrame();

        //相当于一个画布
        Texture2D t = new Texture2D(1920, 1080, TextureFormat.RGBA32, false);

        //相当于在画布上画画
        //读取屏幕的局部像素信息
        t.ReadPixels(new UnityEngine.Rect(0, 0, Screen.width, Screen.height), 0, 0, true);
        t.Apply();
        if (0 == _type)
        {
            byte[] byt = t.EncodeToJPG();
            //将byte[]全部写入到文件 
            File.WriteAllBytes(_filePath, byt);
        }
        if (1 == _type)
        {
            byte[] byt = t.EncodeToPNG();
            File.WriteAllBytes(_filePath, byt);
        }
        cameraTexture.Play();
    }

    /// 拍照PNG
    /// 
    /// 
    public void capturePicPng(string _filePath)
    {
        ///< 正在录屏,直接返回
        if (/*bIsSeriousCapture ||*/ null == cameraTexture)
        {
            return;
        }
        if (null != cameraTexture && !cameraTexture.isPlaying)
        {
            return;
        }
        cameraTexture.Pause();//停止摄像头

        StartCoroutine(getTexture(_filePath, 1));
    }

  

Unity 拍照并生成图片保存

标签:byte   sharp   tst   string   new   width   hmm   system   etop   

原文地址:https://www.cnblogs.com/clhxxlcj/p/10972615.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!