Reimplement Camera control by using NUI CameraView control. (#40)
authorPiotr Czaja/Tizen Services & IoT (PLT) /SRPOL/Engineer/Samsung Electronics <p.czaja@samsung.com>
Wed, 28 Apr 2021 11:27:41 +0000 (13:27 +0200)
committerPiotr Czaja <p.czaja@samsung.com>
Tue, 14 Sep 2021 11:01:34 +0000 (13:01 +0200)
* Reimplement Camera control by using NUI CameraView control.
* Reorder using directives.
* Fix after review comments.
* Disable preview.

Fitness/Controls/Camera.cs
Fitness/Fitness.csproj
Fitness/Views/ExercisingView.xaml.cs
Fitness/Views/ScanningView.cs

index 87a1ec43675bd5740730b9d2ae673389da3176e4..a2e06f074e88a188e6dcdc67ee21cacb39f20812 100644 (file)
@@ -1,6 +1,4 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
+using System;
 using Tizen.Multimedia;
 using Tizen.NUI;
 using Tizen.NUI.BaseComponents;
@@ -10,19 +8,35 @@ namespace Fitness.Controls
     /// <summary>
     /// The view for controlling camera.
     /// </summary>
-    public class Camera : View
+    public class Camera : CameraView
     {
         private Tizen.Multimedia.Camera camera;
-        private Window previewWindow;
 
         /// <summary>
         /// Initializes a new instance of the <see cref="Camera"/> class.
         /// </summary>
         public Camera()
+            : this(new Tizen.Multimedia.Camera(Tizen.Multimedia.CameraDevice.Front))
         {
-            this.Relayout += OnCameraRelayout;
+        }
+
+        /// <summary>
+        /// Initializes a new instance of the <see cref="Camera"/> class.
+        /// </summary>
+        private Camera(IntPtr handle)
+            : base(handle)
+        {
+        }
 
-            CreateCamera();
+        /// <summary>
+        /// Initializes a new instance of the <see cref="Camera"/> class.
+        /// </summary>
+        /// <param name="camera">The multimedia camera.</param>
+        private Camera(Tizen.Multimedia.Camera camera)
+            : this(camera.Handle)
+        {
+            this.camera = camera;
+            camera.StartPreview();
         }
 
         /// <summary>
@@ -58,12 +72,9 @@ namespace Fitness.Controls
         }
 
         /// <summary>
-        /// Gets all the fps supported by the camera.
+        /// Gets the state of preview.
         /// </summary>
-        public IEnumerable<CameraFps> SupportedPreviewFps
-        {
-            get => camera.Capabilities.SupportedPreviewFps;
-        }
+        public CameraState PreviewState => camera.State;
 
         /// <summary>
         /// Gets or sets the preview fps.
@@ -80,68 +91,9 @@ namespace Fitness.Controls
         }
 
         /// <summary>
-        /// Gets the state of preview.
-        /// </summary>
-        public CameraState PreviewState => camera.State;
-
-        /// <summary>
-        /// Starts preview.
-        /// </summary>
-        public void StartPreview()
-        {
-            if (camera.Display != null && camera.State == CameraState.Created)
-            {
-                camera.StartPreview();
-            }
-        }
-
-        /// <summary>
-        /// Stops preview if preivew is displaying.
-        /// </summary>
-        public void StopPreview()
-        {
-            if (camera.State == CameraState.Preview)
-            {
-                camera.StopPreview();
-            }
-        }
-
-        /// <summary>
-        /// Moves preview window to input position.
-        /// </summary>
-        /// <remarks>This API doesn't support to change size of the preview now.</remarks>
-        /// <param name="position">The position to move.</param>
-        public void Move(Position position)
-        {
-            if (position == null)
-            {
-                throw new ArgumentNullException("position should be set");
-            }
-
-            if (position.X < 0 || position.Y < 0)
-            {
-                throw new ArgumentOutOfRangeException("The position should be greater than 0. We don't support relative coordicate.");
-            }
-
-            Services.Logger.Debug($"Position={position.X}x{position.Y}");
-            previewWindow.WindowPosition = position;
-        }
-
-        /// <summary>
-        /// Hides preview window.
-        /// </summary>
-        public void HidePreviewWindow()
-        {
-            previewWindow?.Hide();
-        }
-
-        /// <summary>
-        /// Shows preview window.
+        /// Stops capturing and drawing preview frames on the screen.
         /// </summary>
-        public void ShowPreviewWindow()
-        {
-            previewWindow?.Show();
-        }
+        public void StopPreview() => camera.StopPreview();
 
         /// <summary>
         /// Dispose.
@@ -149,79 +101,8 @@ namespace Fitness.Controls
         /// <param name="type">Dispose type.</param>
         protected override void Dispose(DisposeTypes type)
         {
-            Services.Logger.Debug("Enter");
-
-            // if (disposed)
-            // {
-            //     return;
-            // }
             camera?.Dispose();
-            previewWindow?.Dispose();
-
             base.Dispose(type);
         }
-
-        private void OnCameraRelayout(object sender, EventArgs e)
-        {
-            this.Relayout -= OnCameraRelayout;
-
-            Start();
-        }
-
-        private void Start()
-        {
-            SetDisplay();
-            StartPreview();
-        }
-
-        private void CreateCamera()
-        {
-            try
-            {
-                camera = new Tizen.Multimedia.Camera(CameraDevice.Rear);
-            }
-            catch (Exception e)
-            {
-                Services.Logger.Error(e.ToString());
-
-                return;
-            }
-        }
-
-        private void SetDisplay()
-        {
-            // We need to add some dummy view into Window, even if we doesn't use it.
-            // If not, NUI framework will not render Window at all.
-            var previewResolution = GetPreviewResolution();
-
-            // Create and configure the window for preview
-            previewWindow = new Window(new Tizen.NUI.Rectangle(
-                (int)PositionX, (int)PositionY, (int)SizeWidth, (int)SizeHeight))
-            {
-                BackgroundColor = Color.Transparent,
-                Title = "Scanning view",
-            };
-            previewWindow.SetTransparency(true);
-            previewWindow.Add(new View());
-            previewWindow.SetParent(Window.Instance);
-
-            // Set previewWindow to Camera FW and set values for display
-            camera.Display = new Display(previewWindow);
-            camera.Settings.PreviewResolution = previewResolution;
-            camera.DisplaySettings.Flip = Flips.Horizontal;
-        }
-
-        private Tizen.Multimedia.Size GetPreviewResolution()
-        {
-            var windowSize = Window.Instance.WindowSize;
-
-            var previewResolution = camera.Capabilities.SupportedPreviewResolutions
-                                        .Where(s => (s.Width < windowSize.Width) && (s.Height < windowSize.Height))
-                                        .OrderByDescending(s => s.Width)
-                                        .FirstOrDefault();
-            Services.Logger.Info($"Suitable resolution : {previewResolution.Width}x{previewResolution.Height}");
-
-            return previewResolution;
-        }
     }
 }
index b9e05ce4fe187b17c8fda7f9ab4f1de5994924d1..b3b03fc9eee4a6c402d8dd8708a13a977187b972 100644 (file)
@@ -21,7 +21,7 @@
 
   <ItemGroup>
     <PackageReference Include="Tizen.NET.Sdk" Version="1.1.2" />
-    <PackageReference Include="Tizen.NET" Version="9.0.0.16042" />
+    <PackageReference Include="Tizen.NET" Version="9.0.0.16099" />
     <PackageReference Include="Tizen.NUI.XamlBuild" Version="1.0.11" />
     <PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
       <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
index 6b93f8a39037d5c158b1a76bb3147d8b2afc1b61..328d633731b61f5a91c3b5c9c9d3c1f58320fe03 100644 (file)
@@ -28,8 +28,8 @@ namespace Fitness.Views
             },
             Camera: new Coordinates() // fixed for now
             {
-                Position = new Position(1200, 138),
-                Size = new Size(392, 220),
+                Position = new Position(1356, 144),
+                Size = new Size(500, 292),
             });
 
         public ExercisingView()
@@ -39,33 +39,6 @@ namespace Fitness.Views
             PlayingView.PreviewStub.Relayout += OnPlayingViewRelayout;
         }
 
-        /// <summary>
-        /// OnAppearing.
-        /// </summary>
-        protected override void OnAppearing()
-        {
-            switch (cameraView.PreviewState)
-            {
-                case Tizen.Multimedia.CameraState.Created:
-                    cameraView.StartPreview();
-                    break;
-                case Tizen.Multimedia.CameraState.Preview:
-                    cameraView.ShowPreviewWindow();
-                    break;
-            }
-        }
-
-        /// <summary>
-        /// OnDisappearing.
-        /// </summary>
-        protected override void OnDisappearing()
-        {
-            if (cameraView.PreviewState == Tizen.Multimedia.CameraState.Preview)
-            {
-                cameraView.HidePreviewWindow();
-            }
-        }
-
         private static void OnIsPlayingChanged(BindableObject bindable, object oldValue, object newValue)
         {
             if (newValue is bool isPlaying && bindable is ExercisingView view)
@@ -74,7 +47,7 @@ namespace Fitness.Views
             }
         }
 
-        private static Task Animate(View view, Coordinates from, Coordinates to, CancellationToken token)
+        private static (Task, Task) Animate(View view, Coordinates from, Coordinates to, CancellationToken token)
         {
             Task Scale()
             {
@@ -97,7 +70,7 @@ namespace Fitness.Views
                 return animation.PlayAndDispose(token);
             }
 
-            return Task.WhenAll(Scale(), Move());
+            return (Scale(), Move());
         }
 
         private static Position GetPosition(View view)
@@ -121,9 +94,9 @@ namespace Fitness.Views
 
                 if (isPlaying)
                 {
-                    await Animate(Preview, pause.Preview, playing.Preview, source.Token);
-
-                    cameraView.Move(playing.Camera.Position);
+                    (Task scalePreview, Task movePreview) = Animate(Preview, pause.Preview, playing.Preview, source.Token);
+                    (Task scaleCamera, Task moveCamera) = Animate(cameraView, pause.Camera, playing.Camera, source.Token);
+                    await Task.WhenAll(scalePreview, movePreview, scaleCamera, moveCamera);
 
                     PauseView.Hide();
                     PlayingView.Show();
@@ -133,9 +106,9 @@ namespace Fitness.Views
                     PlayingView.Hide();
                     PauseView.Show();
 
-                    cameraView.Move(pause.Camera.Position);
-
-                    await Animate(Preview, playing.Preview, pause.Preview, source.Token);
+                    (Task scalePreview, Task movePreview) = Animate(Preview, playing.Preview, pause.Preview, source.Token);
+                    (Task scaleCamera, Task moveCamera) = Animate(cameraView, playing.Camera, pause.Camera, source.Token);
+                    await Task.WhenAll(scalePreview, movePreview, scaleCamera, moveCamera);
                 }
 
                 if (source.IsCancellationRequested == false)
index e63385669df5276564fdd99d8ab850bef527ce79..381130597484c36259451e2e74eee0b7d6904036 100644 (file)
@@ -8,6 +8,9 @@ namespace Fitness.Views
     /// </summary>
     public partial class ScanningView : Fitness.Controls.Page
     {
+        /// <summary>
+        /// Initializes a new instance of the <see cref="ScanningView"/> class.
+        /// </summary>
         public ScanningView()
         {
             InitializeComponent();
@@ -20,7 +23,7 @@ namespace Fitness.Views
         {
             if (cameraView.PreviewState == Tizen.Multimedia.CameraState.Preview)
             {
-                cameraView.HidePreviewWindow();
+                cameraView.StopPreview();
             }
         }
     }