如何修改此脚本以连续拍摄照片? [英] How to modify this script to capture photos continuously?

查看:158
本文介绍了如何修改此脚本以连续拍摄照片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am able to capture single photo using this script. How should I modify this script to capture photos continuously to render it on 3D plane? I want to pass that "targetTexture" to some function continuously.

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 () 
    {           
    }
}

推荐答案

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

Afaik you can not make a new TakePhotoAsync call every frame but have to wait until the current process finished. It is to performance intense and afaik also gets exclusive permission to access the camera device so any other call fails meanwhile.

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

In order to wait with the next photo until before one is finished in OnCapturedPhotoToMemory you could simply instead of

photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode);

调用下一张照片

photoCaptureObject.TakePhotoAsync(OnCapturedPhotoToMemory);


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


Maybe you should add an exit condition right before it like a private bool shouldStop like

if(shouldStop)
{
    photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode);
}
else
{
    photoCaptureObject.TakePhotoAsync(OnCapturedPhotoToMemory);
}


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


However, be aware that this will slow down your App drastically! Since most of the Texture2D stuff happens on the main thread and is quite performance intense!

这篇关于如何修改此脚本以连续拍摄照片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆