我有一个脚本,应该能够拍摄多张照片。我正在使用照片捕获,但收到错误,使其无法捕获第二张照片。我在photoCaptureObject.StartPhotoModeAsync(cameraParameters, result =>
行上收到错误"值不能为空",但我不明白为什么会这样。
我已经注释掉了photoCaptureObject = null;
行,以便照片捕获对象不应该为空。行if (photoCaptureObject == null) return;
还证明photoCaptureObject
不为空。
PhotoCapture photoCaptureObject = null;
Texture2D targetTexture = null;
public string path = "";
CameraParameters cameraParameters = new CameraParameters();
private void Awake()
{
var cameraResolution = PhotoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First();
targetTexture = new Texture2D(cameraResolution.width, cameraResolution.height);
// Create a PhotoCapture object
PhotoCapture.CreateAsync(false, captureObject =>
{
photoCaptureObject = captureObject;
cameraParameters.hologramOpacity = 0.0f;
cameraParameters.cameraResolutionWidth = cameraResolution.width;
cameraParameters.cameraResolutionHeight = cameraResolution.height;
cameraParameters.pixelFormat = CapturePixelFormat.BGRA32;
});
}
private void Update()
{
// if not initialized yet don't take input
if (photoCaptureObject == null) return;
if (Input.GetKeyDown(KeyCode.K) || Input.GetKeyDown("k"))
{
Debug.Log("k was pressed");
VuforiaBehaviour.Instance.gameObject.SetActive(false);
// Activate the camera
photoCaptureObject.StartPhotoModeAsync(cameraParameters, result =>
{
if (result.success)
{
// Take a picture
photoCaptureObject.TakePhotoAsync(OnCapturedPhotoToMemory);
}
else
{
Debug.LogError("Couldn't start photo mode!", this);
}
});
}
}
中间有一些代码可以更改拍摄的照片等等,但我认为该代码不是问题的一部分。
private void OnStoppedPhotoMode(PhotoCapture.PhotoCaptureResult result)
{
// Shutdown the photo capture resource
VuforiaBehaviour.Instance.gameObject.SetActive(true);
photoCaptureObject.Dispose();
//photoCaptureObject = null;
Debug.Log("Photomode stopped");
}
那么还有什么可能是空的呢?不知何故是StartPhotoModeAsync
?如何解决此问题?
谢谢!
好的,多亏了亨里克的评论,我现在明白了。
Unity 特别提到了StartPhotoModeAsync
:
在任何给定位置,只有一个照片捕获实例可以启动照片模式 时间
在说应该始终使用PhotoCapture.StopPhotoModeAsync
之后,我更关注这句话,因为打开 PhotoCapture 模式需要更多功能,所以我从未想过实例在停止后不会再次启动。
现在我只在按键更新中TakePhotoAsync
,并且不会停止 PhotoMode,因为我正在制作的应用程序应该始终能够捕获照片。