1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > Unity 调用摄像头拍照保存

Unity 调用摄像头拍照保存

时间:2024-06-07 08:18:17

相关推荐

Unity 调用摄像头拍照保存

unity在制作很多大屏互动时候都会用到摄像头拍照或者保存图片,摄像头拍照方式也有多种,仅记录一下。

using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;using System.IO;public class CameraTest : MonoBehaviour{//摄像头图像类,继承自textureWebCamTexture tex;public Image WebCam;public MeshRenderer ma;public Button saveImage;public RawImage bgimage_02;// public RawImage bgimage_03;int i;void Start(){//开启协程,获取摄像头图像数据StartCoroutine(OpenCamera());saveImage.onClick.AddListener(SaveImage);}// Update is called once per framevoid Update(){}IEnumerator OpenCamera(){//等待用户允许访问yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);//如果用户允许访问,开始获取图像 if (Application.HasUserAuthorization(UserAuthorization.WebCam)){//先获取设备WebCamDevice[] device = WebCamTexture.devices;string deviceName = device[0].name;//然后获取图像tex = new WebCamTexture(deviceName);//将获取的图像赋值ma.material.mainTexture = tex;//开始实施获取tex.Play();}}private void SaveImage(){//在上一段代码中加入该方法Save(tex);i += 1;}public void Save(WebCamTexture t){Texture2D t2d = new Texture2D(t.width, t.height, TextureFormat.ARGB32, true);//将WebCamTexture 的像素保存到texture2D中t2d.SetPixels(t.GetPixels());//t2d.ReadPixels(new Rect(200,200,200,200),0,0,false);t2d.Apply();//编码byte[] imageTytes = t2d.EncodeToJPG();//if (i % 2 == 1)//{// bgimage_02.texture= t2d;//}//else//{// bgimage_03.texture = t2d;//}bgimage_02.texture= t2d;//存储File.WriteAllBytes(Application.streamingAssetsPath + "/my/" + Time.time + ".jpg", imageTytes);}void StopCamera(){//等待用户允许访问//Application.RequestUserAuthorization(UserAuthorization.WebCam);//如果用户允许访问,开始获取图像 if (Application.HasUserAuthorization(UserAuthorization.WebCam)){//先获取设备WebCamDevice[] device = WebCamTexture.devices;string deviceName = device[0].name;//然后获取图像// tex = new WebCamTexture(deviceName);// //将获取的图像赋值// ma.material.mainTexture = tex;//开始实施获取tex.Stop();}}//返回按钮public void Back(){StopCamera();UnityEngine.SceneManagement.SceneManager.LoadScene(0);}}

把它挂到一个有renderer的物体上,

上面已经有调用摄像头并可以保存图片。

保存图片有多种方式

using UnityEngine;using System.Collections;using System.IO;using UnityEngine.UI;public class FrameAnimation : MonoBehaviour{public Texture2D image;public int w;public int h;public float nextTime = 0.0f;public float rate = 0.3f;int i = 0;public static string imageName;public RawImage raw;// Use this for initializationvoid Start(){w = Screen.width;h = Screen.height;image = new Texture2D(w, h);}// Update is called once per framevoid Update(){//if (Input.GetMouseButton(0) && Time.time > nextTime)//{// nextTime = Time.time + rate;// i++;// StartCoroutine(SaveImage(i));//}}public void Btn_SavrImage(){拍照保存//i++;//StartCoroutine(SaveImage(i));UnityEngine.SceneManagement.SceneManager.LoadScene(2);}IEnumerator SaveImage(int i){yield return new WaitForEndOfFrame();image.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0, false);//read pixels from screen to textureimage.Apply();byte[] bytes = image.EncodeToPNG();string date = System.DateTime.Now.ToString("dd-MM-yy");imageName = i + date + ".png";File.WriteAllBytes(Application.streamingAssetsPath + "/" +imageName, bytes);ReadImage();Debug.Log("write a pic"+imageName);yield return null;}public void ReadImage(){string textureName = Application.streamingAssetsPath + imageName;//raw.name = textureName;raw.texture = image;// UnityEngine.SceneManagement.SceneManager.LoadScene(0);}}

注意:image.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0, false);我遇到一个问题截图超过1024,768的时候有时候会报错,这个问题因为后期不用了没有再继续深究。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。