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 ...
随机推荐
- 在nginx下去掉ci框架url中的index.php
ci框架默认的url规则中带有应用的入口文件,例如: example.com/index.php/news/article/my_article 在以上URL中带有入口文件index.PHP,这样的U ...
- libevent系列之一——libevent介绍
摘自:http://libevent.org/ libevent概述:一套事件通知库. libevent提供一套机制完成以下功能:当指定的事件发生在file descriptor时或者超时后执行一个回 ...
- redis pool
Redis Pool--Java 配置文件 #redis conf ADDR=127.0.0.1 PORT= AUTH= #session timeout TIMEOUT= MAX_ACTIVE= M ...
- 解决This function or variable may be unsafe
工程名字右键属性-->C/C++-->预处理器-->预处理器定义-->加入:_CRT_SECURE_NO_WARNINGS
- Duilib创建窗口双击标题栏禁止窗口最大化
使用Duilib创建窗口并禁止窗口最大化 第一步: XXXFrame.Create(NULL, _T("XXXFrame"), UI_WNDSTYLE_EX_FRAME, WS_E ...
- Cacti中文版在Centos上的安装
最近老有人问Cacti中文版在哪下载啊怎么安装啊,我在这里一遍给大家讲解了:Cacti中文版在Centos上的安装 1.基本安装 cacti是运作在apache+php+mysql+net-snmp工 ...
- Xcode 创建.a和framework静态库
库介绍 库从本质上来说是一种可执行代码的二进制格式,可以被载入内存中执行.库分静态库和动态库两种. iOS中的静态库有 .a 和 .framework两种形式:动态库有.dylib 和 .framew ...
- Debug不崩溃Release版本崩溃的一种原因
今天有一个工程Debug是正常,Release崩溃,郁闷至极. 研究了一下下午,原因是一个类成员变量没有构造函数中初始化.而Debug版本正好没有问题. 所以定义类成员,一定不能忘记初始化!!
- POJ 1979 Red and Black (红与黑)
POJ 1979 Red and Black (红与黑) Time Limit: 1000MS Memory Limit: 30000K Description 题目描述 There is a ...
- 线程入门之join方法
package com.thread; /** * <join:将某线程加入进来,相当于方法调用,也叫合并某个线程> * <功能详细描述> * * @author 95Yang ...