[英]The IEnumerator with voice command function
我有一個項目,我只使用語音命令來執行一些不同的 function,其中一個是使用 Hololens 拍照。 所以我使用StartCoroutine(photoshoot());
function 調用IEnumerator photoshoot()
。 IEnumerator photoshoot()
調用TakePhotosnap();
.
它完美地拍攝了照片,但是在拍攝照片后我遇到了問題,它沒有 go 回到IEnumerator
。
它停止代碼並且不能執行任何其他 function。
正如您在我的代碼中看到的(我輸入了一些數字來幫助我解釋 function)
我調用StartCoroutine(photoshoot());
第 11 行,在IEnumerator photoshoot()
中調用TakePhotosnap();
第12行,直到第13行Debug.Log("we finish taking photo successfully ");
然后停止。 它應該 go 到IEnumerator photoshoot()
的第 14 行。
這是我的一些代碼
private void Takephoto()
{
// this function is to call to take a photo and save it in a special folder
Debug.Log("Take Photo function call is started");
11 StartCoroutine(photoshoot());
Debug.Log("Take Photo for Hololens");
}
IEnumerator photoshoot()
{
Debug.Log(" The taking photo coroutine is started ");
yield return new WaitForEndOfFrame();
Debug.Log("Take Photo");
12 TakePhotosnap();
14 Debug.Log("Finish taking Hi again ");
yield return new WaitForEndOfFrame();
GameObject.Find("Cube").transform.localPosition = new Vector3(Random.Range(-1, 1), 0, Random.Range(1, 3));
GameObject.Find("Cube").SetActive(true);
}
--------------------------------------------
private void TakePhotosnap()
{
Debug.Log("TakePhoto Call StartPhotoModeAsync () method to start the photo mode");
Debug.Log("snap pic taken");
PhotoCapture.CreateAsync(false, OnPhotoCaptureCreated);
}
void OnPhotoCaptureCreated(PhotoCapture captureObject)
{
//Store objects, configure shooting parameters and start shooting mode.
Debug.Log("Start taking photo calling function");
photoCaptureObject = captureObject;
Debug.Log("set camera parameters");
Resolution cameraResolution = PhotoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First();
CameraParameters c = new CameraParameters();
/// c= CameraParameters
c.hologramOpacity = 1.0f;
c.cameraResolutionWidth = cameraResolution.width;
c.cameraResolutionHeight = cameraResolution.height;
c.pixelFormat = CapturePixelFormat.BGRA32;
Debug.Log("camera parameters finish");
captureObject.StartPhotoModeAsync(c, OnPhotoModeStarted);
}
private void OnPhotoModeStarted(PhotoCapture.PhotoCaptureResult result)
{
if (result.success)
{
//string filename = string.Format(@"CapturedImage{0}_n.jpg", Time.time);
string filename = string.Format(@"alc.jpg", Time.time);
Debug.Log("FileName: =" + filename);
string filePath = System.IO.Path.Combine(Application.persistentDataPath, filename);
Debug.Log("filePath: =" + filePath);
/////
string targetPath = @"C: \Users\ABC\Pictures\Camera Roll";
string destFile = System.IO.Path.Combine(targetPath, filename);
Debug.Log("destFile: =" + destFile);
if (!System.IO.File.Exists(filePath))
{
//System.IO.File.Create(filePath);
System.IO.File.Create(filePath).Dispose();
}
// https://blog.csdn.net/Lee_gc/java/article/details/79919042
Debug.Log("filePath filePath: =" + filePath);
string filePath2 = System.IO.Path.Combine(Application.dataPath, filename);
Debug.Log("filePath2: =" + filePath2);
Debug.Log("finish to set photo file path and name");
//photoCaptureObject.TakePhotoAsync(filePath, PhotoCaptureFileOutputFormat.JPG, OnCapturedPhotoToDisk);
Debug.LogError("Saved That Image Somewhere" + "FileName: =" + filename + " FilePath: = " + filePath + " FilePath2: = " + filePath2);
Debug.Log("finish to copy photo to new directory");
Debug.Log("finish photo");
photoCaptureObject.TakePhotoAsync(filePath, PhotoCaptureFileOutputFormat.JPG, OnCapturedPhotoToDisk);
13 Debug.Log("we finish taking photo successfuly ");
}
else
{
Debug.LogError("Unable to start photo mode!");
}
}
// clean up
void OnStoppedPhotoMode(PhotoCapture.PhotoCaptureResult result)
{
Debug.Log("result=" + result);
photoCaptureObject.Dispose();
photoCaptureObject = null;
}
void OnCapturedPhotoToDisk(PhotoCapture.PhotoCaptureResult result)
{
if (result.success)
{
Debug.Log("Saved Photo to disk!");
photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode);
}
else
{
Debug.Log("Failed to save Photo to disk");
}
}
}
我的代碼有什么問題? 有沒有其他方法可以解決它?
事實上,代碼 14 的運行時間比您想象的要早。 在TakePhotosnap
function 中,當異步方法PhotoCapture.CreateAsync()
運行到時,該方法會將控制權返回給調用方法,然后“再次完成拍攝”將立即為 output。 另外,也因為異步,在TakePhotoAsync
方法返回任何結果之前,代碼13會是output,所以它並不代表照片拍攝成功。
因此,我們建議您在代碼中添加斷點以確定代碼 14 是否執行,並嘗試將日志語句移至回調。 例如:
photoCaptureObject.TakePhotoAsync(filePath, PhotoCaptureFileOutputFormat.JPG, OnCapturedPhotoToDisk);
Debug.Log("we finish taking photo successfuly ");
=>
photoCaptureObject.TakePhotoAsync(filePath, PhotoCaptureFileOutputFormat.JPG, OnCapturedPhotoToDisk);
void OnCapturedPhotoToDisk(PhotoCapture.PhotoCaptureResult result)
{
if (result.success)
{
Debug.Log("we finish taking photo successfuly ");
}
}
此外,這個文檔值得你一讀: Asynchronous Programming with async and await
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.