unity3d 中控制手机前后摄像头切换

简介: 闲话少说,上代码,google上搜来的代码,不过里面有bug,不能顺利切换,下面的代码是已经修改过的,经测试,可以正常运行。using System;using UnityEngine;using System.

闲话少说,上代码,google上搜来的代码,不过里面有bug,不能顺利切换,下面的代码是已经修改过的,经测试,可以正常运行。

using System;
using UnityEngine;
using System.IO;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {
//public GUITexture mytext;
public UITexture mytext;

private WebCamTexture frontWebcamTexture = null;

private WebCamTexture rearWebcamTexture = null;

private WebCamDevice[] devices;
[HideInInspector]
public WebCamTexture activeCam;

bool flag = false ;
static int cameraIndex = 0 ;

/ 按钮控件 /
public GameObject btn_shot ;
public GameObject btn_change_camera ;

void Awake () {

for( int i = 0 ; i < WebCamTexture.devices.Length ; i++ )
NGUIDebug.Log(WebCamTexture.devices[i].name);

devices = WebCamTexture.devices;

string frontCamName = "";

string rearCamName = "";

for(int i=0; i{

if (devices[i].isFrontFacing)

frontCamName = devices[i].name;

else

rearCamName = devices[i].name;

}

frontWebcamTexture = new WebCamTexture(frontCamName);

rearWebcamTexture = new WebCamTexture(rearCamName);

frontWebcamTexture.Stop();
rearWebcamTexture.Stop();
activeCam = rearWebcamTexture;
//mytext.material.mainTexture = rearWebcamTexture ;
mytext.material.SetTexture("_MainTex", rearWebcamTexture);

activeCam.Play();

}

void Start()

{

/ 事件 /
UIEventListener.Get(btn_shot).onClick += OnShotButtonClick ;
UIEventListener.Get(btn_change_camera).onClick += OnCameraChangeClick ;

NGUIDebug.Log ("Active camera: " + activeCam);

}

public bool HasFrontCamera()
{

if (Application.isEditor)

return false;

return frontWebcamTexture.deviceName != "";

}

public void OnShotButtonClick (GameObject go) {
SavePhoto() ;
}

/ 相机切换 /
public void OnCameraChangeClick (GameObject go) {

if (!HasFrontCamera())

return;

activeCam.Stop ();

if (activeCam == frontWebcamTexture)

{

NGUIDebug.Log ("Switching to rear cam...");

//renderer.material.mainTexture = rearWebcamTexture;
//mytext.material.mainTexture = rearWebcamTexture ;
mytext.material.SetTexture("_MainTex", rearWebcamTexture);
activeCam = rearWebcamTexture;

}

else

{

NGUIDebug.Log ("Switching to front cam...");

//mytext.material.mainTexture = frontWebcamTexture ;
mytext.material.SetTexture("_MainTex", frontWebcamTexture);

activeCam = frontWebcamTexture;

}

NGUIDebug.Log ("... done.");

NGUIDebug.Log("Trying to Play the active WebCamTexture: (" + activeCam + ")");

activeCam.Play();
}

public void ShowCamera()
{
//myCameraTexture.guiTexture.enabled = true;
mytext.enabled = true;
activeCam.Play();
NGUIDebug.Log("-------------show--------->>>>>" + mytext) ;

}

public void HideCamera()
{
NGUIDebug.Log("-------------hide--------->>>>>" + mytext) ;
//myCameraTexture.guiTexture.enabled = false;
mytext.enabled = false;
activeCam.Stop();

}

 public void SavePhoto() {

NGUIDebug.Log("oh finally worked!!!") ;

Texture tex = mytext.material.mainTexture;

Texture2D tx = new Texture2D(activeCam.width, activeCam.height) ;
tx.SetPixels(activeCam.GetPixels());

byte[] byte_photo = tx.EncodeToPNG() ;
NGUIDebug.Log("#####length######################333333" + byte_photo.Length) ;
string photoName = "nimei.png" ;

FileStream fs = new System.IO.FileStream("/mnt/sdcard/DCIM/Camera/nimei.png", System.IO.FileMode.Create);
fs.Write(byte_photo,0,byte_photo.Length);
fs.Close();
}

}



 



更多unity2018的功能介绍请到paws3d爪爪学院查找。

相关文章
|
11月前
|
API Android开发 数据安全/隐私保护
手机虚拟视频替换摄像头,虚拟摄像头手机版,安卓免root虚拟摄像头【python】
这个实现包含四个主要模块:虚拟摄像头核心功能、视频源管理、视频特效处理和主应用界面
|
人工智能 算法 前端开发
OmAgent:轻松构建在终端设备上运行的 AI 应用,赋能手机、穿戴设备、摄像头等多种设备
OmAgent 是 Om AI 与浙江大学联合开源的多模态语言代理框架,支持多设备连接、高效模型集成,助力开发者快速构建复杂的多模态代理应用。
1443 72
OmAgent:轻松构建在终端设备上运行的 AI 应用,赋能手机、穿戴设备、摄像头等多种设备
|
11月前
|
API 开发工具 Android开发
qq虚拟视频插件下载安装手机版, 安卓虚拟视频插件,替换摄像头工具
Xposed入口模块:拦截目标应用的相机调用‌23 Camera1 API处理:通过PreviewCallback替换视频流‌1 Camera2 API适
|
存储 物联网 计算机视觉
|
传感器 vr&ar 图形学
Vuforia⭐️Unity实现对手机陀螺仪的调用
Vuforia⭐️Unity实现对手机陀螺仪的调用
|
算法 数据安全/隐私保护 计算机视觉
基于Aidlux平台实现手机摄像头实时Canny检测
基于Aidlux平台实现手机摄像头实时Canny检测
|
Android开发 iOS开发
【资源共享】手机共享摄像头给电脑用
【资源共享】手机共享摄像头给电脑用
901 0
|
传感器 机器学习/深度学习 编解码
为什么你的手机后置摄像头越来越丑?ECCV2022这篇论文告诉你
为什么你的手机后置摄像头越来越丑?ECCV2022这篇论文告诉你
612 0
|
传感器 编解码 图形学
Unity-使用UnityRemote调试手机Android Studio打印日志
众所周知,人生是一个漫长的流程,不断克服困难,不断反思前进的过程。在这个过程中会产生很多对于人生的质疑和思考,于是我决定将自己的思考,经验和故事全部分享出来,以此寻找共鸣!!!
867 0
|
存储 Android开发 图形学
Unity与Android Studio交互之 ✨ 获取手机权限(存储、录音、相机等)
Unity通过Android Studio 获取手机权限(存储、录音、相机等) 文章目录 Unity通过Android Studio 获取手机权限(存储、录音、相机等) 常用权限 一、动态获取权限流程 二、使用步骤 1.检测当前是否已获取权限 2.检测当前安卓版本 3.申请动态获取权限 总结
Unity与Android Studio交互之 ✨ 获取手机权限(存储、录音、相机等)

热门文章

最新文章