unity调用摄像头进行拍照和保存照片
2016-12-22 21:49
525 查看
using UnityEngine; using System.Collections; using System.IO; using System.Runtime.Serialization; using System.Runtime .Serialization.Formatters.Binary; using System.Threading; public class capture_video_from_camera_device : MonoBehaviour { public string deviceName; //接收返回的图片数据 WebCamTexture tex; void OnGUI() { if (GUI.Button(new Rect(10, 20, 100, 40), "start")) { // 调用摄像头 StartCoroutine(start()); } if(GUI.Button(new Rect(10,70,100,40),"Pause")) { //捕获照片 tex.Pause(); StartCoroutine(getTexture()); } if(GUI.Button(new Rect(10,120,100,40),"replay")) { //重新开始 tex.Play(); } if(GUI.Button(new Rect(120,20,80,40),"record")) { //录像 StartCoroutine(SeriousPhotoes()); } if(GUI.Button(new Rect(10,170,100,40),"stop")) { //停止捕获镜头 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); } } }
相关文章推荐
- android开发(15) 调用摄像头拍照,保存在照片到数据库。
- html5实现摄像头拍照并使用java进行照片保存
- FLASH 调用摄像头 JS中方法触发拍照,并将照片保存到本地文件夹
- 如何调用手机自带的照相机APK进行拍照、录像并自动保存照片和录像
- android开发(15) 调用摄像头拍照,保存在照片到数据库。
- android调用系统相机拍照与打开相册选择照片进行截取保存笔记
- android 调用摄像头进行拍照,并对照片进行裁剪,然后显示。
- java 如何调用摄像头并拍照保存照片
- 教你如何用Silverlight调用摄像头和麦克风,拍照,保存照片
- FootStep 40、如何调用S60的系统摄像头功能进行拍照与摄像?
- Android调用系统相机拍照、从相册中取照片,裁剪,并保存到手机SD卡中,展示到界面
- 实现主流软件的调用系统相机拍照 取得照片 并进行加工
- matlab调用笔记本摄像头拍照并连续保存
- Android调用系统相机拍照保存照片很小解决方案
- FootStep 40、如何调用S60的系统摄像头功能进行拍照与摄像?
- VB 调用摄像头拍照,保存。
- Unity调用手机摄像头进行摄像,并显示
- win8 metro 调用摄像头拍摄照片并将照片保存在对应的位置
- 实现主流软件的调用系统相机拍照 取得照片 并进行加工