HoloLens开发——Locatable camera in Unity
来源:互联网 发布:linux中du命令 编辑:程序博客网 时间:2024/06/06 07:46
参考:
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 () {
}
}
- HoloLens开发——Locatable camera in Unity
- hololens开发手记——Unity之语音输入
- Unity Hololens开发教程
- Hololens输入——Unity语音输入
- 【Unity+Hololens开发】识别二维码
- Hololens——开发入门
- HoloLens开发——利用Unity WWW访问图片并将贴图付给cube Renderer
- Hololens开发手记——Unity之World Anchor空间锚
- Hololens开发手记——Unity之Spatial Sounds空间声音
- 【Hololens开发】为Hololens配置基于Unity的开发环境
- Camera Shake in Unity
- Hololens开发手记--Unity之Persistence
- HoloLens开发——vuforia图像识别
- HoloLens软件开发——介绍
- 微软Hololens应用及开发——HoloLens介绍篇(一)全息图介绍
- 微软Hololens应用及开发——HoloLens介绍篇(二)硬件介绍
- 微软Hololens应用及开发——HoloLens介绍篇(三)应用界面
- Hololens入门之使用Unity开发一个简单的应用
- Oracle数据库学习
- 兼容多浏览器,兼容 div、p、td 的强制不换行及强制换行
- loadrunner录制脚本之前的设置
- BLE开发资料汇总
- 关于QuartZ的那些事儿 1.0
- HoloLens开发——Locatable camera in Unity
- gamma校正中两种不同的访问数据的方法为什么得到的校正图像不同??
- Word替换操作
- 约瑟夫环问题
- 树莓派安装mjpg
- java Date 日期格式
- 2.1 CSS一些常见的属性&&选择器
- CSDN日报20170504 ——《移动开发者的未来在哪里?》
- 利用stm32串口中断进行数码管显示