目录
1、因为需要找到手指的最高点,最终决定使用OpencvForUnity,所以想找到将摄像头画面转换为Mat,再将mat显示在Unity中。这样我就可以对Mat进行类似Opencv一样的操作
1、Unity如何将摄像头画面显示在cube或者plane上面
①总结:good;知道OpencvForUnity下载、使用
1、注意:分辨率一定要注意,我在这里没有仔细写,摄像头的分辨率、RawImage的分辨率都是要注意大小
一:目的:
1、因为需要找到手指的最高点,最终决定使用OpencvForUnity,所以想找到将摄像头画面转换为Mat,再将mat显示在Unity中。这样我就可以对Mat进行类似Opencv一样的操作
一、参考:
1、Unity如何将摄像头画面显示在cube或者plane上面
https://blog.csdn.net/qq_40544338/article/details/102582442
①总结:good:知道打开相机
2、OpenCVForUnity插件 配置基础开发环境
https://blog.csdn.net/tpz789/article/details/91171127
①总结:good;知道OpencvForUnity下载、使用
一、自己做:
1、注意:分辨率一定要注意,我在这里没有仔细写,摄像头的分辨率、RawImage的分辨率都是要注意大小
/// <summary>
/// 功能:打开相机 ,保存图片失败,后序有时间再完善
/// 版本 :
/// </summary>
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using OpenCVForUnity;
using OpenCVForUnity.CoreModule;
using OpenCVForUnity.UnityUtils;
public class My_test03_openCamera : MonoBehaviour
{
// UI 相关参数
public RawImage rawImage;
public RawImage rawImage_copy_camera;
// 摄像机图片参数
private WebCamTexture webCamTexture;
void Start()
{
// 打开相机
StartCoroutine("OpenCamera");
}
void Update()
{
if (rawImage.texture != null)
{
//测试3:webCamTextureToMat:没有报错
Mat frameMat = new Mat(webCamTexture.height, webCamTexture.width, CvType.CV_8UC4);
Color32[] colors = new Color32[webCamTexture.width * webCamTexture.height];
Utils.webCamTextureToMat(webCamTexture, frameMat, colors, false);
//int i_width = (int)rawImage.GetComponentInParent<RectTransform>().rect.width;
//int i_heigth = (int)rawImage.GetComponentInParent<RectTransform>().rect.height;
int i_width = webCamTexture.width;
int i_heigth = webCamTexture.height;
Texture2D texture = new Texture2D(i_width, i_heigth, TextureFormat.RGBA32, false);
Utils.matToTexture2D(frameMat, texture);
rawImage_copy_camera.texture = texture;
}
}
/// <summary>
/// 使用协程打开相机函数
/// </summary>
/// <returns></returns>
IEnumerator OpenCamera()
{
// 申请相机权限
yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
// 判断是否有相机权限
if (Application.HasUserAuthorization(UserAuthorization.WebCam))
{
// 获取相机设备
WebCamDevice[] webCamDevices = WebCamTexture.devices;
// 判断是否有相机设别
if (webCamDevices != null && webCamDevices.Length > 0)
{
// 把 0 号设备(移动端后置摄像头)名称赋值
string webCamName = webCamDevices[0].name;
// 设置相机渲染宽高,并运行相机
webCamTexture = new WebCamTexture(webCamName, Screen.width, Screen.height);
webCamTexture.Play();
// 把获取的图像渲染到画布上
rawImage.texture = webCamTexture;
}
}
}
}
来源:CSDN
作者:智勇双全的智勇
链接:https://blog.csdn.net/qq_40544338/article/details/104020926