Unity3D WebCamTexture 调用外部摄像头
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作为背景,可以自行调节缩放和位置。
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();
}
}
}
Unity3D WebCamTexture 调用外部摄像头的更多相关文章
- Unity3D中调用外接摄像头,并保存为图片文件
http://bbs.9ria.com/thread-170539-1-1.html 项目要求调用摄像头,并且把图像保存下来,上传到服务器. 这里有几个难点,调用摄像头是很简单的,unity已经提供好 ...
- Unity调用外部摄像头,全屏显示摄像头画面
有两种方法,常用的是GUI方法,代码如下: public class CameraTest : MonoBehaviour { WebCamTexture camTexture; void Start ...
- System.Diagnostics.Process 启动进程资源或调用外部的命令的使用
经常看到一些程序在保存为一个txt,或者excel的文件的时候,保存完毕立即打开, 启动程序或打开文件的代码 System.Diagnostics.Process.Start(System.IO.Pa ...
- python调用外部子进程,通过管道实现异步标准输入和输出的交互
我们通常会遇到这样的需求:通过C++或其他较底层的语言实现了一个复杂的功能模块,需要搭建一个基于Web的Demo,方法查询数据.由于Python语言的强大和简洁,其用来搭建Demo非常合适,Flask ...
- 关于js调用外部部署的web api
没想到多年之后我还得继续写这些东西.... 瀑布汗~ 最近不得不开始研究用web api MVC的项目中,在js文件里,实现点击一个按钮调用外部发布好的api,再从api把值回传给js页面,跳转. 经 ...
- windows下调用外部exe程序 SHELLEXECUTEINFO
本文主要介绍两种在windows下调用外部exe程序的方法: 1.使用SHELLEXECUTEINFO 和 ShellExecuteEx SHELLEXECUTEINFO 结构体的定义如下: type ...
- 在Salesforce中调用外部系统所提供的的Web Service
这里需要提供外部service所对应的WSDL文件(Salesforce只支持从本地上传),并且提供的WSDL文件有如下两点要求: 1):wsdl 文件只能有一个binding,Salesforce是 ...
- Unity3D脚本调用Objective C代码实现游戏内购买
0.开篇吐槽: 一年之内从WP转到iOS,又从iOS转到U3D,真心伤不起. 1.Unity3D脚本调用OC代码的原理: 其实也没啥神秘的,因为OC是和C互通的 ,C#又可以通过DllImport的形 ...
- QTP学习一添加默认的注释及调用外部vbs文件
一.添加默认注释 1.新建一个TXT文档,将要添加的注释写在文档中 2.再将文档名改为:ActionTemplate.mst 3.将文件放到QTP安装目录(默认为:C:\Program Files\H ...
随机推荐
- [webkit移动开发笔记]之如何去除android上a标签产生的边框
去年年底,做完最后一个项目就可以开开心心回家,可是在测试阶段,发现了不少bug,为了不影响回家时间,加班加点也要解决这些问题,这里算是工作回忆,也算是工作的一点小总结. 在ios4+和android2 ...
- 微信网页获取openId
<%@ WebHandler Language="C#" Class="UserAuth" %> public class UserAuth : I ...
- maven之一——多模块项目构建
参考这个帖子: http://www.cnblogs.com/xdp-gacl/p/4242221.html
- JLINK仿真器与ST-LINK仿真器的安装与配置.pdf
JLINK仿真器与ST-LINK仿真器的安装与配置.pdf 工欲善其事,......stm32的开发环境搭建 观看地址 说到仿真器,首先要了解一下JTAG. JTAG协议 JTAG(Joint Te ...
- 关于nodejs4.0 npm乱码以及离线全局安装时要注意的问题
近期nodejs更新的到了4.~版本,融合了io.js,升级了v8引擎,对于之前的操作有些变化,在这里提醒大家注意: 1.npm在install和remove时发生乱码,并报出"runTop ...
- BZOJ3687:bitset STL
[bzoj3687][FJ2014集训]简单题 2014年9月14日1,8212 [题目描述]小呆开始研究集合论了,他提出了关于一个数集四个问题:1. 子集的异或和的算术和.2. 子集的异或和的异或和 ...
- 六、IO流——文件
IO流1.字节流(InputStream.OutputStream)2.字符流 (Reader.Writer)3.缓冲流(BufferedInputStream.BufferedOutputStrea ...
- 如何快速清除.svn文件
Windows Registry Editor Version 5.00[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shell\清除SVN信息] @=&qu ...
- C语言实现数组快速排序(含对算法的详细解释)
/* 说明: 代码参考过网上代码,但分析为个人原创,本贴重在说明快速排序算法的思想和运行过程. */ 代码部分: #include<stdio.h> #include<stdlib. ...
- Python串口编程
python的串口网上有很多例子,这里了只是把认为好的整理到一起. 首先,应该安装serial模块,还能开始后续的操作.我用的python2.6,serial模块可以在这里下载安装serial模块下载 ...