我正在使用unity的Photocapture Object來用hololens拍攝視頻。 我根據官方的示例代碼撰寫了代碼,但我改變了更新函式中的流程,以拍攝多張圖片,而不是只拍攝一張。
https://docs.unity3d.com/2018.4/Documentation/Manual/windowsholographic-photocapture.html
然而,當我運行這段代碼時,它最終耗盡了 pagefile.sys,耗盡了記憶體,并中止了。 我搜索了一下,發現大部分的記憶體泄漏是由紋理2D引起的,但在這段代碼中,即使我省略了它們,它們也會出現。此外,unity剖析器并沒有顯示代碼執行后記憶體使用量的逐漸增加。 到底是什么原因呢?如果您能告訴我,我將不勝感激。

using UnityEngine;
using System;
using System.Linq;
using UnityEngine.XR;
using UnityEngine.Windows.WebCam;
using System.Threading.Tasks;
using System.IO.Definition; using System.IO.Definition
使用System.Net.IO; 使用System.Net.IO; 使用System.Net.IO
使用System.Collections。
using System.Collections.Generic;
public class photocap : MonoBehaviour
{
PhotoCapture PhotoCapture = null;
Texture2D targetTexture = null;
解析度cameraResolution。
渲染器quadRenderer。
float dt = 0;
void Start()
{
cameraResolution = PhotoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First()。
GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad)。
quad.transform.localScale = new Vector3(0.2f, 0.2f, 0.2f)。
quadRenderer = quad.GetComponent<Renderer>() as Renderer;
quadRenderer.material = new Material(Shader.Find("Unlit/Texture"))。
quad.transform.parent = this.transform。
quad.transform.localPosition = new Vector3(0.0f, 0.0f, 0.3f)。
}
async void StartCapture()
{
PhotoCapture.CreateAsync(false, delegate (PhotoCapture captureObject)
{
PhotoCapture = captureObject;
CameraParameters cameraParameters = new CameraParameters()。
cameraParameters.hologramOpacity = 0.0f;
cameraParameters.cameraResolutionWidth = cameraResolution.width;
cameraParameters.cameraResolutionHeight = cameraResolution.height。
cameraParameters.pixelFormat = CapturePixelFormat.BGRA32;
PhotoCapture.StartPhotoModeAsync(cameraParameters, delegate (PhotoCapture.PhotoCaptureResult result)
{
PhotoCapture.TakePhotoAsync(OnCapturedPhotoToMemory)。
});
});
}
void OnCapturedPhotoToMemory(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
{
PhotoCapture.StopPhotoModeAsync(OnStoppedPhotoMode)。
}
void OnStoppedPhotoMode(PhotoCapture.PhotoCaptureResult result)
{
PhotoCapture.Dispose()。
PhotoCapture = null。
}
void Update()
{
dt = Time.deltaTime。
if (dt > 3)
{
dt = 0.0f;
StartCapture()。
}
}
}
uj5u.com熱心網友回復:
我很確定你也想/必須處理掉photoCaptureFrame!
否則,那里存盤的紋理將永遠留在你的應用程式記憶體中。
void OnCapturedPhotoToMemory(PhotoCapture. PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)。
{
// TODO: 顯然,你首先要對剛剛捕獲的影像做一些處理...
photoCaptureFrame.Dispose()。
PhotoCapture.StopPhotoModeAsync(OnStoppedPhotoMode)。
哦,還有一件事:
你目前正在根據過去的時間來啟動新的捕獲。
如果拍攝圖片、處理資料和曝光的時間超過了這個時間間隔,會發生什么情況?
你可能更希望在第一次捕獲實際完全完成后才觸發新的捕獲延遲!
實際上,為什么還要創建一個新的捕獲?
實際上,為什么要一直創建、啟動、停止和處置捕獲?你可以堅持使用一個,并且總是重復使用它
private PhotoCapture _photoCapture = null;
private Texture2D targetTexture = null;
private Resolution cameraResolution;
private 渲染器quadRenderer。
private float dt = 0;
private void Start()
{
cameraResolution = PhotoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First()。
var quad = GameObject.CreatePrimitive(PrimitiveType.Quad)。
quad.transform.localScale = new Vector3(0.2f, 0.2f, 0.2f)。
quadRenderer = quad.GetComponent<Renderer>()。
quadRenderer.material = new Material(Shader.Find("Unlit/Texture"))。
targetTexture = new Texture2D(cameraResolution.width, cameraResolution.height)。
quadRenderer.material.mainTexture = targetTexture;
quad.transform.parent = transform;
quad.transform.localPosition = new Vector3(0.0f, 0.0f, 0.3f)。
StartCapture()。
}
private void StartCapture()
{
PhotoCapture.CreateAsync(false, OnPhotoCaptureCreated)。
}
private void OnPhotoCaptureCreated(PhotoCapture captureObject)
{
_photoCapture = captureObject;
var cameraParameters = new CameraParameters
{
hologramOpacity = 0.0f,
cameraResolutionWidth = cameraResolution.width,
cameraResolutionHeight = cameraResolution.height。
pixelFormat = CapturePixelFormat.BGRA32
};
_photoCapture.StartPhotoModeAsync(cameraParameters, OnPhotoCaptureStarted)。
}
private void OnPhotoCaptureStarted(PhotoCapture.PhotoCaptureResult result)。
{
//拍攝第一張照片。
TakePhoto()。
///如果你真的想要更多的延遲,那么
//Invoke(nameof(TakePhoto, 3f));
}
private void TakePhoto()
{
_photoCapture.TakePhotoAsync(OnCapturedPhotoToMemory)。
}
private void OnCapturedPhotoToMemory(PhotoCapture. PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)。
{
//不要一直破壞和創建紋理。
//簡單地將新資料上傳到已經存在的影像上。
photoCaptureFrame.UploadImageDataToTexture(targetTexture)。
Debug.Log("Captured")。
photoCaptureFrame.Dispose()。
//沒有停止和處置等的開銷,只需拍攝下一張圖片。
TakePhoto()。
///如果你真的想要更多的延遲,那么你就需要更多的延遲。
//Invoke(nameof(TakePhoto, 3f));
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/329099.html
標籤:
上一篇:如何檢查元素是否具有此值
