Esempio n. 1
0
 /// <summary>
 /// Play this instance.
 /// </summary>
 public void Play()
 {
     if (!NatCam.IsPlaying)
     {
         NatCam.Play();
     }
 }
Esempio n. 2
0
 /// <summary>
 /// Starts the camera.
 /// </summary>
 public override void Play()
 {
     if (hasInitDone)
     {
         NatCam.Play();
     }
 }
Esempio n. 3
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;
        }
Esempio n. 4
0
    public override void StartCameraPreview()
    {
        if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
        {
            NatCam.Play(DeviceCamera.RearCamera);
        }
        else
        {
            NatCam.Play(DeviceCamera.FrontCamera);
        }

        SetCameraPreviewTexture();
    }
Esempio n. 5
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;
 }
Esempio n. 6
0
    public void SwitchCamera()
    {
        // Switch camera
        if (NatCam.Camera.IsFrontFacing)
        {
            NatCam.Camera = DeviceCamera.RearCamera;
        }
        else
        {
            NatCam.Camera = DeviceCamera.FrontCamera;
        }

        NatCam.Play();
    }
Esempio n. 7
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 = CameraResolution._640x480;
            NatCam.Play(camera);
            NatCam.OnStart += OnStart;
            NatCam.OnFrame += OnFrame;
        }
Esempio n. 8
0
    // Use this for initialization
    void Start()
    {
        NatCam.Camera = DeviceCamera.FrontCamera ?? DeviceCamera.Cameras[0];

        if (!NatCam.Camera)
        {
            Debug.LogError("Camera is null");
            return;
        }
        NatCam.Camera.PreviewResolution = CameraResolution._1920x1080;
        NatCam.Camera.PhotoResolution   = CameraResolution._1920x1080;
        NatCam.Play();


        NatCam.OnStart += NatCam_OnStart;
    }
Esempio n. 9
0
    // 初始化摄像头
    public IEnumerator InitCamera()
    {
        yield return(Application.RequestUserAuthorization(UserAuthorization.WebCam));

        if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            // Set verbose mode
            NatCam.Verbose = verbose;
            // Set the active camera
            NatCam.Camera = facing == Facing.Front ? DeviceCamera.FrontCamera : DeviceCamera.RearCamera;
            if (!NatCam.Camera)
            {
                NatCam.Camera = DeviceCamera.RearCamera;
            }
            // Null checking
            if (!NatCam.Camera)
            {
                // Log
                Debug.LogError("No camera detected!");
                StopCoroutine("InitCamera");
                yield return(null);
            }
            // Set the camera's preview resolution
            NatCam.Camera.SetPreviewResolution(previewResolution);
            // Set the camera's photo resolution
            NatCam.Camera.SetPhotoResolution(photoResolution);
            // Set the camera's framerate
            NatCam.Camera.SetFramerate(framerate);
            // Play
            NatCam.Play();
            // Register callback for when the preview starts //Note that this is a MUST when assigning the preview texture to anything
            NatCam.OnStart += OnStart;

            SelfAdjusSize();
            FaceunityWorker.FixRotation(NatCam.Camera.Facing != Facing.Front);

#if UNITY_EDITOR || UNITY_STANDALONE
            if (img_handle.IsAllocated)
            {
                img_handle.Free();
            }
            webtexdata = new Color32[(int)NatCam.Camera.PreviewResolution.x * (int)NatCam.Camera.PreviewResolution.y];
            img_handle = GCHandle.Alloc(webtexdata, GCHandleType.Pinned);
            p_img_ptr  = img_handle.AddrOfPinnedObject();
#endif
        }
    }
Esempio n. 10
0
    private void ActivateCamera(CameraFacing facing)
    {
        if (!initialized)
        {
            return;
        }

        // Any spray compositing that happens while the camera is initializing should spray pitch black.
        activeTexture       = Texture2D.blackTexture;
        staleFramesReceived = 0;

        var device = (facing == CameraFacing.Front) ? DeviceCamera.FrontCamera : DeviceCamera.RearCamera;

        if (device != null)
        {
            // We can't change the resolution while the preview is playing.
            if (NatCam.IsPlaying)
            {
                NatCam.Pause();
            }

            NatCam.ActiveCamera     = device;
            NatCam.OnPreviewStart  += OnPreviewStart;
            NatCam.OnPreviewUpdate += OnPreviewUpdate;

            NatCam.ActiveCamera.SetFramerate(deviceCameraFramerate);
            NatCam.ActiveCamera.SetResolution(deviceCameraResolution);

            // The exposure and focus modes are only available on the native interface.
            if (NatCam.Interface == NatCamInterface.NativeInterface)
            {
                NatCam.ActiveCamera.FocusMode    = deviceCameraFocusMode;
                NatCam.ActiveCamera.ExposureMode = deviceCameraExposureMode;
            }

            NatCam.Play();

            if (FacingChanged != null)
            {
                FacingChanged(facing);
            }
        }
        else if (Debug.isDebugBuild)
        {
            Debug.LogError("CameraCapture: no camera for this orientation");
        }
    }
        public NatCamCameraSource(CameraResolution cameraResolution)
        {
            NatCam.Camera = DeviceCamera.RearCamera ?? DeviceCamera.FrontCamera;
            NatCam.Camera.PreviewResolution = cameraResolution;
            //NatCam.Camera.Framerate = FrameratePreset.Default;

            if (!Application.isEditor) // "NatCam Error: Focus mode is not supported on legacy"
            {
                NatCam.Camera.FocusMode = FocusMode.AutoFocus;
            }

            NatCam.Play();

            NatCam.OnStart += OnStart;
            NatCam.OnFrame += OnFrame;

            frameCounter = 0;
        }
        public void SetActive(bool active)
        {
            /*
             * if (active == NatCam.IsPlaying)
             *  return;
             */

            if (active)
            {
                NatCam.Play();
                IsPlaying = true;
            }
            else
            {
                NatCam.Pause();
                IsPlaying = false;
            }
        }
Esempio n. 13
0
 /// <summary>
 /// Raises the play button click event.
 /// </summary>
 public void OnPlayButtonClick()
 {
     NatCam.Play();
 }
Esempio n. 14
0
        /// <summary>
        /// Initializes this instance by coroutine.
        /// </summary>
        protected override IEnumerator _Initialize()
        {
            if (!NatCam.Implementation.HasPermissions)
            {
                Debug.LogError("NatCam.Implementation.HasPermissions == false");
            }

            if (hasInitDone)
            {
                ReleaseResources();

                if (onDisposed != null)
                {
                    onDisposed.Invoke();
                }
            }

            isInitWaiting = true;


            // Creates the camera
            if (!String.IsNullOrEmpty(requestedDeviceName))
            {
                int requestedDeviceIndex = -1;
                if (Int32.TryParse(requestedDeviceName, out requestedDeviceIndex))
                {
                    if (requestedDeviceIndex >= 0 && requestedDeviceIndex < DeviceCamera.Cameras.Length)
                    {
                        NatCam.Camera = DeviceCamera.Cameras [requestedDeviceIndex];
                    }
                }
                if (NatCam.Camera == null)
                {
                    Debug.Log("Cannot find camera device " + requestedDeviceName + ".");
                }
            }

            NatCam.Camera = requestedIsFrontFacing ? DeviceCamera.FrontCamera : DeviceCamera.RearCamera;

            if (NatCam.Camera == null)
            {
                if (DeviceCamera.Cameras.Length > 0)
                {
                    NatCam.Camera = DeviceCamera.Cameras [0];
                }
                else
                {
                    isInitWaiting = false;

                    if (onErrorOccurred != null)
                    {
                        onErrorOccurred.Invoke(ErrorCode.CAMERA_DEVICE_NOT_EXIST);
                    }

                    yield break;
                }
            }

            NatCam.Camera.Framerate = requestedFPS;

            // Set the camera's preview resolution
            NatCam.Camera.PreviewResolution = new CameraResolution(requestedWidth, requestedHeight);

            // Register callback for when the preview starts
            // Note that this is a MUST when assigning the preview texture to anything
            NatCam.OnStart += OnStart;
            // Register for preview updates
            NatCam.OnFrame += OnFrame;

            // Starts the camera
            NatCam.Play();

            int  initFrameCount = 0;
            bool isTimeout      = false;

            while (true)
            {
                if (initFrameCount > timeoutFrameCount)
                {
                    isTimeout = true;
                    break;
                }
                else if (didUpdateThisFrame)
                {
                    Debug.Log("NatCamPreviewToMatHelper:: " + " width:" + NatCam.Preview.width + " height:" + NatCam.Preview.height + " fps:" + NatCam.Camera.Framerate
                              + " isFrongFacing:" + NatCam.Camera.IsFrontFacing);

                    frameMat = new Mat(NatCam.Preview.height, NatCam.Preview.width, CvType.CV_8UC4);

                    if (rotate90Degree)
                    {
                        rotatedFrameMat = new Mat(NatCam.Preview.width, NatCam.Preview.height, CvType.CV_8UC4);
                    }

                    isInitWaiting = false;
                    hasInitDone   = true;
                    initCoroutine = null;

                    if (onInitialized != null)
                    {
                        onInitialized.Invoke();
                    }

                    break;
                }
                else
                {
                    initFrameCount++;
                    yield return(null);
                }
            }

            if (isTimeout)
            {
                // Unregister from NatCam callbacks
                NatCam.OnStart -= OnStart;
                NatCam.OnFrame -= OnFrame;
                NatCam.Release();

                isInitWaiting = false;
                initCoroutine = null;

                if (onErrorOccurred != null)
                {
                    onErrorOccurred.Invoke(ErrorCode.TIMEOUT);
                }
            }
        }
Esempio n. 15
0
    public void clickStart()
    {
        if (camType == 0)
        {
            NatCam.SetActiveCamera(DeviceCamera.FrontCamera);
        }
        else if (camType == 1)
        {
            NatCam.SetActiveCamera(DeviceCamera.RearCamera);
        }

        if (resolType == 0)
        {
            NatCam.SetResolution(ResolutionPreset.Default);
        }
        else if (resolType == 1)
        {
            NatCam.SetResolution(ResolutionPreset.MediumResolution);
        }
        else if (resolType == 2)
        {
            NatCam.SetResolution(ResolutionPreset.LowestResolution);
        }

        if (focusType == 0)
        {
            NatCam.FocusMode = FocusMode.AutoFocus;
        }
        else if (focusType == 1)
        {
            NatCam.FocusMode = FocusMode.HybridFocus;
        }
        else if (focusType == 2)
        {
            NatCam.FocusMode = FocusMode.Off;
        }

        if (displaySizeType == 0)
        {
            transform.localScale = new Vector3(5f, 10f, 1f);
        }
        else if (displaySizeType == 1)
        {
            transform.localScale = new Vector3(5f, 6f, 1f);
        }
        else if (displaySizeType == 1)
        {
            transform.localScale = new Vector3(5f, 3f, 1f);
        }
        float theHei   = transform.localScale.y;
        float theWidth = theHei * Screen.width / Screen.height;

        transform.localScale = new Vector3(theWidth, theHei, 1f);

        NatCam.Play();

        GetComponent <Renderer> ().material.mainTexture = NatCam.PreviewTexture;

        completeSetUp = true;
        _Menu.gameObject.SetActive(false);
        _TestUI.gameObject.SetActive(true);
    }
Esempio n. 16
0
 void Start()
 {
     NatCam.Play(DeviceCamera.RearCamera);
     NatCam.OnStart += OnStart;
     //gameObject.GetComponent<Renderer>().material.mainTexture = tex;
 }