public static NatCamDispatch Prepare(DispatchMode Mode, MonoBehaviour TimesliceProvider = null, int Rate = 15) { NatCamDispatch dispatch = new NatCamDispatch { mode = Mode, mainThread = Thread.CurrentThread, timesliceProvider = TimesliceProvider, invocation = new Queue(), update = new Queue(), execution = new Queue(), running = true }; dispatch.workerThread = Mode == DispatchMode.Asynchronous ? new Thread(() => dispatch.Routine <Camera>(dispatch.Update, Rate)) : null; dispatch.targetThread = Mode == DispatchMode.Asynchronous ? dispatch.workerThread : dispatch.mainThread; if (Mode == DispatchMode.Synchronous) { if (dispatch.timesliceProvider) { dispatch.routine = Routine <Camera>(dispatch.Update, new WaitForEndOfFrame()).Invoke(dispatch.timesliceProvider); } else { Camera.onPostRender += dispatch.Update; } } else { dispatch.workerThread.Start(); } Debug.Log("NatCam: Initialized " + Mode + " Dispatcher"); return(dispatch); }
public static void Release(NatCamDispatch dispatch) { if (dispatch == null) { return; } if (!dispatch.running) { return; } dispatch.running = false; if (dispatch.mode == DispatchMode.Synchronous) { if (dispatch.routine != null) { dispatch.routine.Terminate(dispatch.timesliceProvider); } else { Camera.onPostRender -= dispatch.Update; } } else { dispatch.workerThread.Join(); } Debug.Log("NatCam: Released " + dispatch.mode + " Dispatcher"); }
public static void Decode(Color32[] frame, int width, int height, NatCamDispatch dispatch, BarcodeCallback callback) { if (callback == null) { return; } #if NATCAM_ZXING_DEPENDENCY results = reader.DecodeMultiple(frame, width, height); dispatch.Dispatch(() => results.ForEach(result => callback(NatCamBarcode(result)))); #endif }
public static void Initialize(PreviewType previewType, bool metadataDetection) { mReadablePreview = previewType == PreviewType.Readable; mPhotoSaveMode = (int)SaveMode.DoNotSave; mMetadataDetection = metadataDetection; listener = new GameObject("NatCamHelper").AddComponent <NatCamHelper>(); Dispatch = NatCamDispatch.Prepare(DispatchMode.Synchronous, listener); RegisterCallbacks(Render, Update, UpdatePhoto, UpdateCode, UpdateFace); InspectDeviceCameras(); isInitialized = true; Ext.LogVerbose("Initialized native interface"); }
public static void Release() { #if !ALLOCATE_NEW_FRAME_TEXTURES //NatCam retains ownership if we aren't reallocating if (Photo != null) { MonoBehaviour.Destroy(Photo); } Photo = null; #endif #if !ALLOCATE_NEW_PHOTO_TEXTURES //NatCam retains ownership if we aren't reallocating if (mPreviewFrame != null) { MonoBehaviour.Destroy(mPreviewFrame); } mPreviewFrame = null; #endif if (Preview != null) { MonoBehaviour.Destroy(Preview); } Preview = null; #if OPENCV_DEVELOPER_MODE if (PreviewMatrix != null) { PreviewMatrix.release(); } PreviewMatrix = null; #endif mActiveCamera = -1; mPhotoSaveMode = 0; if (listener != null) { listener.WillDestroyMe(); MonoBehaviour.Destroy(listener.gameObject); } listener = null; #if UNITY_IOS TerminateOperations(); NatCamDispatch.Release(Dispatch); Dispatch = null; #elif UNITY_ANDROID A.Call("TerminateOperations"); NCNA = null; #endif FirstFrameReceived = isInitialized = isPlaying = false; }
private static FaceCallback PropagateFace; //Maybe one day #endregion #region ---Public Ops--- public static void Initialize(bool metadataDetection) { if (!supportedDevice) { return; } mMetadataDetection = metadataDetection; Dispatch = NatCamDispatch.Prepare(DispatchMode.Synchronous); Dispatch.DispatchContinuous(Update); if (mMetadataDetection) { MetadataDispatch = NatCamDispatch.Prepare(DispatchMode.Asynchronous); MetadataDispatch.DispatchContinuous(UpdateMetadata); } isInitialized = true; Ext.LogVerbose("Initialized fallback interface"); }
private static void Render(int request) { #if UNITY_ANDROID if (Dispatch == null) { return; } //Dispatch on the main thread Dispatch.Dispatch(() => { //Invocation Ext.LogVerbose("Native requested callback " + request + " on render thread"); GL.IssuePluginEvent(NatCamNativeCallback(), request); //Release if (request == 3) { NatCamDispatch.Release(Dispatch); Dispatch = null; } }); #endif }
public static void Release() { #if !ALLOCATE_NEW_FRAME_TEXTURES //NatCam retains ownership if we aren't reallocating if (Photo != null) { MonoBehaviour.Destroy(Photo); Photo = null; } #endif #if !ALLOCATE_NEW_PHOTO_TEXTURES //NatCam retains ownership if we aren't reallocating if (mPreviewFrame != null) { MonoBehaviour.Destroy(mPreviewFrame); mPreviewFrame = null; } #endif if (Preview.isPlaying) { Preview.Stop(); } MonoBehaviour.Destroy(Preview); Preview = null; #if OPENCV_DEVELOPER_MODE if (PreviewMatrix != null) { PreviewMatrix.release(); } PreviewMatrix = null; #endif mActiveCamera = -1; PreviewBuffer = metadataBuffer = null; NatCamDispatch.Release(Dispatch); if (mMetadataDetection) { NatCamDispatch.Release(MetadataDispatch); } Dispatch = MetadataDispatch = null; FirstFrameReceived = isInitialized = mMetadataDetection = false; }