如何修改此脚本以连续拍摄照片?

时间:2019-03-29 13:42:51

标签: c# unity3d

我可以使用此脚本捕获单张照片。如何修改此脚本以连续捕获照片以在3D平面上渲染?我想将该“ targetTexture”连续传递给某些函数。

public class PhotoCaptureExample : MonoBehaviour
{
    PhotoCapture photoCaptureObject = null;

    // Use this for initialization
    void Start ()
    {
        PhotoCapture.CreateAsync(false, OnPhotoCaptureCreated);
    }

    void OnPhotoCaptureCreated(PhotoCapture captureObject)
    {
        photoCaptureObject = captureObject;

        Resolution cameraResolution = PhotoCapture.SupportedResolutions
            .OrderByDescending((res) => res.width * res.height).First();

        CameraParameters c = new CameraParameters();
        c.hologramOpacity = 0.0f;
        c.cameraResolutionWidth = cameraResolution.width;
        c.cameraResolutionHeight = cameraResolution.height;
        c.pixelFormat = CapturePixelFormat.BGRA32;

        captureObject.StartPhotoModeAsync(c, OnPhotoModeStarted);
    }

    void OnStoppedPhotoMode(PhotoCapture.PhotoCaptureResult result)
    {
        photoCaptureObject.Dispose();
        photoCaptureObject = null;
    }

    private void OnPhotoModeStarted(PhotoCapture.PhotoCaptureResult result)
    {
        if (result.success)
        {
            photoCaptureObject.TakePhotoAsync(OnCapturedPhotoToMemory);
        }
        else
        {
            Debug.LogError("Unable to start photo mode!");
        }
    }

    void OnCapturedPhotoToMemory(PhotoCapture.PhotoCaptureResult result, 
        PhotoCaptureFrame photoCaptureFrame)
    {
        if (result.success)
        {
            // Create our Texture2D for use and set the correct resolution
            Resolution cameraResolution = PhotoCapture.SupportedResolutions
                .OrderByDescending((res) => res.width * res.height).First();
            Texture2D targetTexture = new Texture2D(cameraResolution.width, 
                cameraResolution.height);

            // Copy the raw image data into our target texture
            photoCaptureFrame.UploadImageDataToTexture(targetTexture);
            // Do as we wish with the texture such as apply it to a material, etc.
        }
        // Clean up
        photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode);
    }

    // Update is called once per frame
    void Update () 
    {           
    }
}

2 个答案:

答案 0 :(得分:0)

这远非完美,我还没有测试过,但是类似的东西应该可以工作。不过,您可能需要在异步void方法内使用while循环,以免冻结应用程序。

colSums(a == 'win') > 0

答案 1 :(得分:0)

Afaik,您不能在每一帧都进行新的TakePhotoAsync调用,而必须等到当前过程完成为止。这是对性能的强烈要求,afaik还获得了访问摄像头设备的专有许可,因此任何其他呼叫均会失败。


为了等待下一张照片,直到在OnCapturedPhotoToMemory中完成照片之前,您可以简单地代替

photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode);

调用下一张照片

photoCaptureObject.TakePhotoAsync(OnCapturedPhotoToMemory);

也许您应该像在private bool shouldStop之类的

之前添加退出条件
if(shouldStop)
{
    photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode);
}
else
{
    photoCaptureObject.TakePhotoAsync(OnCapturedPhotoToMemory);
}

但是,请注意,这将大大降低您的应用程序的速度!由于大多数Texture2D东西都发生在主线程上,因此性能非常高!