移动AR技术后置摄像头实时获取
2015-09-01 18:13
253 查看
Unity3D教程手册 ·
2014-12-11 18:01
很多人可能都做过摄像头方面的开发,最近我也是在看增强现实(Augmented Reality 简称AR)关于摄像头的代码,一般都是在PC端来做,开启程序之后,摄像头会捕捉画面,然后将摄像头捕捉到的画面实时地显示在显示终端。在AR上开发摄像头的捕捉基本上都是通过WebCamTexture类的运用,使用WebCamTexture我们可以很方便地使用摄像头捕捉到的数据实现对象的动态贴图。但是在移动智能终端(以我自己的android手机为例)涉及后置摄像的获取操作,材质的实时更新。
下面讲解一个demo实例,一步步手把手教你,相信看完之后你一定有所收获。
1.创建一个新场景,然后在场景中添加一个GUITexture,添加完成之后重命名myCameraTexture(看你自己的爱好,不喜欢重命名的也行)
2.创建一个CameraScript.cs文件,然后开始在这个类文件中写代码。。。
using UnityEngine;
using System.Collections;
using System.IO;
using System;
public class CameraScript : MonoBehaviour {
// Use this for initialization
public GUITexture myCameraTesture;
private WebCamTexture webCameraTexture;
void Start () {
//检查当前设备有多少个可以使用的摄像机
for (int cameraIndex = 0; cameraIndex < WebCamTexture.devices.Length;cameraIndex++ )
{
//获取移动手机的后置摄像机
if(!WebCamTexture.devices[cameraIndex].isFrontFacing){
//这个你们可以自己测试一下,我没有装安卓虚拟机,每次都要导入到手机之后才能测试 你们可以自己试试
//webCameraTexture = new WebCamTexture(cameraIndex, Screen.width, Screen.height);
webCameraTexture = new WebCamTexture(cameraIndex, 200, 200);
//本地坐标变换
myCameraTesture.transform.localScale = new Vector3(-1, -1, 1);
}
}
myCameraTesture.texture = webCameraTexture;
//开始摄像
webCameraTexture.Play();
}
// Update is called once per frame
void Update () {
}
public void ShowCamera()
{
myCameraTesture.guiTexture.enabled = true;
webCameraTexture.Play();
}
public void HideCamera()
{
myCameraTesture.guiTexture.enabled = false;
webCameraTexture.Play();
}
void OnGUI()
{
//添加一个按钮来控制摄像机的开和关
if(GUI.Button(new Rect(0,0,100,100),"ON/OFF")){
if (webCameraTexture.isPlaying)
{
this.HideCamera();
}
else
{
this.ShowCamera();
}
}
if(GUI.Button(new Rect(0,110,100,100),"Quit")){
//退出系统
Application.Quit();
}
}
}
3.将CameraScript.cs绑定到Main Camera上,因为这个类定义的有个公共变量,所以直接拖动myCameraTexture到右边属性面板的Public变量上即可
3.发布设置,这里不再多说,因为之前有一篇文章已经很详细的讲解了Untiy发布安卓的步骤,而且将的也比较详细,如果还有不懂的可以看看,这里主要说一点, 就是在player setting中Resolution and Presentation点开之后有个
Default Orientation 我选择的是Landscape Right ,你们可以自己试试选择left。。。是什么情况
4.然后将发布完成的apk文件安装到自己的安卓手机测试.............一切如你所愿,。。。这是移动智能终端AR最基本的视频获取。代码虽然简单但是值得你学习...
【干货】移动AR技术后置摄像头实时获取
技术摄像头Unity3D教程手册 ·
2014-12-11 18:01
很多人可能都做过摄像头方面的开发,最近我也是在看增强现实(Augmented Reality 简称AR)关于摄像头的代码,一般都是在PC端来做,开启程序之后,摄像头会捕捉画面,然后将摄像头捕捉到的画面实时地显示在显示终端。在AR上开发摄像头的捕捉基本上都是通过WebCamTexture类的运用,使用WebCamTexture我们可以很方便地使用摄像头捕捉到的数据实现对象的动态贴图。但是在移动智能终端(以我自己的android手机为例)涉及后置摄像的获取操作,材质的实时更新。
下面讲解一个demo实例,一步步手把手教你,相信看完之后你一定有所收获。
1.创建一个新场景,然后在场景中添加一个GUITexture,添加完成之后重命名myCameraTexture(看你自己的爱好,不喜欢重命名的也行)
2.创建一个CameraScript.cs文件,然后开始在这个类文件中写代码。。。
using UnityEngine;
using System.Collections;
using System.IO;
using System;
public class CameraScript : MonoBehaviour {
// Use this for initialization
public GUITexture myCameraTesture;
private WebCamTexture webCameraTexture;
void Start () {
//检查当前设备有多少个可以使用的摄像机
for (int cameraIndex = 0; cameraIndex < WebCamTexture.devices.Length;cameraIndex++ )
{
//获取移动手机的后置摄像机
if(!WebCamTexture.devices[cameraIndex].isFrontFacing){
//这个你们可以自己测试一下,我没有装安卓虚拟机,每次都要导入到手机之后才能测试 你们可以自己试试
//webCameraTexture = new WebCamTexture(cameraIndex, Screen.width, Screen.height);
webCameraTexture = new WebCamTexture(cameraIndex, 200, 200);
//本地坐标变换
myCameraTesture.transform.localScale = new Vector3(-1, -1, 1);
}
}
myCameraTesture.texture = webCameraTexture;
//开始摄像
webCameraTexture.Play();
}
// Update is called once per frame
void Update () {
}
public void ShowCamera()
{
myCameraTesture.guiTexture.enabled = true;
webCameraTexture.Play();
}
public void HideCamera()
{
myCameraTesture.guiTexture.enabled = false;
webCameraTexture.Play();
}
void OnGUI()
{
//添加一个按钮来控制摄像机的开和关
if(GUI.Button(new Rect(0,0,100,100),"ON/OFF")){
if (webCameraTexture.isPlaying)
{
this.HideCamera();
}
else
{
this.ShowCamera();
}
}
if(GUI.Button(new Rect(0,110,100,100),"Quit")){
//退出系统
Application.Quit();
}
}
}
3.将CameraScript.cs绑定到Main Camera上,因为这个类定义的有个公共变量,所以直接拖动myCameraTexture到右边属性面板的Public变量上即可
3.发布设置,这里不再多说,因为之前有一篇文章已经很详细的讲解了Untiy发布安卓的步骤,而且将的也比较详细,如果还有不懂的可以看看,这里主要说一点, 就是在player setting中Resolution and Presentation点开之后有个
Default Orientation 我选择的是Landscape Right ,你们可以自己试试选择left。。。是什么情况
4.然后将发布完成的apk文件安装到自己的安卓手机测试.............一切如你所愿,。。。这是移动智能终端AR最基本的视频获取。代码虽然简单但是值得你学习...
相关文章推荐
- 64位win7安装mysqlzip包的形式
- 整理开发第三方管理appid
- Python 随手记
- 声明fakeHead
- 数据结构:线索树之基本例程(1)
- cocos2dlua-场景
- 笔记:用标准c写 com dll
- 云计算设计模式(二十一)——Sharding分片模式
- .net接口开发远程调试
- Ubuntu开机加载自定义脚本
- Hibernate之——动态表名映射
- std::bind1st与std::bind2nd的区别
- MySQL 性能优化的最佳20多条经验分享
- 云计算设计模式(二十)——调度程序代理管理者模式
- javascript中for/in循环及使用技巧
- 黑马程序员——OC单例模式入门
- Tomcat Error Page配置
- Android 手势检测实战 打造支持缩放平移的图片预览效果(下)
- mysql cluster原理
- 使用NSClassFromString