日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/8/1 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Unity如何调用安卓手机摄像头实现拍照和录像 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1,要想調用攝像頭首先要打開攝像頭驅動,如果用戶允許則可以使用。

2,定義WebCamTexture的變量用于捕獲單張照片。

3,連續捕獲須啟用線程。

腳本綁定到MainCamer

實現代碼如下:

  • using?UnityEngine;??
  • using?System.Collections;??
  • using?System.IO;??
  • using?System.Runtime.Serialization;??
  • using?System.Runtime?.Serialization.Formatters.Binary;??
  • using?System.Threading;??
  • public?class?takePhoto?:?MonoBehaviour???
  • {??
  • ????public?string?deviceName;??
  • ????//接收返回的圖片數據??
  • ????WebCamTexture?tex;??
  • ????void?OnGUI()??
  • ????{??
  • ????????if?(GUI.Button(new?Rect(10,?20,?100,?40),?"開啟攝像頭"))??
  • ????????{??
  • ????????????//?調用攝像頭??
  • ????????????StartCoroutine(start());??
  • ????????}??
  • ????????if(GUI.Button(new?Rect(10,70,100,40),"捕獲照片"))??
  • ????????{??
  • ????????????//捕獲照片??
  • ????????????tex.Pause();??
  • ????????????StartCoroutine(getTexture());??
  • ????????}??
  • ????????if(GUI.Button(new?Rect(10,120,100,40),"再次捕獲"))??
  • ????????{??
  • ????????????//重新開始??
  • ????????????tex.Play();??
  • ????????}??
  • ????????if(GUI.Button(new?Rect(120,20,80,40),"錄像"))??
  • ????????{??
  • ????????????//錄像??
  • ????????????StartCoroutine(SeriousPhotoes());??
  • ????????}??
  • ????????if(GUI.Button(new?Rect(10,170,100,40),"停止"))??
  • ????????{??
  • ????????????//停止捕獲鏡頭??
  • ????????????tex.Stop?();??
  • ????????????StopAllCoroutines();??
  • ????????}??
  • ????????if(tex!=null)??
  • ????????{??
  • ????????????//?捕獲截圖大小???????????????—距X左屏距離???|???距Y上屏距離????
  • ????????????GUI.DrawTexture(new?Rect(Screen.width/2-150,Screen.height/2-190,280,200),tex);???
  • ????????}??
  • ????}??
  • ????///?<summary>??
  • ????///?捕獲窗口位置??
  • ????///?</summary>??
  • ????public?IEnumerator?start()??
  • ????{??
  • ????????yield?return?Application.RequestUserAuthorization(UserAuthorization.WebCam);??
  • ????????if?(Application.HasUserAuthorization(UserAuthorization.WebCam))??
  • ????????{??
  • ????????????WebCamDevice[]?devices?=?WebCamTexture.devices;??
  • ????????????deviceName=?devices[0].name;??
  • ????????????tex=new?WebCamTexture(deviceName,300,300,12);??
  • ????????????tex.Play();??
  • ????????}??
  • ????}??
  • ????///?<summary>??
  • ????///?獲取截圖??
  • ????///?</summary>??
  • ????///?<returns>The?texture.</returns>??
  • ????public?IEnumerator?getTexture()??
  • ????{??
  • ????????yield?return?new?WaitForEndOfFrame();??
  • ????????Texture2D?t=new?Texture2D(400,300);??
  • ????????t.ReadPixels(?new?Rect(Screen.width/2-200,Screen.height/2-50,360,300),0,0,false);??
  • ?????????????????????????????????????????//距X左的距離????????距Y屏上的距離??
  • ???????//?t.ReadPixels(new?Rect(220,?180,?200,?180),?0,?0,?false);??
  • ????????t.Apply();??
  • ????????byte[]?byt=t.EncodeToPNG();??
  • ????????File.WriteAllBytes(Application.dataPath+"/Photoes/"+Time.time+".jpg",byt);??
  • ????????tex.Play();??
  • ????}??
  • ????///?<summary>??
  • ????///?連續捕獲照片??
  • ????///?</summary>??
  • ????///?<returns>The?photoes.</returns>??
  • ????public?IEnumerator?SeriousPhotoes()??
  • ????{??
  • ????????while?(true)??
  • ????????{??
  • ????????????yield?return?new?WaitForEndOfFrame();??
  • ????????????Texture2D?t?=?new?Texture2D(400,?300,?TextureFormat.RGB24,?true);??
  • ????????????t.ReadPixels(new?Rect(Screen.width/2-180,Screen.height/2-50,360,300),?0,?0,?false);??
  • ????????????t.Apply();??
  • ????????????print(t);??
  • ????????????byte[]?byt?=?t.EncodeToPNG();??
  • ????????????File.WriteAllBytes(Application.dataPath?+?"/MulPhotoes/"?+?Time.time.ToString().Split('.')[0]?+?"_"?+?Time.time.ToString().Split('.')[1]?+?".png",?byt);??
  • ????????????Thread.Sleep(300);??
  • ????????}??
  • ????}??
  • } ?

  • 總結

    以上是生活随笔為你收集整理的Unity如何调用安卓手机摄像头实现拍照和录像的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。