Unity 拍照并生成图片保存
Posted clhxxlcj
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity 拍照并生成图片保存相关的知识,希望对你有一定的参考价值。
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 拍照并生成图片保存的主要内容,如果未能解决你的问题,请参考以下文章