首先界面是这样的,很简易,脚本上挂了三个按钮

直接上代码

using UnityEngine;
using UnityEngine.UI;public class WebCamera : MonoBehaviour
{WebCamTexture camTexture;private WebCamDevice[] devices;/// <summary>/// 摄像头设备名/// </summary>string deviceName;/// <summary>/// 摄像头显示状态/// </summary>bool cameraIsOpen;public Button openCamera, closeCamera, screenShot;private void Awake(){//初始化摄像头显示的图像的大小camTexture = new WebCamTexture(deviceName, 800, 600, 60);}private void Start(){cameraIsOpen = false;openCamera.onClick.AddListener(OpenWebCamDevice);closeCamera.onClick.AddListener(CloseWebCamDevice);screenShot.onClick.AddListener(() =>{// 将图像传给截图方法if (camTexture.isPlaying)GetComponent<ScreenShot>().SaveScreenShot(camTexture);});}//通过GUI绘制摄像头要显示的窗口private void OnGUI(){//首先根据摄像头展示的画面来判断摄像头是否存在if (cameraIsOpen && camTexture){//绘制画面(Screen.width / 2 - 150f, Screen.height / 2 - 290,这里是画面距离场景的高和宽的限制)//800, 600是和camTexture的画面一样大的绘制窗口GUI.DrawTexture(new Rect(0, 0, 800, 600), camTexture);}if (!cameraIsOpen && camTexture)//不显示画面(没写这个步骤之前有个坑){GUI.DrawTexture(new Rect(Screen.width / 2 - 150f, Screen.height / 2 - 290, 0, 0), camTexture);}}/// <summary>/// 打开摄像头/// </summary>void OpenWebCamDevice(){//用户授权if (Application.HasUserAuthorization(UserAuthorization.WebCam)){//显示画面的设备就是要打开的摄像头devices = WebCamTexture.devices;//获取到设备名称deviceName = devices[0].name;//开启摄像头camTexture.Play();cameraIsOpen = true;}}/// <summary>/// 关闭摄像头/// </summary>void CloseWebCamDevice(){if (cameraIsOpen == true && camTexture != null){cameraIsOpen = false;camTexture.Stop();}}
}

截图脚本

using System;
using System.IO;
using UnityEngine;public class ScreenShot : MonoBehaviour
{readonly string savePath = Application.streamingAssetsPath + "/my"; // 图片保存路径public void SaveScreenShot(Texture texture){Texture2D jietuTexture = TextureToTexture2D(texture);Save(jietuTexture);}/// <summary>/// 保存图片/// </summary>/// <param name="texture2D"></param>void Save(Texture2D texture2D){byte[] textureBytes = texture2D.EncodeToPNG();string filename = string.Format("IMG_{0}{1}{2}_{3}{4}{5}.jpg", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);if (!Directory.Exists(savePath)){Directory.CreateDirectory(savePath);}Debug.Log("保存路径=====" + savePath + "/" + filename);File.WriteAllBytes(savePath + "/" + filename, textureBytes);if (File.Exists(savePath + "/" + filename))Debug.Log("找到照片");elseDebug.Log("未找到");}/// <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;}
}

效果如图

截图 上面那张图让我不小心删了…

Unity调用摄像头并截图相关推荐

  1. unity调用摄像头拍照

    我的unity调用摄像头拍照 因为做的是pc端的,所以不知道手机上效果是怎样的,据说代码是完全相同的. 废话不说,上代码, 打开摄像头代码: /// <summary>/// Opens ...

  2. js调用摄像头并截图

    js调用摄像头并截图 <!DOCTYPE html> <html><head><meta charset="utf-8"><t ...

  3. Unity 调用摄像头拍照保存

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

  4. 关于Unity调用摄像头扫描二维码与生成二维码的实现方法

    1.常用的生成二维码网址 https://cli.im/ 2.上官网下载二维码插件 http://zxingnet.codeplex.com/ 3.将下载的插件中zxing.unity.dll文件放入 ...

  5. Java通过webcam-capture调用摄像头并截图

    需要的包: bridj-0.7.0.jar.webcam-capture-0.3.12.jar. slf4j-api-1.8.0-beta2.jar.slf4j-simple-1.8.0-beta2. ...

  6. unity调用摄像头和闪光灯

     1.开启现行对焦模式(FOCUS_MODE_CONTINUOUS_AUTO) 这种模式可以使设备根据当前场景进行自动对焦 2.Vuforia其他对焦模式 并不是所有的设备都支持线性对焦的,所以需 ...

  7. 调用摄像头_摄像头 | 浏览器调用摄像头并实现截图保存的效果

    ​为了在QT上实现打开摄像头,拍照等操作.就写了这个. 1. 写一个vue组件 先写一个vue的组件,其内容基本如下: <el-button type="primary" @ ...

  8. 怎样unity调用大华摄像头

    unity调用摄像头需要引用插件 UniversalMediaPlayer (这个为unity视频的播放插件) 有的可能也无法unity播放视频的,需要电脑装VLC media player 然后把U ...

  9. python人脸识别截图_Python OpenCV调用摄像头检测人脸并截图

    本文实例为大家分享了Python OpenCV调用摄像头检测人脸并截图的具体代码,供大家参考,具体内容如下 注意:需要在python中安装OpenCV库,同时需要下载OpenCV人脸识别模型haarc ...

最新文章

  1. RSTP的根保护功能
  2. Harbor2.2.1配置(trivy扫描器、镜像签名)
  3. OpenLDAP的安装测试及管理
  4. 工作总结8:关于Vue中的slot-scope=“scope“
  5. 前端开发JS的学习之AngularJS库
  6. Windows Mobile 7(Photon) 梦幻之旅系列-前言
  7. 牛客练习赛24 C PH试纸
  8. 带圈圈的数字1~50,求50以上,不要word的
  9. IIC总线协议详解与总结
  10. JavaScript最详细基础语法总结(跳坑记录!)
  11. 图解PC CPU发展史
  12. 不可思议!信用报告也能出错?
  13. 【Kotlin入门教程】史上最全最易于理解最全面的文章
  14. 在flask中同步调用celery.task函数报错分析
  15. 转载:编剧技巧思路乱谈
  16. APISpace 的 星座配对API
  17. 7000字详解数据指标体系如何从设计到落地
  18. 计算机辅助设计2004,电子线路计算机辅助设计( Protel 2004 ).PDF
  19. jpg转bmp操作方法
  20. 随机分布嵌入使短时高维序列可预测

热门文章

  1. 图解六种全面质量管理工具用法和举例
  2. 仿翁天信旅行足迹地图源码
  3. for循环之国际象棋棋盘
  4. apache2+php 初配置
  5. [Office Web Apps]实现在线office文档预览
  6. 十代思域遥控升降窗 - 远程开窗
  7. winserver 重启 事件10016 distributed
  8. 无线电空间传输损耗衰减计算(转帖)
  9. VirtIO-GPU环境搭建与应用
  10. 差速机器人的纯轨迹跟踪仿真(Matlab)