MaxstARSDK  3.5.0
CameraDevice.cs
1 /*==============================================================================
2 Copyright 2017 Maxst, Inc. All Rights Reserved.
3 ==============================================================================*/
4 
5 using UnityEngine;
6 using System;
7 using System.Linq;
8 using System.Runtime.InteropServices;
9 using System.Collections.Generic;
10 using System.Text;
11 
12 namespace maxstAR
13 {
17  public class CameraDevice
18  {
22  public enum FocusMode
23  {
27  FOCUS_MODE_CONTINUOUS_AUTO = 1,
31  FOCUS_MODE_AUTO = 2
32  }
33 
37  public enum CameraType
38  {
42  Rear = 0,
43 
47  Face = 1,
48  }
49 
53  public enum CameraResolution
54  {
58  Resolution640x480,
59 
63  Resolution1280x720,
64 
68  Resolution1920x1080,
69  }
70 
74  public enum FlipDirection
75  {
79  HORIZONTAL = 0,
80 
84  VERTICAL = 1
85  }
86 
87  private static CameraDevice instance = null;
88 
93  public static CameraDevice GetInstance()
94  {
95  if (instance == null)
96  {
97  instance = new CameraDevice();
98  }
99  return instance;
100  }
101 
102  private int cameraId = 0;
103  private int preferredWidth = 0;
104  private int preferredHeight = 0;
105  private bool flipHorizontal = false;
106  private bool flipVertical = false;
107  private byte[] cameraFrameBuffer = null;
108 
109  private CameraDevice()
110  {
111  }
112 
116  public ResultCode Start()
117  {
118  int cameraType = 0;
119  if (Application.platform == RuntimePlatform.OSXEditor || Application.platform == RuntimePlatform.WindowsEditor)
120  {
122  }
123  else
124  {
125  // TODO: Check WebCamTexture.devices isFrontFacing
127  }
128 
130  switch (cameraResolution)
131  {
132  case CameraDevice.CameraResolution.Resolution640x480:
133  preferredWidth = 640;
134  preferredHeight = 480;
135  break;
136 
137  case CameraDevice.CameraResolution.Resolution1280x720:
138  preferredWidth = 1280;
139  preferredHeight = 720;
140  break;
141 
142  case CameraDevice.CameraResolution.Resolution1920x1080:
143  preferredWidth = 1920;
144  preferredHeight = 1080;
145  break;
146 
147  default:
148  preferredWidth = 640;
149  preferredHeight = 480;
150  break;
151  }
152 
153  Debug.Log("Camera id : " + cameraId);
154 
155  if (Application.platform == RuntimePlatform.IPhonePlayer)
156  {
157  return (ResultCode)(NativeStaticAPI.CameraDevice_start(cameraType, preferredWidth, preferredHeight));
158  }
159  else
160  {
161  return (ResultCode)(NativeSharedAPI.CameraDevice_start(cameraType, preferredWidth, preferredHeight));
162  }
163  }
164 
170  public bool SetFocusMode(FocusMode focusMode)
171  {
172  if (Application.platform == RuntimePlatform.IPhonePlayer)
173  {
174  return NativeStaticAPI.CameraDevice_setFocusMode((int)focusMode);
175  }
176  else
177  {
178  return NativeSharedAPI.CameraDevice_setFocusMode((int)focusMode);
179  }
180  }
181 
185  public bool SetFlashLightMode(bool toggle)
186  {
187  if (Application.platform == RuntimePlatform.IPhonePlayer)
188  {
189  return NativeStaticAPI.CameraDevice_setFlashLightMode(toggle);
190  }
191  else
192  {
193  return NativeSharedAPI.CameraDevice_setFlashLightMode(toggle);
194  }
195  }
196 
200  public bool SetAutoWhiteBalanceLock(bool toggle)
201  {
202  if (Application.platform == RuntimePlatform.IPhonePlayer)
203  {
204  return NativeStaticAPI.CameraDevice_setAutoWhiteBalanceLock(toggle);
205  }
206  else
207  {
208  return NativeSharedAPI.CameraDevice_setAutoWhiteBalanceLock(toggle);
209  }
210  }
211 
217  public void FlipVideo(FlipDirection direction, bool on)
218  {
219  if (Application.platform == RuntimePlatform.IPhonePlayer)
220  {
221  NativeStaticAPI.CameraDevice_flipVideo((int)direction, on);
222  }
223  else
224  {
225  NativeSharedAPI.CameraDevice_flipVideo((int)direction, on);
226  }
227 
228  if (direction == FlipDirection.HORIZONTAL)
229  {
230  flipHorizontal = on;
231  }
232 
233  if (direction == FlipDirection.VERTICAL)
234  {
235  flipVertical = on;
236  }
237  }
238 
243  public List<string> GetParamList()
244  {
245  List<string> paramList = new List<string>();
246  if (Application.platform == RuntimePlatform.IPhonePlayer)
247  {
248  int size = NativeStaticAPI.CameraDevice_getParamList();
249  for (int i = 0; i < size; i++)
250  {
251  int keyLength = NativeStaticAPI.CameraDevice_Param_getKeyLength(i);
252  byte[] keyBytes = new byte[keyLength];
253  NativeStaticAPI.CameraDevice_Param_getKey(i, keyBytes);
254  string keyString = Encoding.UTF8.GetString(keyBytes).TrimEnd('\0');
255  paramList.Add(keyString);
256  }
257  }
258  else
259  {
260  int size = NativeSharedAPI.CameraDevice_getParamList();
261  for (int i = 0; i < size; i++)
262  {
263  int keyLength = NativeSharedAPI.CameraDevice_Param_getKeyLength(i);
264  byte[] keyBytes = new byte[keyLength];
265  NativeSharedAPI.CameraDevice_Param_getKey(i, keyBytes);
266  string keyString = Encoding.UTF8.GetString(keyBytes).TrimEnd('\0');
267  paramList.Add(keyString);
268  }
269  }
270 
271  return paramList;
272  }
273 
274 
281  public bool SetParam(string key, bool boolTypeValue)
282  {
283  if (Application.platform == RuntimePlatform.IPhonePlayer)
284  {
285  return NativeStaticAPI.CameraDevice_setBoolTypeParameter(key, boolTypeValue);
286  }
287  else
288  {
289  return NativeSharedAPI.CameraDevice_setBoolTypeParameter(key, boolTypeValue);
290  }
291  }
292 
299  public bool SetParam(string key, int intTypeValue)
300  {
301  if (Application.platform == RuntimePlatform.IPhonePlayer)
302  {
303  return NativeStaticAPI.CameraDevice_setIntTypeParameter(key, intTypeValue);
304  }
305  else
306  {
307  return NativeSharedAPI.CameraDevice_setIntTypeParameter(key, intTypeValue);
308  }
309  }
310 
318  public bool SetParam(string key, int min, int max)
319  {
320  if (Application.platform == RuntimePlatform.IPhonePlayer)
321  {
322  return NativeStaticAPI.CameraDevice_setRangeTypeParameter(key, min, max);
323  }
324  else
325  {
326  return NativeSharedAPI.CameraDevice_setRangeTypeParameter(key, min, max);
327  }
328  }
329 
336  public bool SetParam(string key, string stringTypeValue)
337  {
338  if (Application.platform == RuntimePlatform.IPhonePlayer)
339  {
340  return NativeStaticAPI.CameraDevice_setStringTypeParameter(key, stringTypeValue);
341  }
342  else
343  {
344  return NativeSharedAPI.CameraDevice_setStringTypeParameter(key, stringTypeValue);
345  }
346  }
347 
351  public void Stop()
352  {
353  if (Application.platform == RuntimePlatform.IPhonePlayer)
354  {
355  NativeStaticAPI.CameraDevice_stop();
356  }
357  else
358  {
359  NativeSharedAPI.CameraDevice_stop();
360  }
361  }
362 
366  public int GetWidth()
367  {
368  if (Application.platform == RuntimePlatform.IPhonePlayer)
369  {
370  return NativeStaticAPI.CameraDevice_getWidth();
371  }
372  else
373  {
374  return NativeSharedAPI.CameraDevice_getWidth();
375  }
376  }
377 
381  public int GetHeight()
382  {
383  if (Application.platform == RuntimePlatform.IPhonePlayer)
384  {
385  return NativeStaticAPI.CameraDevice_getHeight();
386  }
387  else
388  {
389  return NativeSharedAPI.CameraDevice_getHeight();
390  }
391  }
392 
393  internal void GetProjectionMatrix(float[] matrix)
394  {
395  if (Application.platform == RuntimePlatform.IPhonePlayer)
396  {
397  NativeStaticAPI.CameraDevice_getProjectionMatrix(matrix);
398  }
399  else
400  {
401  NativeSharedAPI.CameraDevice_getProjectionMatrix(matrix);
402  }
403  }
404 
405  internal bool IsFlipHorizontal()
406  {
407  return flipHorizontal;
408  }
409 
410  internal bool IsFlipVertical()
411  {
412  return flipVertical;
413  }
414 
415  internal byte [] GetCameraFrameBuffer()
416  {
417  if (cameraFrameBuffer == null)
418  {
419  int cameraWidth = GetWidth();
420  int cameraHeight = GetHeight();
421  if (cameraWidth > 0 && cameraHeight > 0)
422  {
423  cameraFrameBuffer = new byte[cameraWidth * cameraHeight * 3];
424  }
425  }
426 
427  if (cameraFrameBuffer != null)
428  {
429  TrackingState trackingState = TrackerManager.GetInstance().GetTrackingState();
430  NativeSharedAPI.CameraDevice_getCameraImagePtr(trackingState.GetTrackingStateCPtr(), cameraFrameBuffer);
431  }
432 
433  return cameraFrameBuffer;
434  }
435  }
436 }
bool SetFlashLightMode(bool toggle)
Turn on/off flash light
ResultCode Start()
Start camera preview
static AbstractConfigurationScriptableObject GetInstance()
Get configuration asset instance
bool SetParam(string key, int min, int max)
Set camera parameter (Android only supported now)
void Stop()
Stop camera preview
void FlipVideo(FlipDirection direction, bool on)
Flip video background
bool SetFocusMode(FocusMode focusMode)
Set camera focus mode
CameraResolution
Supported camera resolution
Definition: CameraDevice.cs:53
bool SetParam(string key, string stringTypeValue)
Set camera parameter (Android only supported now)
static CameraDevice GetInstance()
Get a CameraDevice instance.
Definition: CameraDevice.cs:93
bool SetAutoWhiteBalanceLock(bool toggle)
Toggle auto white balance lock (Android only supported now)
CameraType
Supported camera type (Mobile only)
Definition: CameraDevice.cs:37
ResultCode
API call return value
Definition: ResultCode.cs:15
List< string > GetParamList()
Get parameter key list (Android only supported now)
bool SetParam(string key, int intTypeValue)
Set camera parameter (Android only supported now)
bool SetParam(string key, bool boolTypeValue)
Set camera parameter (Android only supported now)
CameraDevice.CameraResolution CameraResolution
Select camera resolution
FlipDirection
Flip video
Definition: CameraDevice.cs:74
class for camera device handling
Definition: CameraDevice.cs:17
FocusMode
Camera focus mode
Definition: CameraDevice.cs:22