예제 #1
0
 public void DispatchContinuous(Action action)
 {
     lock (queueLock) {
         Ext.LogVerbose("Dispatch Enqueue Update");
         update.Add(action);
     }
 }
예제 #2
0
 public static void CapturePhoto()
 {
     if (!supportedDevice)
     {
         Ext.Warn("Current device has no cameras");
         return;
     }
     if (Preview == null)
     {
         return;
     }
     if (!Preview.isPlaying)
     {
         return;
     }
     #if ALLOCATE_NEW_PHOTO_TEXTURES
     Photo = new Texture2D(Preview.width, Preview.height);
     #else
     if (Photo == null)
     {
         Photo = new Texture2D(Preview.width, Preview.height);
     }
     #endif
     lock (bufferLock) {
         Preview.GetPixels32(PreviewBuffer);
         Photo.SetPixels32(PreviewBuffer);
     }
     Photo.Apply();
     PropagatePhoto(Photo);
 }
예제 #3
0
 private static void UpdatePhoto(UIntPtr JPGPtr, UIntPtr width, UIntPtr height, UIntPtr size)
 {
     //Dispatch on main thread
     Dispatch.Dispatch(() => {
         if (JPGPtr == UIntPtr.Zero)
         {
             Ext.Warn("Failed to retrieve captured photo from device");
             return;
         }
         Photo =
                             #if ALLOCATE_NEW_PHOTO_TEXTURES
             new Texture2D((int)width, (int)height, TextureFormat.RGBA32, false);
                             #else
             Photo ?? new Texture2D((int)width, (int)height, TextureFormat.RGBA32, false);
         if (Photo.width != (int)width || Photo.height != (int)height)                      //Realloc if size is different
         {
             MonoBehaviour.Destroy(Photo);
             Photo = new Texture2D((int)width, (int)height, TextureFormat.RGBA32, false);
         }
                             #endif
         Photo.LoadRawTextureData(unchecked ((IntPtr)(long)(ulong)JPGPtr), (int)size);
         Photo.Apply();
         ReleasePhotoBuffer();
         PropagatePhoto(Photo);
     });
 }
예제 #4
0
 public static void Play()
 {
     if (!supportedDevice)
     {
         Ext.Warn("Current device has no cameras");
         return;
     }
     if (Preview == null)                                   //First Play()
     {
         if (((Vector2)(DeviceCamera)mActiveCamera).x == 0) //Default
         {
             Preview = new WebCamTexture(WebCamTexture.devices[mActiveCamera].name);
         }
         else   //Resolution set
         {
             Vector2 resolution = (Vector2)(DeviceCamera)mActiveCamera;
             float   frameRate  = (float)(DeviceCamera)mActiveCamera;
             if ((int)frameRate == 0)
             {
                 Preview = new WebCamTexture(WebCamTexture.devices[mActiveCamera].name, (int)resolution.x, (int)resolution.y);
             }
             else
             {
                 Preview = new WebCamTexture(WebCamTexture.devices[mActiveCamera].name, (int)resolution.x, (int)resolution.y, (int)frameRate);
             }
         }
     }
     Preview.Play();
 }
예제 #5
0
 public static void Pause()
 {
     if (!supportedDevice)
     {
         Ext.Warn("Current device has no cameras");
         return;
     }
     Preview.Pause();
 }
예제 #6
0
 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");
 }
예제 #7
0
 public static void SwitchActiveCamera()
 {
     if (!supportedDevice)
     {
         Ext.Warn("Current device has no cameras");
         return;
     }
     FirstFrameReceived = false;
     Preview.Stop();
     Preview.deviceName = WebCamTexture.devices[mActiveCamera].name;
     Preview.Play();
 }
예제 #8
0
 private static void Update(IntPtr RGBA32GPUPtr, UIntPtr RGBA32Ptr, UIntPtr width, UIntPtr height, UIntPtr size)
 {
     //Dispatch on main thread
     Dispatch.Dispatch(() => {
         Ext.LogVerbose("Received native update: " + string.Format("ptr<{0}>, ptr<{1}>, {2}, {3}, {4}", RGBA32GPUPtr.ToInt32().ToString(), RGBA32Ptr.ToUInt32().ToString(), width.ToString(), height.ToString(), size.ToString()));
         //Initialization
         Preview = Preview ?? Texture2D.CreateExternalTexture((int)width, (int)height, TextureFormat.RGBA32, false, false, RGBA32GPUPtr);
         //Size checking
         if (Preview.width != (int)width || Preview.height != (int)height)
         {
             Preview.Resize((int)width, (int)height, Preview.format, false);
         }
         //Update
         Preview.UpdateExternalTexture(RGBA32GPUPtr);
         //Propagation
         if (OnNativePreviewUpdate != null)
         {
             OnNativePreviewUpdate(ComponentBuffer.RGBA32GPU, unchecked ((UIntPtr)(ulong)(long)RGBA32GPUPtr), (int)width, (int)height, (int)size);
             if (mReadablePreview)
             {
                 OnNativePreviewUpdate(ComponentBuffer.RGBA32, RGBA32Ptr, (int)width, (int)height, (int)size);
             }
         }
         //OpenCV
         #if OPENCV_DEVELOPER_MODE
         if (mReadablePreview)
         {
             PreviewMatrix = PreviewMatrix ?? new Mat(new Size((int)width, (int)height), CvType.CV_8UC4);
             if (PreviewMatrix.cols() != (int)width || PreviewMatrix.rows() != (int)height)
             {
                 Imgproc.resize(PreviewMatrix, PreviewMatrix, new Size((int)width, (int)height));
             }
             Utils.copyToMat(unchecked ((IntPtr)(long)(ulong)RGBA32Ptr), PreviewMatrix);
             //Core.flip (PreviewMatrix, PreviewMatrix, 0); //Dev should do this themselves
         }
         #endif
         //Propagation
         if (
             !FirstFrameReceived
             #if UNITY_ANDROID
             && !A.Get <bool>("orientationDirty") //In NCNA, this flag is bound to active camera state
             #endif
             )
         {
             PropagateStart(); FirstFrameReceived = isPlaying = true;
         }
         if (FirstFrameReceived)
         {
             PropagateUpdate();
         }
     });
 }
예제 #9
0
 public void Dispatch(Action action)
 {
     //Check that we aren't already on the target thread
     if (Thread.CurrentThread.ManagedThreadId == targetThread.ManagedThreadId)
     {
         Ext.LogVerbose("Dispatch Execute");
         action();
     }
     //Enqueue
     else
     {
         lock (queueLock) {
             Ext.LogVerbose("Dispatch Enqueue");
             invocation.Add(action);
         }
     }
 }
예제 #10
0
            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");
            }
예제 #11
0
            private static void UpdateCode(int format, IntPtr ptr, UIntPtr size)
            {
                //Get the string immediately since we can't guarantee memory preservation
                if (ptr == IntPtr.Zero)
                {
                    Ext.Warn("Detected barcode string does not exist");
                    return;
                }
                string code = Marshal.PtrToStringUni(ptr, unchecked ((int)(uint)size));

                if (code == null)
                {
                    Ext.Warn("Detected barcode string is null");
                    return;
                }
                //Dispatch on main thread
                Dispatch.Dispatch(() => {
                    PropagateBarcode(new Barcode(code, (BarcodeFormat)format));
                });
            }
예제 #12
0
 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
 }