public void TakePhotoAsync (string filename, XR.WSA.WebCam.PhotoCaptureFileOutputFormat fileOutputFormat, XR.WSA.WebCam.PhotoCapture.OnCapturedToDiskCallback onCapturedPhotoToDiskCallback);
public void TakePhotoAsync (XR.WSA.WebCam.PhotoCapture.OnCapturedToMemoryCallback onCapturedPhotoToMemoryCallback);

参数

filename应保存照片的位置。文件名末尾的文件扩展名必须为 png 或 jpg。
fileOutputFormat应使用的编码格式。
onCapturedPhotoToDiskCallback在照片被保存到磁盘中时调用。
onCapturedPhotoToMemoryCallback在照片被复制到目标纹理中时调用。

描述

使用网络摄像机异步捕捉照片并将照片存储在磁盘上。

可通过两种方式使用此方法。 您可以使用此方法通过网络摄像机直接将照片捕捉到 CPU 内存中。然后,您可以将图像数据 应用到纹理中,以便在 Unity 中使用,或者将图像数据传递到外部插件。直接将照片捕捉到内存中时, 您还将看到空间信息,并通过这些信息了解拍摄照片时所处的空间位置。 您还可以直接将照片捕捉到磁盘中,保存为 png 或 jpg。

将图像保存为 JPEG 格式时,系统会将 EXIF 元数据编码到捕捉的图像中。使用 BGRA32 和 NV12 像素格式时,您可以将捕捉的图像保存为 JPEG 格式。使用 BGRA32 像素格式时,则只能将捕捉的图像保存为 PNG 格式。

此示例将使用网络摄像机捕捉图像,并将图像保存到磁盘上。

using UnityEngine;
using System.Collections;
using System.Linq;
using UnityEngine.XR.WSA.WebCam;

public class PhotoCaptureToDiskExample : MonoBehaviour { PhotoCapture photoCaptureObject = null;

static readonly int TotalImagesToCapture = 3; int capturedImageCount = 0;

// Use this for initialization void Start() { Resolution cameraResolution = PhotoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First(); Texture2D targetTexture = new Texture2D(cameraResolution.width, cameraResolution.height);

PhotoCapture.CreateAsync(false, delegate(PhotoCapture captureObject) { Debug.Log("Created PhotoCapture Object"); photoCaptureObject = captureObject;

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

captureObject.StartPhotoModeAsync(c, delegate(PhotoCapture.PhotoCaptureResult result) { Debug.Log("Started Photo Capture Mode"); TakePicture(); }); }); }

void OnCapturedPhotoToDisk(PhotoCapture.PhotoCaptureResult result) { Debug.Log("Saved Picture To Disk!");

if (capturedImageCount < TotalImagesToCapture) { TakePicture(); } else { photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode); } }

void TakePicture() { capturedImageCount++; Debug.Log(string.Format("Taking Picture ({0}/{1})...", capturedImageCount, TotalImagesToCapture)); string filename = string.Format(@"CapturedImage{0}.jpg", capturedImageCount); string filePath = System.IO.Path.Combine(Application.persistentDataPath, filename);

photoCaptureObject.TakePhotoAsync(filePath, PhotoCaptureFileOutputFormat.JPG, OnCapturedPhotoToDisk); }

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

Debug.Log("Captured images have been saved at the following path."); Debug.Log(Application.persistentDataPath); } }