Tanoda
DeviceCamera.cs
Go to the documentation of this file.
1using System;
2using UnityEngine;
3
4public class DeviceCamera
5{
6 public Texture preview
7 {
8 get
9 {
10#if (UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR
11 if(isUseEasyWebCam)
12 {
13 return EasyWebCam.WebCamPreview;
14 }
15 else
16 {
17 return webcamera;
18 }
19 #else
20 return webcamera;
21#endif
22 }
23 }
24
25 WebCamTexture webcamera;
26 bool isRunning;
27 bool isUseEasyWebCam = true;
28
29 int previewWidth = 640;
30 int previewHeight = 480;
31
32 public DeviceCamera(bool isUseEWC = true)
33 {
34#if (UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR
35 isUseEasyWebCam = isUseEWC;
36 if(isUseEasyWebCam)
37 {
38 GameObject webCamObject = new GameObject("EasyWebCamLib");
39 webCamObject.AddComponent<EasyWebCam>();
40 }
41 else
42 {
43#if UNITY_ANDROID
44 if(EasyWebCam.checkPermissions())
45 {
46 webcamera = new WebCamTexture (640, 480);
47 }
48#else
49 webcamera = new WebCamTexture (640, 480);
50#endif
51 }
52#else
53 webcamera = new WebCamTexture(640, 480);
54#endif
55 }
56
57 public DeviceCamera(int width, int height, bool isUseEWC = true)
58 {
59#if (UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR
60 isUseEasyWebCam = isUseEWC;
61 if(isUseEasyWebCam)
62 {
63 GameObject gameObject = new GameObject("EasyWebCamLib");
64 gameObject.AddComponent<EasyWebCam>();
65 }
66 else
67 {
68#if UNITY_ANDROID
69 if(EasyWebCam.checkPermissions())
70 {
71 previewWidth = width;
72 previewHeight = height;
73 webcamera = new WebCamTexture (width, height);
74 }
75#else
76 previewWidth = width;
77 previewHeight = height;
78 webcamera = new WebCamTexture (width, height);
79#endif
80 }
81#else
82 webcamera = new WebCamTexture(width, height);
83#endif
84 }
85
89 public void Play()
90 {
91 if (isRunning) return;
92#if (UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR
93 if(isUseEasyWebCam)
94 {
95 EasyWebCam.Play();
96 }
97 else{
98 if(webcamera != null)
99 {
100 webcamera.Play ();
101 }
102 else
103 {
104 webcamera = new WebCamTexture (previewWidth, previewHeight);
105 webcamera.Play ();
106 }
107 }
108#else
109 var success = false;
110 foreach (var webCamDevice in WebCamTexture.devices)
111 {
112 if (webCamDevice.name == "UVC Camera") continue;
113 try
114 {
115 webcamera = new WebCamTexture(webCamDevice.name, 640, 480);
116 webcamera.Play();
117 success = true;
118 break;
119 }
120 catch (Exception)
121 {
122 //ignored
123 }
124 }
125
126 if (!success) throw new Exception("No usable webcamera found!");
127#endif
128 isRunning = true;
129 }
130
134 public void Stop()
135 {
136 if (!isRunning) return;
137#if (UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR
138 if(isUseEasyWebCam)
139 {
140 EasyWebCam.Stop();
141 }
142 else
143 {
144 if(webcamera != null)
145 {
146 webcamera.Stop ();
147 }
148 }
149#else
150 webcamera.Stop();
151#endif
152 isRunning = false;
153 }
154
159 public Vector2 getSize()
160 {
161#if (UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR
162 if(isUseEasyWebCam)
163 {
164 return new Vector2(EasyWebCam.Width(), EasyWebCam.Height());
165 }
166 else
167 {
168 return new Vector2(webcamera.width, webcamera.height);
169 }
170#else
171 return new Vector2(webcamera.width, webcamera.height);
172#endif
173 }
174
178 public int Width()
179 {
180#if (UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR
181 if(isUseEasyWebCam)
182 {
183 return EasyWebCam.Width();
184 }
185 else
186 {
187 return webcamera.width;
188 }
189#else
190 return webcamera.width;
191#endif
192 }
193
197 public int Height()
198 {
199#if (UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR
200 if(isUseEasyWebCam)
201 {
202 return EasyWebCam.Height();
203 }
204 else
205 {
206 return webcamera.height;
207 }
208
209#else
210 return webcamera.height;
211#endif
212 }
213
218 public bool isPlaying()
219 {
220#if (UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR
221 if(isUseEasyWebCam)
222 {
223 return EasyWebCam.isPlaying ();
224 }
225 else
226 {
227 return webcamera.isPlaying;
228 }
229
230#else
231 return webcamera.isPlaying;
232#endif
233 }
234
239 public Color[] GetPixels()
240 {
241#if (UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR
242 if(isUseEasyWebCam)
243 {
244 return EasyWebCam.WebCamPreview.GetPixels();
245 }
246 else
247 {
248 return webcamera.GetPixels();
249 }
250
251#else
252 return webcamera.GetPixels();
253#endif
254 }
255
264 public Color[] GetPixels(int x, int y, int targetWidth, int targetHeight)
265 {
266#if (UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR
267 if(isUseEasyWebCam)
268 {
269 return EasyWebCam.WebCamPreview.GetPixels(x,y,targetWidth,targetHeight);
270 }
271 else
272 {
273 return webcamera.GetPixels(x,y,targetWidth,targetHeight);
274 }
275
276#else
277 return webcamera.GetPixels(x, y, targetWidth, targetHeight);
278#endif
279 }
280
285 public Color32[] GetPixels32()
286 {
287#if (UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR
288 if(isUseEasyWebCam)
289 {
290 return EasyWebCam.WebCamPreview.GetPixels32();
291 }
292 else
293 {
294 return webcamera.GetPixels32();
295 }
296#else
297 return webcamera.GetPixels32();
298#endif
299 }
300}
UnityEngine.Color Color
Definition: TestScript.cs:32
int Width()
get the width of the camera
Color32[] GetPixels32()
Gets the pixels32 of the camera
DeviceCamera(bool isUseEWC=true)
Definition: DeviceCamera.cs:32
int Height()
get the height of the camera
bool isPlaying()
get status of the camera
Color[] GetPixels(int x, int y, int targetWidth, int targetHeight)
get the pixels of the camera image by using the target rect range
void Play()
open the camera
Definition: DeviceCamera.cs:89
Texture preview
Definition: DeviceCamera.cs:7
void Stop()
Stop this camera.
Vector2 getSize()
Gets the size of the webcam
Color[] GetPixels()
get the Pixels of the camera image
DeviceCamera(int width, int height, bool isUseEWC=true)
Definition: DeviceCamera.cs:57