forked from EnoxSoftware/OpenCVForUnity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebCamTextureToMatSample.cs
More file actions
134 lines (89 loc) · 3.55 KB
/
Copy pathWebCamTextureToMatSample.cs
File metadata and controls
134 lines (89 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using UnityEngine;
using System.Collections;
using OpenCVForUnity;
namespace OpenCVForUnitySample
{
/// <summary>
/// WebCamTexture to mat sample.
/// </summary>
public class WebCamTextureToMatSample : MonoBehaviour
{
WebCamTexture webCamTexture;
Color32[] colors;
#if (UNITY_ANDROID || UNITY_IPHONE) && !UNITY_EDITOR
bool isFront = false;
#endif
int width = 640;
int height = 480;
Mat rgbaMat;
Texture2D texture;
bool initDone = false;
// Use this for initialization
void Start ()
{
// Checks how many and which cameras are available on the device
for (int cameraIndex = 0; cameraIndex < WebCamTexture.devices.Length; cameraIndex++) {
#if (UNITY_ANDROID || UNITY_IPHONE) && !UNITY_EDITOR
if (WebCamTexture.devices [cameraIndex].isFrontFacing == isFront) {
#endif
Debug.Log (cameraIndex + " name " + WebCamTexture.devices [cameraIndex].name + " isFrontFacing " + WebCamTexture.devices [cameraIndex].isFrontFacing);
//Set the appropriate fps
webCamTexture = new WebCamTexture (WebCamTexture.devices [cameraIndex].name, width, height, 30);
#if (UNITY_ANDROID || UNITY_IPHONE) && !UNITY_EDITOR
break;
}
#endif
}
Debug.Log ("width " + webCamTexture.width + " height " + webCamTexture.height + " fps " + webCamTexture.requestedFPS);
// Starts the camera
webCamTexture.Play ();
StartCoroutine (init ());
}
private IEnumerator init ()
{
while (true) {
//If you want to use webcamTexture.width and webcamTexture.height on iOS, you have to wait until webcamTexture.didUpdateThisFrame == 1, otherwise these two values will be equal to 16. (http://forum.unity3d.com/threads/webcamtexture-and-error-0x0502.123922/)
if (webCamTexture.didUpdateThisFrame) {
Debug.Log ("width " + webCamTexture.width + " height " + webCamTexture.height + " fps " + webCamTexture.requestedFPS);
colors = new Color32[webCamTexture.width * webCamTexture.height];
rgbaMat = new Mat (webCamTexture.height, webCamTexture.width, CvType.CV_8UC4);
texture = new Texture2D (webCamTexture.width, webCamTexture.height, TextureFormat.RGBA32, false);
gameObject.transform.eulerAngles = new Vector3 (0, 0, -90);
gameObject.transform.localScale = new Vector3 (webCamTexture.width, webCamTexture.height, 1);
gameObject.GetComponent<Renderer> ().material.mainTexture = texture;
Camera.main.orthographicSize = webCamTexture.width / 2;
initDone = true;
break;
} else {
yield return 0;
}
}
}
// Update is called once per frame
void Update ()
{
if (!initDone)
return;
if (webCamTexture.didUpdateThisFrame) {
Utils.WebCamTextureToMat (webCamTexture, rgbaMat, colors);
Utils.matToTexture2D (rgbaMat, texture, colors);
gameObject.GetComponent<Renderer> ().material.mainTexture = texture;
}
}
void OnDisable ()
{
webCamTexture.Stop ();
}
void OnGUI ()
{
float screenScale = Screen.width / 240.0f;
Matrix4x4 scaledMatrix = Matrix4x4.Scale (new Vector3 (screenScale, screenScale, screenScale));
GUI.matrix = scaledMatrix;
GUILayout.BeginVertical ();
if (GUILayout.Button ("back")) {
Application.LoadLevel ("OpenCVForUnitySample");
}
GUILayout.EndVertical ();
}
}
}