找回密码
 立即注册
查看: 5024|回复: 72

[脚本] Unity3D调用摄像头并保持为图片然后加载到场景中代码 拍照照相代码

  [复制链接]
发表于 2013-4-3 16:45 | 显示全部楼层 |阅读模式
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.IO;


  4. public class CameraController : MonoBehaviour {
  5.     public string deviceName;
  6.   public  WebCamTexture webTex;
  7.   public GameObject objCameraController;
  8.   public GameObject objAvatar;

  9.   public GameObject ImgAtlas;
  10.   public string strTempTime="";
  11.     // Use this for initialization
  12.     void Start () {
  13.    
  14.     }
  15.    
  16.     // Update is called once per frame
  17.     void Update () {
  18.         if(strTempTime!="")
  19.         {
  20.          //   GameObject.Find("pig5").GetComponent<UITexture>().mainTexture = Resources.Load(strTempTime.ToString(), typeof(Texture)) as Texture;
  21.         //    print("44444:"+strTempTime);
  22.         }
  23.         
  24.     }
  25.     /// <summary>
  26.     /// 调用摄像机
  27.     /// </summary>
  28.     /// <returns></returns>
  29.     IEnumerator CallTheCamera()
  30.     {
  31.         yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
  32.         if(Application.HasUserAuthorization(UserAuthorization.WebCam))
  33.         {
  34.             WebCamDevice[] devices = WebCamTexture.devices;
  35.             deviceName = devices[0].name;
  36.             //摄像机摄像的区域
  37.             webTex = new WebCamTexture(deviceName, 400, 300, -112);
  38.             GameObject.Find("CameraTexture").GetComponent<UITexture>().mainTexture = webTex;
  39.             webTex.Play();
  40.         }

  41.     }
  42.     /// <summary>
  43.     /// 获取并保存texture
  44.     /// </summary>
  45.     /// <returns></returns>
  46.     IEnumerator GetTexture2D()
  47.     {
  48.         print(GameObject.Find("CameraTexture").transform.localPosition);
  49.         yield return new WaitForEndOfFrame();
  50.         Texture2D t = new Texture2D(GameObject.Find("CameraTexture").GetComponent<UITexture>().mainTexture.width, GameObject.Find("CameraTexture").GetComponent<UITexture>().mainTexture.height);
  51.         t.ReadPixels(new Rect(15, 177, 406.5f, 281.6f), 0, 0, false);
  52.         t.Apply();
  53.         //把图片数据转换为byte数组
  54.         byte[] byt = t.EncodeToPNG();
  55.         //然后保存为图片
  56.          strTempTime = Time.time.ToString();
  57.         print("11111:"+strTempTime);
  58.          File.WriteAllBytes(Application.dataPath + "/Resources/" + strTempTime + ".jpg", byt);
  59.         print("22222"+strTempTime);
  60.         //GameObject atlasPrefab = Resources.Load("TempAtlas") as GameObject;
  61.        // Instantiate(ImgAtlas) as GameObject;
  62.     //    tempA.GetComponent<UIAtlas>().spriteList.Add(
  63. //
  64.         UnityEditor.AssetDatabase.Refresh();
  65.         //yield return new WaitForSeconds(1);
  66.         GameObject.Find("pig5").GetComponent<UITexture>().mainTexture = Resources.Load(strTempTime.ToString(), typeof(Texture)) as Texture;
  67.         print("33333"+strTempTime);
  68.    //     NGUITools.AddSprite(tempA, tempA.GetComponent<UIAtlas>(), "tempSprite");
  69.      //   UIAtlas atlas = (Instantiate(ImgAtlas) as GameObject).GetComponent<UIAtlas>();
  70.      //   Texture text = Resources.Load(Application.dataPath + "/Resources/" + strTempTime + ".jpg", typeof(Texture)) as Texture;
  71.       //  print(atlas.ToString());
  72.         //UISlicedSprite ssprite = NGUITools.AddWidget<UISlicedSprite>(Game);
  73.        // GameObject.Find("pig5").GetComponent<UISlicedSprite>().s
  74.         //UIAtlas atlas = Resources.Load(Application.dataPath + "/test/" + "Camera Atlas", typeof(UIAtlas)) as UIAtlas;
  75.         //print(atlas.ToString());
  76.       //  objAvatar.GetComponent<UISlicedSprite>().spriteName = atlas.name;//this.gameObject.GetComponent<UISlicedSprite>().spriteName;
  77.         //objCameraController.SetActive(false);
  78.     }
  79.     void CameraStart()
  80.     {
  81.         StartCoroutine("CallTheCamera");
  82.     }
  83.     void CameraSStart()
  84.     {
  85.         StartCoroutine("GetTexture2D");
  86.     }
  87.     void CameraRestart()
  88.     {
  89.         webTex.Play();
  90.     }
  91.     void CameraPause()
  92.     {
  93.         webTex.Pause();
  94.     }

  95.     void ClickCameraBtn()
  96.     {
  97.         objCameraController.SetActive(true);
  98.     }
  99. }
复制代码

评分

参与人数 1鲜花 +1 收起 理由
chaosvia + 1 赞一个!

查看全部评分

发表于 2013-8-7 13:04 | 显示全部楼层
Assets/GUI/paizao.cs(41,59): error CS0246: The type or namespace name `UITexture' could not be found. Are you missing a using directive or an assembly reference?
Assets/GUI/paizao.cs(54,83): error CS0246: The type or namespace name `UITexture' could not be found. Are you missing a using directive or an assembly reference?
Assets/GUI/paizao.cs(54,161): error CS0246: The type or namespace name `UITexture' could not be found. Are you missing a using directive or an assembly reference?
Assets/GUI/paizao.cs(54,193): error CS1502: The best overloaded method match for `UnityEngine.Texture2D.Texture2D(int, int)' has some invalid arguments
Assets/GUI/paizao.cs(54,193): error CS1503: Argument `#1' cannot convert `object' expression to type `int'
Assets/GUI/paizao.cs(70,46): error CS0246: The type or namespace name `UITexture' could not be found. Are you missing a using directive or an assembly reference?

这些是什么情况{:5_385:}
发表于 2014-4-26 10:19 | 显示全部楼层
学习学习了
发表于 2017-6-14 11:59 | 显示全部楼层
楼主是超人
发表于 2017-6-14 11:31 | 显示全部楼层
好帖就是要顶
发表于 2017-6-14 11:10 | 显示全部楼层
真心顶
发表于 2017-6-14 11:21 | 显示全部楼层
很好哦
发表于 2017-6-14 11:29 | 显示全部楼层
LZ真是人才
发表于 2017-6-18 21:59 | 显示全部楼层
楼主是超人
发表于 2017-6-18 22:05 | 显示全部楼层
真心顶
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-11-23 19:56 , Processed in 0.106048 second(s), 28 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表