首先界面是这样的,很简易
直接上代码
using System.IO;
using UnityEngine;
public class WebCamera : MonoBehaviour
{
[Tooltip("返回的照片数据,根据返回的照片,进行照片的识别和比对认证等等!"), Space(5)]
[Header("摄像头拍摄的照片"),]
public WebCamTexture camTexture;//摄像头拍下的图片数据
[Header("摄像头设备名")]
private WebCamDevice[] devices;
[Tooltip("USB摄像头设备"), Space(5)]
[Header("摄像头设备名")]
public string deviceName;//摄像头设备名称
public bool isClick;//是否点击了按钮
private void Start()
{
isClick = false;
}
//初始化摄像头显示的图像的大小
private void Awake()
{
camTexture = new WebCamTexture(deviceName, 800, 600, 60);
}
//通过GUI绘制摄像头要显示的窗口
private void OnGUI()
{
//首先根据摄像头展示的画面来判断摄像头是否存在
if (isClick == true && camTexture != null)
{
//绘制画面(Screen.width / 2 - 150f, Screen.height / 2 - 290,这里是画面距离场景的高和宽的限制)
//800, 600是和camTexture的画面一样大的绘制窗口
GUI.DrawTexture(new Rect(0, 0, 800, 600), camTexture);
}
if (isClick == false && camTexture != null)//不显示画面(没写这个步骤之前有个坑)
{
GUI.DrawTexture(new Rect(Screen.width / 2 - 150f, Screen.height / 2 - 290, 0, 0), camTexture);
}
}
//打开摄像机的方法//挂到button按钮上
public void OpenWebCamDevice()
{
isClick = true;
if (isClick == true)
{
//用户授权打开摄像头
if (Application.HasUserAuthorization(UserAuthorization.WebCam))
{
devices = WebCamTexture.devices;//显示画面的设备就是要打开的摄像头
deviceName = devices[0].name;//获取到设备名称
camTexture.Play();//开启摄像头
}
}
}
//关闭摄像头//挂到button按钮上
public void CloseWebCamDevice()
{
if (isClick == true && camTexture != null)
{
isClick = false;
}
}
}
截图脚本
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
public class JieTu : MonoBehaviour
{
public RawImage RawImgLeft;
public Image[] warningImgs;
List<Texture2D> cacheTexture = new List<Texture2D>(); // 缓存图片
readonly string savePath = Application.streamingAssetsPath + "/SavePng/"; // 图片保存路径
// 此方法实现的是显示四个图片,多余四个的就后面排上将前面挤掉
public void JieTuFunc(float temperature)
{
Texture2D jietuTexture = TextureToTexture2D(RawImgLeft.texture);
if (this.cacheTexture.Count >= 4)
{
this.cacheTexture.RemoveAt(0);
}
save(jietuTexture, temperature);
this.cacheTexture.Add(jietuTexture);
for (int i = 0; i < this.cacheTexture.Count; i++)
{
warningImgs[i].sprite = Sprite.Create(this.cacheTexture[i], new Rect(0, 0, this.cacheTexture[i].width, this.cacheTexture[i].height), new Vector2(0.5f, 0.5f));
warningImgs[i].rectTransform.sizeDelta = Vector3.one * 90;
}
}
/// <summary>
/// 保存图片
/// </summary>
/// <param name="texture2D"></param>
void save(Texture2D texture2D, float avg)
{
byte[] textureBytes = texture2D.EncodeToPNG();
FileStream fs = File.Open(savePath + DateTime.Now.Year + "_" + DateTime.Now.Month + "_" + DateTime.Now.Day + "_" + DateTime.Now.Hour + "_" + DateTime.Now.Minute + "_" + DateTime.Now.Second + "_" + avg + "℃.png", FileMode.OpenOrCreate);
fs.Write(textureBytes, 0, textureBytes.Length);
fs.Flush();
fs.Close();
}
/// <summary>
/// Texture转换成Texture2D
/// </summary>
/// <param name="texture"></param>
/// <returns></returns>
private Texture2D TextureToTexture2D(Texture texture)
{
Texture2D texture2D = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false);
RenderTexture currentRT = RenderTexture.active;
RenderTexture renderTexture = RenderTexture.GetTemporary(texture.width, texture.height, 32);
Graphics.Blit(texture, renderTexture);
RenderTexture.active = renderTexture;
texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
texture2D.Apply();
RenderTexture.active = currentRT;
RenderTexture.ReleaseTemporary(renderTexture);
return texture2D;
}
}
需要注意的是,因为路径的问题,需要手动创建放置截图的文件夹 StreamingAssets/my
效果如图
截图