日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

Unity将相机内容输出成图片

發(fā)布時間:2023/12/16 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Unity将相机内容输出成图片 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Unity自帶Recorder

之所以要講Recorder,是因為Recorder的錄像功能實際上就是把屏幕圖像保存成了一幀幀的圖片,而實現(xiàn)的功能,所以我們先看下它的源碼。

public override void RecordFrame(RecordingSession session){if (m_Inputs.Count != 1)throw new Exception("Unsupported number of sources");Texture2D tex = null;if (m_Inputs[0] is GameViewInput)//游戲視窗輸出的圖像{tex = ((GameViewInput)m_Inputs[0]).image;if (m_Settings.outputFormat == ImageRecorderOutputFormat.EXR){var textx = new Texture2D(tex.width, tex.height, TextureFormat.RGBAFloat, false);textx.SetPixels(tex.GetPixels());tex = textx;}else if (m_Settings.outputFormat == ImageRecorderOutputFormat.PNG){var textx = new Texture2D(tex.width, tex.height, TextureFormat.RGB24, false);textx.SetPixels(tex.GetPixels());//直接復(fù)制像素信息tex = textx;}}else{var input = (BaseRenderTextureInput)m_Inputs[0];//普通渲染貼圖var width = input.outputRT.width;var height = input.outputRT.height;tex = new Texture2D(width, height, m_Settings.outputFormat != ImageRecorderOutputFormat.EXR ? TextureFormat.RGBA32 : TextureFormat.RGBAFloat, false);var backupActive = RenderTexture.active;RenderTexture.active = input.outputRT;//將貼圖信息輸入到緩存中tex.ReadPixels(new Rect(0, 0, width, height), 0, 0, false);//讀取緩存的像素tex.Apply();//保存應(yīng)用RenderTexture.active = backupActive;}byte[] bytes;switch (m_Settings.outputFormat)//輸出格式{case ImageRecorderOutputFormat.PNG:bytes = tex.EncodeToPNG();break;case ImageRecorderOutputFormat.JPEG:bytes = tex.EncodeToJPG();break;case ImageRecorderOutputFormat.EXR:bytes = tex.EncodeToEXR();break;default:throw new ArgumentOutOfRangeException();}if(m_Inputs[0] is BaseRenderTextureInput || m_Settings.outputFormat != ImageRecorderOutputFormat.JPEG)UnityHelpers.Destroy(tex);var path = m_Settings.fileNameGenerator.BuildAbsolutePath(session);File.WriteAllBytes( path, bytes);//保存到本地文件}

仿照進(jìn)行功能實現(xiàn)

1.創(chuàng)建一個相機,將拍攝的圖片保存到RenderTexture上


2.代碼實現(xiàn)

namespace App.System {/// <summary>/// 相機截圖/// </summary>public class CamUI : MonoBehaviour{public Button takeBtn;public RawImage imgCam;public void Start(){takeBtn.onClick.AddListener(OnTakePicture);}#region 事件private void OnTakePicture(){var dicPath = Application.streamingAssetsPath;string path1 = dicPath + $"/VPImg/picture1.png";SaveTexture(path1, (RenderTexture) imgCam.texture);}private void SaveTexture(string path, RenderTexture renderTexture){var width = renderTexture.width;var height = renderTexture.height;Texture2D tex =new Texture2D(width, height, TextureFormat.ARGB32, false); //創(chuàng)建空白貼圖var backupActive = RenderTexture.active;RenderTexture.active = renderTexture; //將渲染貼圖信息放入緩存tex .ReadPixels(new Rect(0, 0, width, height), 0, 0); //復(fù)制緩存信息tex .Apply();RenderTexture.active = backupActive;var bytes = tex .EncodeToPNG();//示例保存成png格式File.WriteAllBytes(path, bytes);}#endregion} }

總結(jié)

以上是生活随笔為你收集整理的Unity将相机内容输出成图片的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。