【发布时间】:2020-10-10 07:46:23
【问题描述】:
我有一个项目,我只使用语音命令来执行一些不同的功能,其中之一是使用 Hololens 拍照。所以我使用StartCoroutine(photoshoot());函数调用IEnumerator photoshoot()。 IEnumerator photoshoot() 调用TakePhotosnap();。
它完美地拍摄了照片,但我在拍摄照片后出现问题,它不会返回到IEnumerator。
它会停止代码并且不能执行任何其他功能。
正如你在我的代码中看到的(我放了一些数字来帮助我解释函数)
我打电话给StartCoroutine(photoshoot()); 第11 行,在IEnumerator photoshoot() 中调用TakePhotosnap(); 第12 行,它会拍照直到第13 行Debug.Log("we finish taking photo successfully "); 然后停止。它应该转到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");
}
}
}
我的代码有什么问题?有没有其他办法解决?
【问题讨论】:
-
如果您要添加这么多代码,请正确格式化您的代码
-
@Jonesopolis 是的,我将不得不支持这个。仅这些括号就使事情变得很难阅读。除了代码的格式之外,问题写得很好!
-
请阅读Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers? - 总结是这不是解决志愿者的理想方式,并且可能会适得其反。请不要将此添加到您的问题中。
标签: c# unity3d camera hololens ienumerator