Unity如何调用安卓手机摄像头实现拍照和录像

1,要想调用摄像头首先要打开摄像头驱动,如果用户允许则可以使用。

2,定义WebCamTexture的变量用于捕获单张照片。

3,连续捕获须启用线程。

脚本绑定到MainCamer

实现代码如下:

  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.IO;  
  4. using System.Runtime.Serialization;  
  5. using System.Runtime .Serialization.Formatters.Binary;  
  6. using System.Threading;  
  7. public class takePhoto : MonoBehaviour   
  8. {  
  9.     public string deviceName;  
  10.     //接收返回的图片数据  
  11.     WebCamTexture tex;  
  12.     void OnGUI()  
  13.     {  
  14.         if (GUI.Button(new Rect(10, 20, 100, 40), "开启摄像头"))  
  15.         {  
  16.             // 调用摄像头  
  17.             StartCoroutine(start());  
  18.         }  
  19.         if(GUI.Button(new Rect(10,70,100,40),"捕获照片"))  
  20.         {  
  21.             //捕获照片  
  22.             tex.Pause();  
  23.             StartCoroutine(getTexture());  
  24.         }  
  25.         if(GUI.Button(new Rect(10,120,100,40),"再次捕获"))  
  26.         {  
  27.             //重新开始  
  28.             tex.Play();  
  29.         }  
  30.         if(GUI.Button(new Rect(120,20,80,40),"录像"))  
  31.         {  
  32.             //录像  
  33.             StartCoroutine(SeriousPhotoes());  
  34.         }  
  35.         if(GUI.Button(new Rect(10,170,100,40),"停止"))  
  36.         {  
  37.             //停止捕获镜头  
  38.             tex.Stop ();  
  39.             StopAllCoroutines();  
  40.         }  
  41.         if(tex!=null)  
  42.         {  
  43.             // 捕获截图大小               —距X左屏距离   |   距Y上屏距离    
  44.             GUI.DrawTexture(new Rect(Screen.width/2-150,Screen.height/2-190,280,200),tex);   
  45.         }  
  46.     }  
  47.     /// <summary>  
  48.     /// 捕获窗口位置  
  49.     /// </summary>  
  50.     public IEnumerator start()  
  51.     {  
  52.         yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);  
  53.         if (Application.HasUserAuthorization(UserAuthorization.WebCam))  
  54.         {  
  55.             WebCamDevice[] devices = WebCamTexture.devices;  
  56.             deviceName= devices[0].name;  
  57.             tex=new WebCamTexture(deviceName,300,300,12);  
  58.             tex.Play();  
  59.         }  
  60.     }  
  61.     /// <summary>  
  62.     /// 获取截图  
  63.     /// </summary>  
  64.     /// <returns>The texture.</returns>  
  65.     public IEnumerator getTexture()  
  66.     {  
  67.         yield return new WaitForEndOfFrame();  
  68.         Texture2D t=new Texture2D(400,300);  
  69.         t.ReadPixels( new Rect(Screen.width/2-200,Screen.height/2-50,360,300),0,0,false);  
  70.                                          //距X左的距离        距Y屏上的距离  
  71.        // t.ReadPixels(new Rect(220, 180, 200, 180), 0, 0, false);  
  72.         t.Apply();  
  73.         byte[] byt=t.EncodeToPNG();  
  74.         File.WriteAllBytes(Application.dataPath+"/Photoes/"+Time.time+".jpg",byt);  
  75.         tex.Play();  
  76.     }  
  77.     /// <summary>  
  78.     /// 连续捕获照片  
  79.     /// </summary>  
  80.     /// <returns>The photoes.</returns>  
  81.     public IEnumerator SeriousPhotoes()  
  82.     {  
  83.         while (true)  
  84.         {  
  85.             yield return new WaitForEndOfFrame();  
  86.             Texture2D t = new Texture2D(400, 300, TextureFormat.RGB24, true);  
  87.             t.ReadPixels(new Rect(Screen.width/2-180,Screen.height/2-50,360,300), 0, 0, false);  
  88.             t.Apply();  
  89.             print(t);  
  90.             byte[] byt = t.EncodeToPNG();  
  91.             File.WriteAllBytes(Application.dataPath + "/MulPhotoes/" + Time.time.ToString().Split('.')[0] + "_" + Time.time.ToString().Split('.')[1] + ".png", byt);  
  92.             Thread.Sleep(300);  
  93.         }  
  94.     }  
  95. }  

  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值