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

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


public class CameraController : MonoBehaviour {
    public stringdeviceName;
  public  WebCamTexturewebTex;
  public GameObjectobjCameraController;
  public GameObjectobjAvatar;

  public GameObjectImgAtlas;
  public stringstrTempTime="";
    // Use this forinitialization
    void Start () {
   
    }
   
    // Update is called once perframe
    void Update () {
       if(strTempTime!="")
       {
       //  GameObject.Find("pig5").GetComponent().mainTexture= Resources.Load(strTempTime.ToString(), typeof(Texture)) asTexture;
       //   print("44444:"+strTempTime);
       }
       
    }
    ///
    /// 调用摄像机
    ///
    ///
    IEnumeratorCallTheCamera()
    {
       yield returnApplication.RequestUserAuthorization(UserAuthorization.WebCam);
       if(Application.HasUserAuthorization(UserAuthorization.WebCam))
       {
         WebCamDevice[] devices =WebCamTexture.devices;
          deviceName =devices[0].name;
         //摄像机摄像的区域
          webTex = newWebCamTexture(deviceName, 400, 300, -112);
         GameObject.Find("CameraTexture").GetComponent().mainTexture= webTex;
         webTex.Play();
       }

    }
    ///
    /// 获取并保存texture
    ///
    ///
    IEnumeratorGetTexture2D()
    {
       print(GameObject.Find("CameraTexture").transform.localPosition);
       yield returnnew WaitForEndOfFrame();
       Texture2D t =newTexture2D(GameObject.Find("CameraTexture").GetComponent().mainTexture.width,GameObject.Find("CameraTexture").GetComponent().mainTexture.height);
       t.ReadPixels(newRect(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);
       //GameObjectatlasPrefab = Resources.Load("TempAtlas") as GameObject;
      // Instantiate(ImgAtlas) as GameObject;
    //   tempA.GetComponent().spriteList.Add(
//
       UnityEditor.AssetDatabase.Refresh();
       //yieldreturn new WaitForSeconds(1);
       GameObject.Find("pig5").GetComponent().mainTexture= Resources.Load(strTempTime.ToString(), typeof(Texture)) asTexture;
       print("33333"+strTempTime);
   //    NGUITools.AddSprite(tempA,tempA.GetComponent(), "tempSprite");
    //  UIAtlas atlas = (Instantiate(ImgAtlas) asGameObject).GetComponent();
    //  Texture text =Resources.Load(Application.dataPath + "/Resources/" + strTempTime +".jpg", typeof(Texture)) as Texture;
     //  print(atlas.ToString());
       //UISlicedSpritessprite = NGUITools.AddWidget(Game);
      //GameObject.Find("pig5").GetComponent().s
       //UIAtlasatlas = Resources.Load(Application.dataPath + "/test/" + "CameraAtlas", 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();
    }
 

  voidClickCameraBtn()
    {
       objCameraController.SetActive(true);
    }
}

原文地址:http://blog.sina.com.cn/s/blog_697b1b8c0101dqb3.html

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值