http://www.itnose.net/detail/6259004.html

一:Unity 中使用WebCamTexture 调用摄像头实现拍照和摄像。

using UnityEngine;
using System.Collections;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime .Serialization.Formatters.Binary;
using System.Threading; public class takePhoto : MonoBehaviour
{
public string deviceName;
//接收返回的图片数据
WebCamTexture tex;
public Texture2D _tex; void OnGUI()
{
if (GUI.Button(new Rect(10, 20, 100, 40), "开启摄像头"))
{
// 调用摄像头
StartCoroutine(start());
} if(GUI.Button(new Rect(10,70,100,40),"捕获照片"))
{
//捕获照片
tex.Pause();
StartCoroutine(getTexture());
} if(GUI.Button(new Rect(10,120,100,40),"再次捕获"))
{
//重新开始
tex.Play();
} if(GUI.Button(new Rect(120,20,80,40),"录像"))
{
//录像
StartCoroutine(SeriousPhotoes());
} if(GUI.Button(new Rect(10,170,100,40),"停止"))
{
//停止捕获镜头
tex.Stop ();
StopAllCoroutines();
} if(tex!=null)
{
// 捕获截图大小 ?距X左屏距离 | 距Y上屏距离
GUI.DrawTexture(new Rect(Screen.width/2-150,Screen.height/2-190,280,200),tex);
} } /// <summary>
/// 捕获窗口位置
/// </summary>
public IEnumerator start()
{
yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
if (Application.HasUserAuthorization(UserAuthorization.WebCam))
{
WebCamDevice[] devices = WebCamTexture.devices;
deviceName= devices[0].name;
tex=new WebCamTexture(deviceName,300,300,12);
tex.Play();
}
} /// <summary>
/// 获取截图
/// </summary>
/// <returns>The texture.</returns>
public IEnumerator getTexture()
{
yield return new WaitForEndOfFrame();
Texture2D t=new Texture2D(400,300);
t.ReadPixels( new Rect(Screen.width/2-200,Screen.height/2-50,360,300),0,0,false);
//距X左的距离 距Y屏上的距离
// t.ReadPixels(new Rect(220, 180, 200, 180), 0, 0, false);
t.Apply();
byte[] byt=t.EncodeToPNG();
// File.WriteAllBytes(Application.dataPath+"/Photoes/"+Time.time+".jpg",byt);
tex.Play();
} /// <summary>
/// 连续捕获照片
/// </summary>
/// <returns>The photoes.</returns>
public IEnumerator SeriousPhotoes()
{
while (true)
{
yield return new WaitForEndOfFrame();
Texture2D t = new Texture2D(400, 300, TextureFormat.RGB24, true);
t.ReadPixels(new Rect(Screen.width/2-180,Screen.height/2-50,360,300), 0, 0, false);
t.Apply();
print(t);
byte[] byt = t.EncodeToPNG();
// File.WriteAllBytes(Application.dataPath + "/MulPhotoes/" + Time.time.ToString().Split('.')[0] + "_" + Time.time.ToString().Split('.')[1] + ".png", byt);
Thread.Sleep(300);
}
}
}

二:Unity 中使用WebCamTexture 设置背景为摄像头画面

1在unity的场景中新建一个Quad作为背景,可以自行调节缩放和位置。

2.新建一个Material文件夹用来存放Material,在Material里新建一个Material材质,并命名为CamTex。
3.选中CamTex材质,在Inspector面板中选择shader的模式为Unlit/Texture。
4.新建C#脚本,并将其命名为WebCam,双击脚本进行编辑,添加以下代码:
using UnityEngine;
using System.Collections; public class WebCam : MonoBehaviour { public string deviceName;
WebCamTexture tex;
// Use this for initialization
IEnumerator Start () {
yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
if(Application.HasUserAuthorization(UserAuthorization.WebCam))
{
WebCamDevice[] devices = WebCamTexture.devices;
deviceName = devices[0].name;
tex = new WebCamTexture(deviceName, 400, 300, 12);
renderer.material.mainTexture = tex;
tex.Play();
}
}
}
5.将CamWeb脚本和CamTex材质拖到Quad上
6.将Quad调至摄像机正对位置。
7.点击播放按钮即可调用摄像头,在quad的贴图会显示摄像头中的画面。

最新文章

  1. 常用的数据统计Sql 总结
  2. ios - runtime运行时应用---交换方法
  3. 在.NET下多层架构企业管理系统的开发
  4. 20160205 - Windows 10 家庭版没有组策略
  5. 转:Web App开发入门
  6. PowerCmd-cmd命令行管理工具
  7. 在C#中使用json字符串
  8. 连接池dbcp pool
  9. block, inline和inline-block的区别
  10. Android 开发技巧
  11. MOBA服务器开发第一阶段完成总结
  12. 解决jequry使用keydown无法跳转的问题
  13. Linux vi 编辑器常见命令的使用
  14. ES6中Set 和 Map用法
  15. mysql 自增长
  16. 模拟windows全盘搜索
  17. [vue] [axios] 设置代理实现跨域时的纠错
  18. 升级mac xcode打包证书报错 git 报错
  19. [javaSE] 多线程(售票例子)
  20. CXF整合Spring之JaxWsProxyFactoryBean调用

热门文章

  1. php访问mysql数据库
  2. Struts2的类型转换
  3. 【leetcode❤python】Binary Watch
  4. MySQL中怎么对varchar类型排序问题
  5. FZU 2147 A-B Game
  6. 【转载】STL&quot;源码&quot;剖析-重点知识总结
  7. XUnit的使用
  8. kaili 2.0 开启ssh远程
  9. shape into blocks--source code in python based on pySpark
  10. 纯js写验证码