Unity如何调用安卓手机摄像头实现拍照和录像
2017-03-27 09:39
537 查看
1,要想调用摄像头首先要打开摄像头驱动,如果用户允许则可以使用。
2,定义WebCamTexture的变量用于捕获单张照片。
3,连续捕获须启用线程。
实现代码:
将此脚本绑定到MainCamer上
其余也没什么了,哦,对了,记得建个空文件夹用于储存捕获到的照片。
2,定义WebCamTexture的变量用于捕获单张照片。
3,连续捕获须启用线程。
实现代码:
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); } } }
将此脚本绑定到MainCamer上
其余也没什么了,哦,对了,记得建个空文件夹用于储存捕获到的照片。
相关文章推荐
- Unity如何调用安卓手机摄像头实现拍照和录像
- Unity如何调用安卓手机摄像头实现拍照和录像
- Unity使用WebCamTexture 实现手机前后摄像头调用
- HTML5调用手机摄像头拍照的实现思路及代码
- 手机页面调用手机摄像头,录像、摄像、上传已有照片,安卓,苹果通用
- html5调用手机摄像头,实现拍照上传功能
- html5调用手机摄像头,实现拍照上传功能
- 如何让HTML5调用手机摄像头拍照——实践就是一切
- html5调用手机摄像头,实现拍照上传功能
- 求教:如何通过JNI调用手机自身摄像头录像并处理图片?
- html5调用手机摄像头,实现拍照上传功能
- html5调用手机摄像头,实现拍照上传功能
- 如何调用手机自带的照相机APK进行拍照、录像并自动保存照片和录像
- html5调用手机摄像头,实现拍照上传功能
- html5调用手机摄像头,实现拍照上传功能
- VFP调用API来控件USB摄像头,实现拍照或录像。
- 初识安卓之自定义摄像头拍照和调用系统摄像头拍照
- FootStep 40、如何调用S60的系统摄像头功能进行拍照与摄像?
- android调用手机摄像头拍照
- 如何实现android手机摄像头的的自动对焦