代码仅供参考

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System;public class test : MonoBehaviour
{    private WebCamTexture mWebcamTexBack;private WebCamTexture mWebcamTexFront;int back = -1, front = -1, current = -1;public RawImage rawImg_CamTexture;public Button btn_Photo;public Button btn_Change;public Image img_Preview;public Text txtInfos;public Text txtInfos2;// Use this for initializationvoid Start (){btn_Photo.onClick.AddListener(() => { OnBtn_Photo(); });btn_Change.onClick.AddListener(() => { OnBtn_Change(); });StartCoroutine(InitAndOpenCamera());PrintInfo();}void Update(){if(Time.frameCount%10==0){PrintInfo();}}IEnumerator InitAndOpenCamera(){yield return StartCoroutine(ApplyCamera());OpenBackCam();}/// <summary>/// 申请相机/// </summary>/// <returns></returns>IEnumerator ApplyCamera(){if (!Application.HasUserAuthorization(UserAuthorization.WebCam)){yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);//授权}if (Application.HasUserAuthorization(UserAuthorization.WebCam)){int length = WebCamTexture.devices.Length;if (length <= 0){Debug.Log("你的设备没有摄像头!!!");enabled = false;yield break;}else if(length == 1){mWebcamTexBack = new WebCamTexture(WebCamTexture.devices[0].name, (int)(Screen.width), (int)(Screen.height), 12);}else{                for(int i= 0; i < length; i++){if(WebCamTexture.devices[i].isFrontFacing){front = i;}else{if(back==-1){back = i;}}}if(front==-1){front = back + 1;}}}{enabled = false;}}void OpenBackCam(){if (back == -1){Debug.Log("你的设备没有摄像头!!!");return;}current = back;if (mWebcamTexFront!=null && mWebcamTexFront.isPlaying){mWebcamTexFront.Stop();}//if(mWebcamTexBack!=null)//{//    if (mWebcamTexBack.isPlaying)//    {//        return;//    }//    mWebcamTexBack.Play();//    return;//}mWebcamTexBack = new WebCamTexture(WebCamTexture.devices[current].name, (int)(Screen.height), (int)(Screen.width), 20);mWebcamTexBack.Play();rawImg_CamTexture.texture = mWebcamTexBack;}void OpenFrontCam(){if(front==-1){OpenBackCam();return;}current = front; if (mWebcamTexBack != null && mWebcamTexBack.isPlaying){mWebcamTexBack.Stop();}//if (mWebcamTexFront != null)//{//    if (mWebcamTexFront.isPlaying)//    {//        return;//    }//    mWebcamTexFront.Play();//    return;//}mWebcamTexFront = new WebCamTexture(WebCamTexture.devices[front].name, (int)(Screen.height), (int)(Screen.width), 20);mWebcamTexFront.Play();rawImg_CamTexture.texture = mWebcamTexFront;}private void OnBtn_Photo(){StartCoroutine(GetTexture());}private void OnBtn_Change(){ChangeCamera();}public void ChangeCamera(){if(current==back){OpenFrontCam();}else{OpenBackCam();}WebCamTexture _webCamTexture = GetCurrent_WebCamTexture();int videoRotationAngle = _webCamTexture.videoRotationAngle;rawImg_CamTexture.transform.rotation = Quaternion.Euler(0, 0, -videoRotationAngle);}public WebCamTexture GetCurrent_WebCamTexture(){WebCamTexture _webCamTexture;if (current == back) _webCamTexture = mWebcamTexBack;else _webCamTexture = mWebcamTexFront;return _webCamTexture;}public IEnumerator GetTexture(){WebCamTexture _webCamTexture = GetCurrent_WebCamTexture();_webCamTexture.Pause();yield return new WaitForEndOfFrame();// 旋转预览精灵int videoRotationAngle = _webCamTexture.videoRotationAngle;img_Preview.transform.parent.rotation = Quaternion.Euler(0, 0, -videoRotationAngle);// 确定预览图,要截取镜头的大小int w = _webCamTexture.width;w = _webCamTexture.height > w ? w : _webCamTexture.height;Vector2 offset = new Vector2((_webCamTexture.width - w) / 2, (_webCamTexture.height - w) / 2);//Texture2D mTexture = new Texture2D(_webCamTexture.width, _webCamTexture.height, TextureFormat.ARGB32, false);//mTexture.SetPixels32(_webCamTexture.GetPixels32());Texture2D mTexture = new Texture2D(w, w, TextureFormat.ARGB32, false);mTexture.SetPixels(_webCamTexture.GetPixels((int)offset.x,(int)offset.y, w, w));mTexture.Apply();        img_Preview.sprite = Sprite.Create(mTexture, new Rect(0, 0, w, w),Vector2.zero);_webCamTexture.Play();//byte[] bt = mtexture.EncodeToPNG();//string mPhotoName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".png";//mPhotoPath = Application.persistentDataPath + "/" + mPhotoName;//System.IO.File.WriteAllBytes(mPhotoPath, bt);//Debug.LogError("照片路径  " + mPhotoPath);//if (callback != null) callback();}void PrintInfo(){if (txtInfos == null || !txtInfos.isActiveAndEnabled) return;System.Text.StringBuilder sb = new System.Text.StringBuilder();sb.AppendLine("screen: w = " + Screen.width + " h = " + Screen.height);sb.AppendLine("backCamText: " + mWebcamTexBack.width + ", " + mWebcamTexBack.height);sb.AppendLine("frontCamText: " + mWebcamTexFront.width + ", " + mWebcamTexFront.height);sb.Append("current: " + current);sb.Append("  back: " + back);sb.AppendLine("  front: " + front);sb.AppendLine("back videoRotationAngle:" + mWebcamTexBack.videoRotationAngle);sb.AppendLine("front videoRotationAngle:" + mWebcamTexFront.videoRotationAngle);txtInfos.text = sb.ToString();}
}

hololens拍照相关推荐

  1. Hololens拍照,录像,保存到本地,读取图片和视频

    工程地址: https://download.csdn.net/download/shanguuncle/10548611 可到裙文件下载211031265 1.拍照 拍照API https://do ...

  2. Hololens第三人称视角Spectator View!

    首先分享一段TED Talk,这个是全球顶级的演讲,一般演讲跟IT男都没啥关系,比较内敛对吧,或者就没啥这方面细胞,乔布斯的演讲还不错,每次新品发布都挑逗观众G点,可惜人家不是IT男.能在TED Ta ...

  3. 开始开发HoloLens应用吧 Start Developing HoloLens Apps Today

    在经历数个月的期待与等待后,终于拿到了预订的 HoloLens 开发者版本套件.经过一个月的学习和研究,对于HoloLens开发有了更浓厚的兴趣. 根据积累的经验,特录制了一节HoloLens开发教程 ...

  4. openGL之API学习(一四八)3D全息眼镜HoloLens

    http://www.igao7.com/news/201609/fB8o5t4zKGncFKpB.html 北京时间2016年3月28日午间消息,微软研发部门开发出一种新的3D视频捕捉系统" ...

  5. 深度分析:Magic Leap与微软Hololens有哪些异同

    深度分析:Magic Leap与微软Hololens有哪些异同 2015-11-04 10:20:16 来源: 雷锋网(深圳) 分享到: 12 本文作者:@胡伯涛 Botao Amber Hu,清华大 ...

  6. 使用Unity在HoloLens中实现终结者视觉HUD

    詹姆斯·卡梅隆1984年的电影<终结者>引入了我们现在认为理所当然的许多科幻风格.其中最为持久的是热成像平视显示器(HUD),能让观众通过阿诺德斯瓦辛格的T-800角色看到世界.在设计圈里 ...

  7. HoloLens联合发明人:打造理想的全天AR需要解决这些问题

    自从HoloLens 1在2016年发布,至今已经过去8年时间,但AR眼镜/头显小型化的进度依然缓慢,AR硬件厂商需要不断在性能配置和重量/外形之间做权衡.相比于VR,AR的发展进度显然更加缓慢,研发 ...

  8. SpectatorView For Hololens

    文章目录 一.第三人称视角(Preview) 1.1 使用案例 1.2 当前功能 1.3 许可证 二.如何设置第三人称视角(Preview) 2.1 必需品 2.2 构建第三人称视角本地插件 2.3 ...

  9. HoloLens 网络摄像头

    网络摄像头 API 是一种可以拍摄照片和录制视频的异步 API.网络摄像头属于共享资源.这意味着在任何给定时间只能激活一个 PhotoCapture 或 VideoCapture 实例.可以使用 We ...

最新文章

  1. 想知道黑苹果什么味道?来咬一口尝尝~
  2. 【企业管理】优秀的管理者没有追随者,而是与大家一起奋斗
  3. java: PO,VO,TO,BO,DAO,POJO 解释
  4. 质量故事(4)---割草的男孩
  5. Spring DI(依赖注入)注解篇
  6. SpringBoot使用AOP,PointCut表达式详解以及使用
  7. java xml annotation_Annotation与XML配置ID生成策略
  8. myeclipse 编写html,myeclipse怎么写html
  9. ulimit: command not found 问题解决
  10. 阿里云为什么在十三年后重构调度系统?
  11. 【正点原子STM32连载】第三章 开发环境搭建 摘自【正点原子】MiniPro STM32H750 开发指南_V1.1
  12. OWASP juice shop靶场闯关题解
  13. 抢购茅台,618只能用这种方法
  14. vue获取当前时间、时间戳方法
  15. 求10个数中的最大值及其对应的下标
  16. 不同的国家/地区与语言缩写代码
  17. 计算机硬件专业知识西瓜视频,go实现西瓜视频花椒直播等平台智能答题
  18. DBA所需要具备技能
  19. 一探B站后台架构, 他山之石, 何以攻玉? -- 仅从一个一线Golang开发者的角度谈B站4.22代码
  20. Copy攻城狮的年度之“战”|回顾2020

热门文章

  1. 建筑平面布置与防火防烟分区(一)
  2. Civil3D二次开发之撤销(Undo\Ctrl+Z)命令
  3. 【日常计算机问题】win11、win10解决公共WiFi认证不弹出的问题。电脑没有弹出认证界面。以广州图书馆i-guangdong;i广东为例
  4. 椭圆曲线加密中定义的加法运算
  5. PDF转ePub选择人工转换的原因
  6. 论文笔记-Suppress and Balance: A Simple Gated Network for Salient Object Detection
  7. Segment Routing入门
  8. 安全芯片介绍-身份认证加密芯片方案
  9. Policy Center > Permissions and APIs that Access Sensitive Information
  10. NPOI编辑WORD文档