forked from mtysgithub/Qualcomm.Vuforia.UnityExtensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCameraDeviceImpl.cs
267 lines (245 loc) · 7.32 KB
/
CameraDeviceImpl.cs
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
internal class CameraDeviceImpl : CameraDevice
{
private CameraDevice.CameraDirection mCameraDirection;
private Dictionary<Image.PIXEL_FORMAT, Image> mCameraImages = new Dictionary<Image.PIXEL_FORMAT, Image>();
private bool mCameraReady;
private bool mIsDirty;
private static WebCamImpl mWebCam;
public override bool Deinit()
{
if (this.DeinitCameraDevice() == 0)
{
return false;
}
this.mCameraReady = false;
return true;
}
private int DeinitCameraDevice()
{
if (!QCARRuntimeUtilities.IsPlayMode())
{
return QCARWrapper.Instance.CameraDeviceDeinitCamera();
}
int num = 0;
if (mWebCam != null)
{
mWebCam.StopCamera();
num = 1;
}
QCARWrapper.Instance.CameraDeviceDeinitCamera();
return num;
}
public Dictionary<Image.PIXEL_FORMAT, Image> GetAllImages()
{
return this.mCameraImages;
}
public override CameraDevice.CameraDirection GetCameraDirection()
{
return this.mCameraDirection;
}
public override Image GetCameraImage(Image.PIXEL_FORMAT format)
{
if (this.mCameraImages.ContainsKey(format))
{
Image image = this.mCameraImages[format];
if (image.IsValid())
{
return image;
}
}
return null;
}
public override CameraDevice.VideoModeData GetVideoMode(CameraDevice.CameraDeviceMode mode)
{
if (QCARRuntimeUtilities.IsPlayMode())
{
return this.WebCam.GetVideoMode();
}
IntPtr videoMode = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(CameraDevice.VideoModeData)));
QCARWrapper.Instance.CameraDeviceGetVideoMode((int) mode, videoMode);
CameraDevice.VideoModeData data = (CameraDevice.VideoModeData) Marshal.PtrToStructure(videoMode, typeof(CameraDevice.VideoModeData));
Marshal.FreeHGlobal(videoMode);
return data;
}
public override bool Init(CameraDevice.CameraDirection cameraDirection)
{
if (QCARRuntimeUtilities.IsPlayMode())
{
cameraDirection = CameraDevice.CameraDirection.CAMERA_BACK;
}
if (this.InitCameraDevice((int) cameraDirection) == 0)
{
return false;
}
this.mCameraDirection = cameraDirection;
this.mCameraReady = true;
if (this.CameraReady)
{
QCARAbstractBehaviour behaviour = (QCARAbstractBehaviour) UnityEngine.Object.FindObjectOfType(typeof(QCARAbstractBehaviour));
if (behaviour != null)
{
behaviour.ResetClearBuffers();
behaviour.ConfigureVideoBackground(true);
}
}
return true;
}
private int InitCameraDevice(int camera)
{
if (!QCARRuntimeUtilities.IsPlayMode())
{
return QCARWrapper.Instance.CameraDeviceInitCamera(camera);
}
int num = 0;
try
{
WebCamAbstractBehaviour behaviour = (WebCamAbstractBehaviour) UnityEngine.Object.FindObjectOfType(typeof(WebCamAbstractBehaviour));
behaviour.InitCamera();
mWebCam = behaviour.ImplementationClass;
QCARWrapper.Instance.CameraDeviceSetCameraConfiguration(mWebCam.ResampledTextureSize.x, mWebCam.ResampledTextureSize.y);
num = 1;
}
catch (NullReferenceException exception)
{
Debug.LogError(exception.Message);
}
QCARWrapper.Instance.CameraDeviceInitCamera(camera);
this.mCameraImages.Clear();
return num;
}
public bool IsDirty()
{
if (!QCARRuntimeUtilities.IsPlayMode())
{
return this.mIsDirty;
}
if (!this.mIsDirty)
{
return this.WebCam.IsRendererDirty();
}
return true;
}
public void ResetDirtyFlag()
{
this.mIsDirty = false;
}
public override bool SelectVideoMode(CameraDevice.CameraDeviceMode mode)
{
if (QCARWrapper.Instance.CameraDeviceSelectVideoMode((int) mode) == 0)
{
return false;
}
return true;
}
public override bool SetFlashTorchMode(bool on)
{
bool flag = QCARWrapper.Instance.CameraDeviceSetFlashTorchMode(on ? 1 : 0) != 0;
Debug.Log("Toggle flash " + (on ? "ON" : "OFF") + " " + (flag ? "WORKED" : "FAILED"));
return flag;
}
public override bool SetFocusMode(CameraDevice.FocusMode mode)
{
bool flag = QCARWrapper.Instance.CameraDeviceSetFocusMode((int) mode) != 0;
Debug.Log("Requested Focus mode " + mode + (flag ? " successfully." : ". Not supported on this device."));
return flag;
}
public override bool SetFrameFormat(Image.PIXEL_FORMAT format, bool enabled)
{
if (enabled)
{
if (!this.mCameraImages.ContainsKey(format))
{
if (QCARWrapper.Instance.QcarSetFrameFormat((int) format, 1) == 0)
{
Debug.LogError("Failed to set frame format");
return false;
}
Image image = new ImageImpl {
PixelFormat = format
};
this.mCameraImages.Add(format, image);
return true;
}
}
else if (this.mCameraImages.ContainsKey(format))
{
if (QCARWrapper.Instance.QcarSetFrameFormat((int) format, 0) == 0)
{
Debug.LogError("Failed to set frame format");
return false;
}
return this.mCameraImages.Remove(format);
}
return true;
}
public override bool Start()
{
this.mIsDirty = true;
GL.Clear(false, true, new Color(0f, 0f, 0f, 1f));
if (this.StartCameraDevice() == 0)
{
return false;
}
return true;
}
private int StartCameraDevice()
{
if (!QCARRuntimeUtilities.IsPlayMode())
{
return QCARWrapper.Instance.CameraDeviceStartCamera();
}
int num = 0;
if (mWebCam != null)
{
mWebCam.StartCamera();
num = 1;
}
QCARWrapper.Instance.CameraDeviceStartCamera();
return num;
}
public override bool Stop()
{
if (this.StopCameraDevice() == 0)
{
return false;
}
return true;
}
private int StopCameraDevice()
{
if (!QCARRuntimeUtilities.IsPlayMode())
{
return QCARWrapper.Instance.CameraDeviceStopCamera();
}
int num = 0;
if (mWebCam != null)
{
mWebCam.StopCamera();
num = 1;
}
QCARWrapper.Instance.CameraDeviceStopCamera();
return num;
}
public bool CameraReady
{
get
{
if (!QCARRuntimeUtilities.IsPlayMode())
{
return this.mCameraReady;
}
return ((mWebCam != null) && mWebCam.IsTextureSizeAvailable);
}
}
public WebCamImpl WebCam
{
get
{
return mWebCam;
}
}
}