参考:
https://developer.microsoft.com/en-us/windows/mixed-reality/locatable_camera_in_unity
http://www.cnblogs.com/mantgh/p/5667385.html
1.Capture a Photo to a File 捕获照片到文件
可通过HoloLens PC网页端打开文件搜索,找到相应APP下文件图像信息
2.Capture a Photo to a Texture2D 捕获文件到Texture2D对象
// 进一步使用Texture2D对象,比如赋给材质神马的
在这里,可以在Unity中cube上的材质显示图片
源代码:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.VR.WSA.WebCam; using System.Linq; public class PhotoCaptureCamera : MonoBehaviour { PhotoCapture photoCaptureObject = null; 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, false, OnPhotoModeStarted); captureObject.StartPhotoModeAsync(c,OnPhotoModeStarted); } void OnStoppedPhotoMode(PhotoCapture.PhotoCaptureResult result) { photoCaptureObject.Dispose(); photoCaptureObject = null; } //Capture a Photo to a File 捕获照片到文件 /* private void OnPhotoModeStarted(PhotoCapture.PhotoCaptureResult result) { if (result.success) { string filename = string.Format(@"CapturedImage{0}_n.jpg", Time.time); string filePath = System.IO.Path.Combine(Application.persistentDataPath, filename); photoCaptureObject.TakePhotoAsync(filePath, PhotoCaptureFileOutputFormat.JPG, OnCapturedPhotoToDisk); } else { Debug.LogError("Unable to start photo mode!"); } } 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"); } } */ //Capture a Photo to a Texture2D 捕获文件到Texture2D对象 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) { // 使用正确分辨率创建Texture2D对象 Resolution cameraResolution = PhotoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First(); Texture2D targetTexture = new Texture2D(cameraResolution.width, cameraResolution.height); // 将图像数据拷贝到Texture2D对象中 photoCaptureFrame.UploadImageDataToTexture(targetTexture); // 进一步使用Texture2D对象,比如赋给材质神马的 // Create a gameobject that we can apply our texture to GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad); Renderer quadRenderer = quad.GetComponent<Renderer>() as Renderer; quadRenderer.material = new Material(Shader.Find("Custom/Unlit/UnlitTexture")); quad.transform.parent = this.transform; quad.transform.localPosition = new Vector3(0.0f, 0.0f, 3.0f); quadRenderer.material.SetTexture("_MainTex", targetTexture); } // 清理相机 photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode); } //Capture a Photo and Interact with the Raw bytes 捕获照片并和原始数据交互 /* void OnCapturedPhotoToMemory(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame) { if (result.success) { List<byte> imageBufferList = new List<byte>(); // 复制原始 IMFMediaBuffer 数据到空的list photoCaptureFrame.CopyRawImageDataIntoBuffer(imageBufferList); //本例使用 BGRA32 格式捕获照片. int stride = 4; float denominator = 1.0f / 255.0f; List<Color> colorArray = new List<Color>(); for (int i = imageBufferList.Count - 1; i >= 0; i -= stride) { float a = (int)(imageBufferList[i - 0]) * denominator; float r = (int)(imageBufferList[i - 1]) * denominator; float g = (int)(imageBufferList[i - 2]) * denominator; float b = (int)(imageBufferList[i - 3]) * denominator; colorArray.Add(new Color(r, g, b, a)); } // 接下来可以把list用做进一步的处理 } photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode); } */ // Update is called once per frame void Update () { } }
