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的贴图会显示摄像头中的画面。

Unity3D WebCamTexture 调用外部摄像头的更多相关文章

  1. Unity3D中调用外接摄像头,并保存为图片文件

    http://bbs.9ria.com/thread-170539-1-1.html 项目要求调用摄像头,并且把图像保存下来,上传到服务器. 这里有几个难点,调用摄像头是很简单的,unity已经提供好 ...

  2. Unity调用外部摄像头,全屏显示摄像头画面

    有两种方法,常用的是GUI方法,代码如下: public class CameraTest : MonoBehaviour { WebCamTexture camTexture; void Start ...

  3. System.Diagnostics.Process 启动进程资源或调用外部的命令的使用

    经常看到一些程序在保存为一个txt,或者excel的文件的时候,保存完毕立即打开, 启动程序或打开文件的代码 System.Diagnostics.Process.Start(System.IO.Pa ...

  4. python调用外部子进程,通过管道实现异步标准输入和输出的交互

    我们通常会遇到这样的需求:通过C++或其他较底层的语言实现了一个复杂的功能模块,需要搭建一个基于Web的Demo,方法查询数据.由于Python语言的强大和简洁,其用来搭建Demo非常合适,Flask ...

  5. 关于js调用外部部署的web api

    没想到多年之后我还得继续写这些东西.... 瀑布汗~ 最近不得不开始研究用web api MVC的项目中,在js文件里,实现点击一个按钮调用外部发布好的api,再从api把值回传给js页面,跳转. 经 ...

  6. windows下调用外部exe程序 SHELLEXECUTEINFO

    本文主要介绍两种在windows下调用外部exe程序的方法: 1.使用SHELLEXECUTEINFO 和 ShellExecuteEx SHELLEXECUTEINFO 结构体的定义如下: type ...

  7. 在Salesforce中调用外部系统所提供的的Web Service

    这里需要提供外部service所对应的WSDL文件(Salesforce只支持从本地上传),并且提供的WSDL文件有如下两点要求: 1):wsdl 文件只能有一个binding,Salesforce是 ...

  8. Unity3D脚本调用Objective C代码实现游戏内购买

    0.开篇吐槽: 一年之内从WP转到iOS,又从iOS转到U3D,真心伤不起. 1.Unity3D脚本调用OC代码的原理: 其实也没啥神秘的,因为OC是和C互通的 ,C#又可以通过DllImport的形 ...

  9. QTP学习一添加默认的注释及调用外部vbs文件

    一.添加默认注释 1.新建一个TXT文档,将要添加的注释写在文档中 2.再将文档名改为:ActionTemplate.mst 3.将文件放到QTP安装目录(默认为:C:\Program Files\H ...

随机推荐

  1. Linux 性能监控、测试、优化工具

    Linux 平台上的性能工具有很多,眼花缭乱,长期的摸索和经验发现最好用的还是那些久经考验的.简单的小工具.系统性能专家 Brendan D. Gregg 在最近的 LinuxCon NA 2014 ...

  2. 25-React事件处理及条件渲染

    Handling Events React元素的事件处理非常类似于对DOM元素添加事件处理,但有一部分的语法不同: 1.React事件使用camelCase(驼峰命名法)来进行命名,而不是小写字母 2 ...

  3. 十一、Swing

    1.Swing常用窗体 (1)JFrame框架窗体 JFrame窗体是一个容器,是Swing程序中各组件的载体. 语法:JFrame jf = new JFrame(title); Container ...

  4. MySql使用show processlist查看正在执行的Sql语句

    今天上班例行的查看了下服务器的运行状况,发现服务器特卡,是mysqld这个进程占用CPU到了99%导致的. 比较好奇是那个程序在使用mysql导致cpu这么高的,通过show processlist命 ...

  5. PHP工作原理

    文章一 :http://blog.csdn.net/21aspnet/article/details/6973405 简介 先看看下面这个过程: 我们从未手动开启过PHP的相关进程,它是随着Apach ...

  6. How To Tune or Test PLSQL Code Performance in Oracle D2k Forms

    You can test or tune your program unit performance in Oracle forms with Ora_Prof package.Suppose you ...

  7. [POJ1681]Painter's Problem(高斯消元,异或方程组,状压枚举)

    题目链接:http://poj.org/problem?id=1681 题意:还是翻格子的题,但是这里有可能出现自由变元,这时候枚举一下就行..(其实这题直接状压枚举就行) /* ━━━━━┒ギリギリ ...

  8. Qt修改xml文件

    <taskpackage styleId="styles/2dc70235-e48b-4e11-b074-3c3f773ae1f3" id="3333" ...

  9. return、 return false的用法

    1. return返回null,起到中断方法执行的效果,只要不return false事件处理函数将会继续执行,表单将提交2. return false,事件处理函数会取消事件,不再继续向下执行.比如 ...

  10. Threejs 使用的3D格式

    3D格式你可以通过任意软件导出(.3ds,dae等),但是threejs 无法使用, 1,http://www.blender.org/ 下载这款开源的3d软件 2,https://github.co ...