/// <summary> /// Starts the camera. /// </summary> public override void Play() { if (hasInitDone) { NatCam.Play(); } }
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); } } }
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); }
/// <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); }
/// <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); }
/// <summary> /// Stops the active camera. /// </summary> public override void Stop() { if (hasInitDone) { NatCam.Pause(); } }
void OnDisable() { if (!willDestroy) { NatCam.Release(); //We check that this wasn't caused by NatCam.Release() before calling NatCam.Release(); } }
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(); }
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(); }
/// <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; } }
/// <summary> /// Pause this instance. /// </summary> public void Pause() { if (NatCam.IsPlaying) { NatCam.Pause(); } }
/// <summary> /// Play this instance. /// </summary> public void Play() { if (!NatCam.IsPlaying) { NatCam.Play(); } }
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(); }
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; } }
} // Don't do anything in OnStart public override void OnFrame() { // Get the preview matrix and call OnMatrix if (NatCam.PreviewMatrix(ref _matrix)) { OnMatrix(); } }
public static void StopCamera() { NatCam.StopPreview(); // clear the memory Resources.UnloadUnusedAssets(); OnCameraComplete?.Invoke(_previews); }
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; }
private void ReleaseCamera() { if (NatCam.ActiveCamera != null) { NatCam.Release(); } initialized = false; }
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(); }
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); }
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; }
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); }
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); }
// 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(); }
/// <summary> /// Releases all resource. /// </summary> private void Dispose() { NatCam.Release(); if (texture != null) { Texture2D.Destroy(texture); texture = null; } didUpdateThisFrame = false; }
public void SwitchCamera() { // Switch camera if (NatCam.Camera.IsFrontFacing) { NatCam.StartPreview(DeviceCamera.RearCamera, OnStart); } else { NatCam.StartPreview(DeviceCamera.FrontCamera, OnStart); } }
public override void StartCameraPreview() { if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer) { NatCam.Play(DeviceCamera.RearCamera); } else { NatCam.Play(DeviceCamera.FrontCamera); } SetCameraPreviewTexture(); }
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(); } }
// 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; }