public virtual void Start() { //Initialize NatCam NatCam.Initialize(Interface, PreviewType, DetectMetadata); //Set verbose mode NatCam.Verbose = VerboseMode; //Set the active camera NatCam.ActiveCamera = Facing == Facing.Front ? DeviceCamera.FrontCamera : DeviceCamera.RearCamera; //Null checking if (NatCam.ActiveCamera == null) { //Log Ext.Warn("Unitygram: Active camera is null. Consider changing the facing of the camera. Terminating"); //Return return; } //Set the camera's resolution NatCam.ActiveCamera.SetResolution(Resolution); //Set the camera's framerate NatCam.ActiveCamera.SetFramerate(Framerate); //Set the camera's focus mode NatCam.ActiveCamera.FocusMode = FocusMode; //Play NatCam.Play(); //Pass a callback to be executed once the preview starts //Note that this is a MUST when assigning the preview texture to anything NatCam.ExecuteOnPreviewStart(OnPreviewStart); //Register for preview updates NatCam.OnPreviewUpdate += OnPreviewUpdate; }
public void SetFramerate(float framerate) { if (NatCam.IsPlaying) { Ext.Warn("Cannot set frame rate when preview is running"); return; } #if UNITY_IOS if (NatCam.Interface == NatCamInterface.NativeInterface) { Native.SetFramerate(this, framerate); } else { requestedFramerate = framerate; } #elif UNITY_ANDROID if (NatCam.Interface == NatCamInterface.NativeInterface) { Native.A.Call("SetFramerate", (int)this, framerate); } else { requestedFramerate = framerate; } #else requestedFramerate = framerate; #endif }
public void SetPhotoResolution(int width, int height) { if (NatCam.IsPlaying) { Ext.Warn("Cannot set photo resolution when preview is running"); return; } #if UNITY_IOS if (NatCam.Interface == NatCamInterface.NativeInterface) { Native.SetPhotoResolution(this, width, height); } else { Ext.Warn("Cannot set photo resolution on fallback interface"); } #elif UNITY_ANDROID if (NatCam.Interface == NatCamInterface.NativeInterface) { Native.A.Call("SetPhotoResolution", (int)this, width, height); } else { Ext.Warn("Cannot set photo resolution on fallback interface"); } #else Ext.Warn("Cannot set photo resolution on fallback interface"); #endif }
public void SetFocus(Vector2 viewportPoint) { //Check that this camera is active if (NatCam.ActiveCamera.index != index) { Ext.Warn("This camera is not the NatCam ActiveCamera. SetFocus might fail"); } #if UNITY_IOS if (NatCam.Interface == NatCamInterface.NativeInterface) { Native.SetFocus(this, viewportPoint.x, viewportPoint.y); } else { Ext.Warn("SetFocus is not supported on the fallback interface"); } #elif UNITY_ANDROID if (NatCam.Interface == NatCamInterface.NativeInterface) { Native.A.Call("SetFocus", (int)this, viewportPoint.x, viewportPoint.y); } else { Ext.Warn("SetFocus is not supported on the fallback interface"); } #else Ext.Warn("SetFocus is not supported on the fallback interface"); #endif }
public void SetResolution(int width, int height) { if (NatCam.IsPlaying) { Ext.Warn("Cannot set resolution when preview is running"); return; } #if UNITY_IOS if (NatCam.Interface == NatCamInterface.NativeInterface) { Native.SetResolution(this, width, height); } else { requestedResolution = new Vector2(width, height); } #elif UNITY_ANDROID if (NatCam.Interface == NatCamInterface.NativeInterface) { Native.A.Call("SetResolution", (int)this, width, height); } else { requestedResolution = new Vector2(width, height); } #else requestedResolution = new Vector2(width, height); #endif }
public static void RequestFace(params FaceCallback[] callbacks) { if (Interface == NatCamInterface.FallbackInterface) { Ext.Warn("Cannot request face detection on fallback interface"); return; } if (!Native.mMetadataDetection) { Ext.Warn("NatCam was initialized without preparing for metadata detection. Ignoring call"); return; } if (callbacks.Length == 0) { Ext.Warn("Cannot request faces with no callbacks supplied"); return; } for (int i = 0; i < callbacks.Length; i++) { if (callbacks[i] != null) { OnFaceDetect += callbacks[i]; } } }
public static void RequestBarcode(params BarcodeRequest[] requests) { if ((Interface == NatCamInterface.NativeInterface && !Native.mMetadataDetection) || (Interface == NatCamInterface.FallbackInterface && !Fallback.mMetadataDetection)) { Ext.Warn("NatCam was initialized without preparing for metadata detection. Ignoring call"); return; } if (requests.Length == 0) { Ext.Warn("Cannot request barcode with no requests supplied"); return; } for (int i = 0; i < requests.Length; i++) { BarcodeRequest req = requests[i]; BarcodeCallback temp = null; temp = delegate(Barcode code) { if ((code.format & req.format) > 0) { if (req.callback != null) { req.callback(code); //Null checking saves one, kills none } if (req.detectOnce) { OnBarcodeDetect -= temp; } } }; OnBarcodeDetect += temp; } }
public void Apply(Vector2 customResolution) { //Null checking if (graphic == null) { Ext.Warn("Scaler Apply failed, there is no graphic attached to the gameObject"); return; } //Apply InternalApply(customResolution).Invoke(this); }
public static void SaveToPhotos(Texture2D photo, SaveMode saveMode = SaveMode.SaveToPhotoGallery) //DEPLOY { if (Interface == NatCamInterface.FallbackInterface) { Ext.Warn("Cannot save photo on fallback interface"); return; } if (saveMode == SaveMode.DoNotSave) { Ext.Warn("Don't ask me to save a photo then say I shouldn't :/"); //Sassy return; } Native.SaveToPhotos(photo, (int)saveMode); }
public virtual void OnPreviewStart() { //Set the RawImage texture once the preview starts if (RawImage != null) { RawImage.texture = NatCam.Preview; } //Log else { Ext.Warn("Unitygram: Preview RawImage has not been set"); } //Log Ext.LogVerbose("Unitygram: Preview resolution: " + NatCam.ActiveCamera.ActiveResolution); }
Vector2 Regularize(Texture tex) { Vector2 input = new Vector2(tex.width, tex.height); bool isPortrait = Screen.orientation == ScreenOrientation.Portrait || Screen.orientation == ScreenOrientation.PortraitUpsideDown || (Screen.orientation == ScreenOrientation.AutoRotation && (Input.deviceOrientation == DeviceOrientation.Portrait || Input.deviceOrientation == DeviceOrientation.PortraitUpsideDown)); //This is the only appropriate flag int min = Mathf.RoundToInt(Mathf.Min(input.x, input.y)), max = Mathf.RoundToInt(Mathf.Max(input.x, input.y)); Ext.LogVerbose("PreviewScaler: orientation-" + Screen.orientation + " portrait-" + isPortrait + " min-" + min + " max-" + max); return(new Vector2 { x = isPortrait ? min : max, y = isPortrait ? max : min }); }
public static void CapturePhoto(params PhotoCallback[] callbacks) { if (!IsPlaying) { Ext.Warn("Cannot capture photo when session is not running"); return; } if (callbacks.Length == 0 && OnPhotoCapture == null) { Ext.Warn("Cannot capture photo because there is no callback subscribed"); return; } else if (callbacks.Length > 0) { for (int i = 0; i < callbacks.Length; i++) { PhotoCallback callback = callbacks[i]; PhotoCallback captureWrapper = null; captureWrapper = (Texture2D photo) => { if (callback != null) { callback(photo); } OnPhotoCapture -= captureWrapper; }; OnPhotoCapture += captureWrapper; } } if (Interface == NatCamInterface.NativeInterface) { Native.CapturePhoto(); } else { Fallback.CapturePhoto(); } }
public static void Initialize(NatCamInterface NatCamInterface = NatCamInterface.NativeInterface, PreviewType PreviewType = PreviewType.NonReadable, Switch MetadataDetection = Switch.Off) { Interface = NatCamInterface; if (Interface == NatCamInterface.NativeInterface && !IsSupportedDevice) { Ext.Warn("Running on an unsupported platform or a device without cameras. Falling back to Fallback"); Interface = NatCamInterface.FallbackInterface; } switch (Interface) { case NatCamInterface.NativeInterface: Native.Initialize(PreviewType, MetadataDetection == Switch.On); Native.RegisterEvents(PropagateStart, PropagateUpdate, PropagatePhoto, PropagateBarcode, PropagateFace, SetApplicationFocus); break; case NatCamInterface.FallbackInterface: Fallback.Initialize(MetadataDetection == Switch.On); Fallback.RegisterEvents(PropagateStart, PropagateUpdate, PropagatePhoto, PropagateBarcode, PropagateFace, SetApplicationFocus); break; default: goto case NatCamInterface.FallbackInterface; } }