Exemplo n.º 1
0
 /// <summary>
 /// Starts the camera.
 /// </summary>
 public override void Play()
 {
     if (hasInitDone)
     {
         NatCam.Play();
     }
 }
Exemplo n.º 2
0
    public override void Photograph(Action <Texture2D> onImageAction)
    {
        try
        {
            NatCam.Camera.SetPhotoResolution(512, 512);

            NatCam.CapturePhoto((image, orientation) =>
            {
                onImageAction(OrientatePhoto(image, Input.deviceOrientation));
            });
        }
        catch (Exception e)
        {
            Debug.Log("Failed to take photo, trying a low res version");
            Debug.Log(e.Message);

            try
            {
                NatCam.Camera.SetPhotoResolution(ResolutionPreset.LowestResolution);

                NatCam.CapturePhoto((image, orientation) =>
                {
                    onImageAction(OrientatePhoto(image, Input.deviceOrientation));
                });
            }
            catch (Exception lowResAttemptException)
            {
                Debug.Log("Failed to take photo");
                Debug.Log(lowResAttemptException.Message);
            }
        }
    }
Exemplo n.º 3
0
    public void clickReset()
    {
        NatCam.Stop();
        Scene csc = SceneManager.GetActiveScene();

        SceneManager.LoadScene(csc.name);
    }
        bool PreviewBuffer(ref Color32[] colors, out int width, out int height)
        {
            width  = NatCam.Preview.width;
            height = NatCam.Preview.height;
            var pixelCount         = width * height;
            var rawFrameBytesCount = pixelCount * 4;

            lock (threadLock)
            {
                if ((rawFrameBytes == null) || (rawFrameBytes.Length != rawFrameBytesCount))
                {
                    rawFrameBytes = new byte[rawFrameBytesCount];
                }

                if (!NatCam.CaptureFrame(rawFrameBytes))
                {
                    return(false);
                }

                if ((colors == null) || (colors.Length != pixelCount))
                {
                    colors = new Color32[pixelCount];
                }

                var handle = GCHandle.Alloc(colors, GCHandleType.Pinned);
                Marshal.Copy(rawFrameBytes, 0, handle.AddrOfPinnedObject(), rawFrameBytesCount);
                handle.Free();
            }

            return(true);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Releases all resource.
        /// </summary>
        private void Dispose()
        {
            NatCam.Release();

            if (matrix != null)
            {
                matrix.Dispose();
                matrix = null;
            }
            if (grayMatrix != null)
            {
                grayMatrix.Dispose();
                grayMatrix = null;
            }
            if (texture != null)
            {
                Texture2D.Destroy(texture);
                texture = null;
            }

            didUpdateThisFrame = false;

            //Reset material
            if (preview)
            {
                preview.material = originalMaterial;
            }
            //Destroy view material
            Destroy(viewMaterial);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Method called when the camera preview starts
        /// </summary>
        public virtual void OnStart()
        {
            // Create pixel buffer
            pixelBuffer = new byte[NatCam.Preview.width * NatCam.Preview.height * 4];

            // Get the preview data
            NatCam.CaptureFrame(pixelBuffer, true);

            // Create preview matrix
            if (matrix != null && (matrix.cols() != NatCam.Preview.width || matrix.rows() != NatCam.Preview.height))
            {
                matrix.Dispose();
                matrix = null;
            }
            matrix = matrix ?? new Mat(NatCam.Preview.height, NatCam.Preview.width, CvType.CV_8UC4);
            Utils.copyToMat(pixelBuffer, matrix);

            // Create display texture
            if (texture && (texture.width != matrix.cols() || texture.height != matrix.rows()))
            {
                Texture2D.Destroy(texture);
                texture = null;
            }
            texture = texture ?? new Texture2D(matrix.cols(), matrix.rows(), textureFormat, false, false);

            // Scale the panel to match aspect ratios
            aspectFitter.aspectRatio = NatCam.Preview.width / (float)NatCam.Preview.height;

            // Display the result
            preview.texture = texture;

            Debug.Log("OnStart (): " + matrix.cols() + " " + matrix.rows() + " " + NatCam.Preview.width + " " + NatCam.Preview.height + " " + texture.width + " " + texture.height);
        }
Exemplo n.º 7
0
 /// <summary>
 /// Stops the active camera.
 /// </summary>
 public override void Stop()
 {
     if (hasInitDone)
     {
         NatCam.Pause();
     }
 }
Exemplo n.º 8
0
 void OnDisable()
 {
     if (!willDestroy)
     {
         NatCam.Release();               //We check that this wasn't caused by NatCam.Release() before calling NatCam.Release();
     }
 }
Exemplo n.º 9
0
    public static void StartCamera()
    {
        _savedCounter = 0;
        DeviceCamera camera = DeviceCamera.RearCamera;

        if (camera == null)
        {
            camera = DeviceCamera.FrontCamera;
            if (camera == null)
            {
                Debug.LogWarning("No device camera found.");
                return;
            }
        }

        Vector2Int newSize = new Vector2Int(Mathf.FloorToInt(Screen.width / 2f), Mathf.FloorToInt(Screen.height / 2f));

        camera.PhotoResolution = camera.PreviewResolution = newSize;
        camera.Framerate       = 30;
        Debug.Log("New PhotoResolution size: " + newSize);

        NatCam.StartPreview(camera, OnStart);

        Clear();
    }
Exemplo n.º 10
0
 public override void Start()
 {
     // Request barcodes // NOTE: Since 1.5, you must request metadata before calling Play()
     NatCam.DetectBarcode(OnBarcode, format, false);
     // Base
     base.Start();
 }
Exemplo n.º 11
0
        /// <summary>
        /// Method called on every frame that the camera preview updates
        /// </summary>
        public virtual void OnFrame()
        {
            onFrameCount++;

            if (imageProcessingType == ImageProcessingType.None)
            {
                didUpdateThisFrame = true;
                preview.texture    = NatCam.Preview;
            }
            else
            {
                // Size checking
                if (buffer != null && buffer.Length != NatCam.Preview.width * NatCam.Preview.height * 4)
                {
                    buffer = null;
                }

                // Create the managed buffer
                buffer = buffer ?? new byte[NatCam.Preview.width * NatCam.Preview.height * 4];

                // Capture the current frame
                NatCam.CaptureFrame(buffer);

                // Size checking
                if (texture && (texture.width != NatCam.Preview.width || texture.height != NatCam.Preview.height))
                {
                    Texture2D.Destroy(texture);
                    texture = null;
                }
                // Create the texture
                texture = texture ?? new Texture2D(NatCam.Preview.width, NatCam.Preview.height, textureFormat, false, false);

                didUpdateThisFrame = true;
            }
        }
Exemplo n.º 12
0
 /// <summary>
 /// Pause this instance.
 /// </summary>
 public void Pause()
 {
     if (NatCam.IsPlaying)
     {
         NatCam.Pause();
     }
 }
Exemplo n.º 13
0
 /// <summary>
 /// Play this instance.
 /// </summary>
 public void Play()
 {
     if (!NatCam.IsPlaying)
     {
         NatCam.Play();
     }
 }
Exemplo n.º 14
0
        private const long FaceExpirationEps = 300L;         // After 300ms of not being detected, consider face expired

        public override void Start()
        {
            // Subscribe for face detection // NOTE: Since 1.5, you must request metadata before calling Play()
            NatCam.DetectFace(OnFace);
            // base
            base.Start();
        }
Exemplo n.º 15
0
 void Update()
 {
     // Enumerate touches
     foreach (Touch t in Input.touches)
     {
         // Check for double tap
         if (t.phase == TouchPhase.Ended && t.tapCount == 2)
         {
             // Toggle recording
             if (NatCam.IsRecording)
             {
                 NatCam.StopRecording();
             }
             else
             {
                 NatCam.StartRecording(OnVideoSave);
             }
         }
     }
     // Set IsRecording property on RecordingIcon
     if (recordingIcon)
     {
         recordingIcon.IsRecording = NatCam.IsRecording;
     }
 }
Exemplo n.º 16
0
        }                                  // Don't do anything in OnStart

        public override void OnFrame()
        {
            // Get the preview matrix and call OnMatrix
            if (NatCam.PreviewMatrix(ref _matrix))
            {
                OnMatrix();
            }
        }
Exemplo n.º 17
0
    public static void StopCamera()
    {
        NatCam.StopPreview();

        // clear the memory
        Resources.UnloadUnusedAssets();

        OnCameraComplete?.Invoke(_previews);
    }
Exemplo n.º 18
0
        public virtual void Start()
        {
            fpsMonitor = GetComponent <FpsMonitor> ();

            if (!NatCam.Implementation.HasPermissions)
            {
                Debug.LogError("NatCam.Implementation.HasPermissions == false");

                if (fpsMonitor != null)
                {
                    fpsMonitor.consoleText = "NatCam.Implementation.HasPermissions == false";
                }
            }

            // Load global camera benchmark settings.
            int width, height, fps;

            NatCamWithOpenCVForUnityExample.GetCameraResolution(out width, out height);
            NatCamWithOpenCVForUnityExample.GetCameraFps(out fps);
            previewResolution = new NatCamU.Core.CameraResolution(width, height);
            requestedFPS      = fps;

            // Set the active camera
            NatCam.Camera = useFrontCamera ? DeviceCamera.FrontCamera : DeviceCamera.RearCamera;

            // Null checking
            if (!NatCam.Camera)
            {
                Debug.LogError("Camera is null. Consider using " + (useFrontCamera ? "rear" : "front") + " camera");
                return;
            }
            if (!preview)
            {
                Debug.LogError("Preview RawImage has not been set");
                return;
            }

            // Set the camera's preview resolution
            NatCam.Camera.PreviewResolution = previewResolution;
            // Set the camera framerate
            NatCam.Camera.Framerate = requestedFPS;
            NatCam.Play();
            NatCam.OnStart += OnStart;
            NatCam.OnFrame += OnFrame;

            if (fpsMonitor != null)
            {
                fpsMonitor.Add("Name", "NatCamPreviewOnlyExample");
                fpsMonitor.Add("onFrameFPS", onFrameFPS.ToString("F1"));
                fpsMonitor.Add("drawFPS", drawFPS.ToString("F1"));
                fpsMonitor.Add("width", "");
                fpsMonitor.Add("height", "");
                fpsMonitor.Add("orientation", "");
            }

            imageProcessingTypeDropdown.value = (int)imageProcessingType;
        }
Exemplo n.º 19
0
    private void ReleaseCamera()
    {
        if (NatCam.ActiveCamera != null)
        {
            NatCam.Release();
        }

        initialized = false;
    }
Exemplo n.º 20
0
 void OnFrame()
 {
     // Capture the preview frame
     NatCam.CaptureFrame(buffer);
     // Convert to greyscale
     ConvertToGrey(buffer);
     // Fill the texture with the greys
     texture.LoadRawTextureData(buffer);
     texture.Apply();
 }
Exemplo n.º 21
0
        public virtual void CapturePhoto()
        {
            // Divert control if we are checking the captured photo
            //if (!checkIco.gameObject.activeInHierarchy) NatCam.CapturePhoto(OnPhoto);
            // Check captured photo
            //else OnViewPhoto();

            Debug.Log("CapturePhoto() called, NatCam.Camera.PreviewResolution = " + NatCam.Camera.PreviewResolution);
            NatCam.CapturePhoto(OnPhoto);
        }
Exemplo n.º 22
0
    private void InitCamera()
    {
        Debug.Log("InitCamera() called, starting natcam");
                #if (UNITY_IOS || UNITY_ANDROID) && !UNITY_EDITOR
        NatCam.Initialize(NatCamInterface.NativeInterface, PreviewType.NonReadable, Switch.Off);
                #else
        NatCam.Initialize(NatCamInterface.FallbackInterface, PreviewType.NonReadable, Switch.Off);
                #endif

        initialized = true;
    }
Exemplo n.º 23
0
        void Start()
        {
            var camera = useFrontCamera ? DeviceCamera.FrontCamera : DeviceCamera.RearCamera;

            if (!camera)
            {
                Debug.LogError("Camera is null. Consider using " + (useFrontCamera ? "rear" : "front") + " camera");
                return;
            }
            camera.PreviewResolution = new Vector2Int(640, 480);
            NatCam.StartPreview(camera, OnStart, OnFrame);
        }
Exemplo n.º 24
0
 public void CapturePhoto()
 {
     //Divert control if we are checking the captured photo
     if (checkIco.gameObject.activeInHierarchy)
     {
         OnCheckedPhoto();
         //Terminate this control chain
         return;
     }
     //Capture photo with our callback
     NatCam.CapturePhoto(OnCapturedPhoto);
 }
Exemplo n.º 25
0
    // Use this for initialization
    public override void Start()
    {
        //Start base
        base.Start();
        //Create a barcode detection request
        BarcodeRequest request = new BarcodeRequest(OnDetectBarcode, detectionFormat, !continuousDetection);         //Negate continuousDetection because when true, we don't want to automatically unsubscribe our callback

        //Request barcode detection
        NatCam.RequestBarcode(request);
        //Set the flash icon
        SetFlashIcon();
    }
Exemplo n.º 26
0
        /// <summary>
        /// Releases all resource.
        /// </summary>
        private void Dispose()
        {
            NatCam.Release();

            if (texture != null)
            {
                Texture2D.Destroy(texture);
                texture = null;
            }

            didUpdateThisFrame = false;
        }
Exemplo n.º 27
0
 public void SwitchCamera()
 {
     // Switch camera
     if (NatCam.Camera.IsFrontFacing)
     {
         NatCam.StartPreview(DeviceCamera.RearCamera, OnStart);
     }
     else
     {
         NatCam.StartPreview(DeviceCamera.FrontCamera, OnStart);
     }
 }
Exemplo n.º 28
0
    public override void StartCameraPreview()
    {
        if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
        {
            NatCam.Play(DeviceCamera.RearCamera);
        }
        else
        {
            NatCam.Play(DeviceCamera.FrontCamera);
        }

        SetCameraPreviewTexture();
    }
Exemplo n.º 29
0
 public virtual void CapturePhoto()
 {
     // Divert control if we are checking the captured photo
     if (!checkIco.gameObject.activeInHierarchy)
     {
         NatCam.CapturePhoto(OnPhoto);
     }
     // Check captured photo
     else
     {
         OnView();
     }
 }
Exemplo n.º 30
0
 // Use this for initialization
 private void Start()
 {
     // Start NatCam
     NatCam.Camera = useFrontCamera ? DeviceCamera.FrontCamera : DeviceCamera.RearCamera;
     if (!NatCam.Camera)
     {
         Debug.LogError("Camera is null. Consider using " + (useFrontCamera ? "rear" : "front") + " camera");
         return;
     }
     NatCam.Camera.PreviewResolution = CameraResolution._1920x1080;
     NatCam.Camera.PhotoResolution   = CameraResolution._1920x1080;
     NatCam.Play();
     NatCam.OnStart += OnStart;
 }