using UnityEngine;
using System.Collections;
using System.IO;
public class CameraController : MonoBehaviour {
public string deviceName;
public WebCamTexture webTex;
public GameObject objCameraController;
public GameObject objAvatar;
public GameObject ImgAtlas;
public string strTempTime="";
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(strTempTime!="")
{
// GameObject.Find("pig5").GetComponent().mainTexture = Resources.Load(strTempTime.ToString(), typeof(Texture)) as Texture;
// print("44444:"+strTempTime);
}
}
///
/// 调用摄像机
///
///
IEnumerator CallTheCamera()
{
yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
if(Application.HasUserAuthorization(UserAuthorization.WebCam))
{
WebCamDevice[] devices = WebCamTexture.devices;
deviceName = devices[0].name;
//摄像机摄像的区域
webTex = new WebCamTexture(deviceName, 400, 300, -112);
GameObject.Find("CameraTexture").GetComponent().mainTexture = webTex;
webTex.Play();
}
}
///
/// 获取并保存texture
///
///
IEnumerator GetTexture2D()
{
print(GameObject.Find("CameraTexture").transform.localPosition);
yield return new WaitForEndOfFrame();
Texture2D t = new Texture2D(GameObject.Find("CameraTexture").GetComponent().mainTexture.width, GameObject.Find("CameraTexture").GetComponent().mainTexture.height);
t.ReadPixels(new Rect(15, 177, 406.5f, 281.6f), 0, 0, false);
t.Apply();
//把图片数据转换为byte数组
byte[] byt = t.EncodeToPNG();
//然后保存为图片
strTempTime = Time.time.ToString();
print("11111:"+strTempTime);
File.WriteAllBytes(Application.dataPath + "/Resources/" + strTempTime + ".jpg", byt);
print("22222"+strTempTime);
//GameObject atlasPrefab = Resources.Load("TempAtlas") as GameObject;
// Instantiate(ImgAtlas) as GameObject;
// tempA.GetComponent().spriteList.Add(
//
UnityEditor.AssetDatabase.Refresh();
//yield return new WaitForSeconds(1);
GameObject.Find("pig5").GetComponent().mainTexture = Resources.Load(strTempTime.ToString(), typeof(Texture)) as Texture;
print("33333"+strTempTime);
// NGUITools.AddSprite(tempA, tempA.GetComponent(), "tempSprite");
// UIAtlas atlas = (Instantiate(ImgAtlas) as GameObject).GetComponent();
// Texture text = Resources.Load(Application.dataPath + "/Resources/" + strTempTime + ".jpg", typeof(Texture)) as Texture;
// print(atlas.ToString());
//UISlicedSprite ssprite = NGUITools.AddWidget(Game);
// GameObject.Find("pig5").GetComponent().s
//UIAtlas atlas = Resources.Load(Application.dataPath + "/test/" + "Camera Atlas", typeof(UIAtlas)) as UIAtlas;
//print(atlas.ToString());
// objAvatar.GetComponent().spriteName = atlas.name;//this.gameObject.GetComponent().spriteName;
//objCameraController.SetActive(false);
}
void CameraStart()
{
StartCoroutine("CallTheCamera");
}
void CameraSStart()
{
StartCoroutine("GetTexture2D");
}
void CameraRestart()
{
webTex.Play();
}
void CameraPause()
{
webTex.Pause();
}
void ClickCameraBtn()
{
objCameraController.SetActive(true);
}
}
Unity3D调用摄像头并保持为图片然后加载到场景中代码 拍照照相代码
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 在vc中先引入一个头文件, 用于ios9下判断是否有访问系统相册权限 先签这俩协议 写个属性 写个懒加载, 避免...
- 最近的一个小项目需要用到移动端调用手机摄像头拍照,经多方查找验证,最后选定用file域调用手机摄像头拍照,转成ba...
- 如果不是手机掉水里,如果不是换手机,如果不是高清后置摄像头对脸测像素。也许还活在美颜相机滤镜里自己肌白无暇而无法自...