Implement CameraView (#2680)
authorJoogabYun <40262755+JoogabYun@users.noreply.github.com>
Tue, 30 Mar 2021 07:58:05 +0000 (16:58 +0900)
committerdongsug-song <35130733+dongsug-song@users.noreply.github.com>
Thu, 1 Apr 2021 01:07:37 +0000 (10:07 +0900)
src/Tizen.NUI/src/internal/Interop/Interop.CameraView.cs [new file with mode: 0755]
src/Tizen.NUI/src/public/BaseComponents/CameraView.cs [new file with mode: 0755]
test/Tizen.NUI.Samples/Tizen.NUI.Samples/Samples/CameraVideoViewTest.cs [new file with mode: 0755]
test/Tizen.NUI.Samples/Tizen.NUI.Samples/Samples/CameraViewTest.cs [new file with mode: 0755]
test/Tizen.NUI.Samples/Tizen.NUI.Samples/Tizen.NUI.Samples.csproj
test/Tizen.NUI.Samples/Tizen.NUI.Samples/tizen-manifest.xml

diff --git a/src/Tizen.NUI/src/internal/Interop/Interop.CameraView.cs b/src/Tizen.NUI/src/internal/Interop/Interop.CameraView.cs
new file mode 100755 (executable)
index 0000000..9eb5b6e
--- /dev/null
@@ -0,0 +1,37 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace Tizen.NUI
+{
+    internal static partial class Interop
+    {
+        internal static partial class CameraView
+        {
+            [global::System.Runtime.InteropServices.DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_CameraView_New__SWIG_0")]
+            public static extern global::System.IntPtr New(global::System.IntPtr jarg1, int jarg2);
+
+            [global::System.Runtime.InteropServices.DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_new_CameraView__SWIG_0")]
+            public static extern global::System.IntPtr NewCameraView();
+
+            [global::System.Runtime.InteropServices.DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_delete_CameraView")]
+            public static extern void DeleteCameraView(global::System.Runtime.InteropServices.HandleRef jarg1);
+
+            [global::System.Runtime.InteropServices.DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_new_CameraView__SWIG_1")]
+            public static extern global::System.IntPtr NewCameraView(global::System.Runtime.InteropServices.HandleRef jarg1);
+
+            [global::System.Runtime.InteropServices.DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_CameraView_Assign")]
+            public static extern global::System.IntPtr Assign(global::System.Runtime.InteropServices.HandleRef jarg1, global::System.Runtime.InteropServices.HandleRef jarg2);
+
+            [global::System.Runtime.InteropServices.DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_CameraView_DownCast")]
+            public static extern global::System.IntPtr DownCast(global::System.Runtime.InteropServices.HandleRef jarg1);
+
+            [global::System.Runtime.InteropServices.DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_CameraView_Update")]
+            public static extern void Update(global::System.Runtime.InteropServices.HandleRef jarg1);
+
+            [global::System.Runtime.InteropServices.DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_CameraView_SWIGUpcast")]
+            public static extern global::System.IntPtr Upcast(global::System.IntPtr jarg1);
+
+        }
+    }
+}
diff --git a/src/Tizen.NUI/src/public/BaseComponents/CameraView.cs b/src/Tizen.NUI/src/public/BaseComponents/CameraView.cs
new file mode 100755 (executable)
index 0000000..0720048
--- /dev/null
@@ -0,0 +1,93 @@
+/*
+ * Copyright(c) 2021 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+using System;
+using System.ComponentModel;
+using System.Runtime.InteropServices;
+
+namespace Tizen.NUI.BaseComponents
+{
+    /// <summary>
+    /// CameraView is a view for camera preview.
+    /// </summary>
+    [EditorBrowsable(EditorBrowsableState.Never)]
+    public class CameraView : View
+    {
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public enum DisplayType
+        {
+            Window = 0,   //  HW overlay
+            Image     //  texture stream
+        };
+
+        /// <summary>
+        /// Creates an initialized CameraView.
+        /// </summary>
+        /// <param name="handle">Multimedia Camera handle</param>
+        /// <param name="type">DisplayType</param>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public CameraView(global::System.IntPtr handle, DisplayType type = DisplayType.Window) : this(Interop.CameraView.New(handle, (int)type), true)
+        {
+            if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
+        }
+
+        internal CameraView(CameraView cameraView) : this(Interop.CameraView.NewCameraView(CameraView.getCPtr(cameraView)), true)
+        {
+            if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
+        }
+
+        internal CameraView(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
+        {
+        }
+
+        internal static global::System.Runtime.InteropServices.HandleRef getCPtr(CameraView obj)
+        {
+            return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.SwigCPtr;
+        }
+
+        /// <summary>
+        /// Called when the camera view needs to be updated if the camera setting is changed..
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public void Update()
+        {
+            Interop.CameraView.Update(SwigCPtr);
+        }
+
+        /// <summary>
+        /// Dispose.
+        /// </summary>
+        /// <param name="type">DisposeTypes</param>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        protected override void Dispose(DisposeTypes type)
+        {
+            if (disposed)
+            {
+                return;
+            }
+
+            base.Dispose(type);
+        }
+
+        /// This will not be public opened.
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
+        {
+            Interop.CameraView.DeleteCameraView(swigCPtr);
+        }
+    }
+
+}
diff --git a/test/Tizen.NUI.Samples/Tizen.NUI.Samples/Samples/CameraVideoViewTest.cs b/test/Tizen.NUI.Samples/Tizen.NUI.Samples/Samples/CameraVideoViewTest.cs
new file mode 100755 (executable)
index 0000000..f7dda56
--- /dev/null
@@ -0,0 +1,92 @@
+
+using global::System;
+using Tizen.NUI.BaseComponents;
+using Tizen.NUI.Components;
+
+namespace Tizen.NUI.Samples
+{
+    using tlog = Tizen.Log;
+
+    public class CameraVideoViewTest : IExample
+    {
+
+        Window win;
+        CameraView cameraView;
+        VideoView videoView;
+        Tizen.Multimedia.Camera camera;
+
+        const string tag = "gab_test";
+
+        public void Activate()
+        {
+            win = NUIApplication.GetDefaultWindow();
+            win.BackgroundColor = new Color(1, 1, 1, 0);
+            win.KeyEvent += Win_KeyEvent;
+
+            AddCameraView();
+            AddVideoView();
+        }
+
+        private void AddCameraView()
+        {
+            camera = new Tizen.Multimedia.Camera(Tizen.Multimedia.CameraDevice.Front);
+
+            // default display type is Window (Overlay mode)
+            cameraView = new CameraView(camera.Handle, CameraView.DisplayType.Window);
+            cameraView.Size = new Size(300, 300);
+            cameraView.Position = new Position(100, 50);
+
+            if (camera != null && camera.State == Tizen.Multimedia.CameraState.Created)
+            {
+                camera.StartPreview();
+            }
+            win.Add(cameraView);
+        }
+
+        private void AddVideoView()
+        {
+            videoView = new VideoView();
+            videoView.Underlay = false;
+            string resourcePath = Tizen.Applications.Application.Current.DirectoryInfo.Resource + "v.mp4";
+            videoView.ResourceUrl = resourcePath;
+            videoView.Looping = true;
+            videoView.Size = new Size(300, 300);
+            videoView.Position = new Position(100, 360);
+            videoView.Play();
+            win.Add(videoView);
+        }
+
+        public void Deactivate()
+        {
+            win.KeyEvent -= Win_KeyEvent;
+
+            if (camera != null )
+            {
+                if(camera.State == Tizen.Multimedia.CameraState.Preview)
+                    camera.StopPreview();
+
+                camera.Dispose();
+            }
+
+            videoView.Dispose();
+
+            tlog.Fatal(tag, $"Deactivate()! cameraView dispsed");
+        }
+
+
+        private void Win_KeyEvent(object sender, Window.KeyEventArgs e)
+        {
+            if (e.Key.State == Key.StateType.Down)
+            {
+                tlog.Fatal(tag, $"key pressed name={e.Key.KeyPressedName}");
+                if (e.Key.KeyPressedName == "XF86Back")
+                {
+                    Deactivate();
+                }
+            }
+        }
+
+    }
+}
+
+
diff --git a/test/Tizen.NUI.Samples/Tizen.NUI.Samples/Samples/CameraViewTest.cs b/test/Tizen.NUI.Samples/Tizen.NUI.Samples/Samples/CameraViewTest.cs
new file mode 100755 (executable)
index 0000000..39b8800
--- /dev/null
@@ -0,0 +1,220 @@
+
+using global::System;
+using Tizen.NUI.BaseComponents;
+using Tizen.NUI.Components;
+
+namespace Tizen.NUI.Samples
+{
+    using tlog = Tizen.Log;
+
+    public class CameraViewTest : IExample
+    {
+
+        Window win;
+        CameraView overlayCameraView;
+        CameraView imageCameraView;
+        Button overlayButton;
+        Button nativeButton;
+        Button sizeButton;
+        Button rotationButton;
+
+        Tizen.Multimedia.Camera overlayCamera;
+        Tizen.Multimedia.Camera imageCamera;
+
+        const string tag = "NUI";
+
+        public void Activate()
+        {
+            win = NUIApplication.GetDefaultWindow();
+            win.BackgroundColor = new Color(1, 1, 1, 0);
+            win.KeyEvent += Win_KeyEvent;
+
+            overlayButton = new Button();
+            overlayButton.Text = "Overlay";
+            overlayButton.Size = new Size(100, 100);
+            overlayButton.Position = new Position( 50, 750);
+            overlayButton.Clicked += OverlayButtonClicked;
+            win.Add(overlayButton);
+
+            nativeButton = new Button();
+            nativeButton.Text = "Image";
+            nativeButton.Size = new Size(100, 100);
+            nativeButton.Position = new Position( 160, 750);
+            nativeButton.Clicked += NativeButtonClicked;
+            win.Add(nativeButton);
+
+            sizeButton = new Button();
+            sizeButton.Text = "Size";
+            sizeButton.Size = new Size(100, 100);
+            sizeButton.Position = new Position(50, 850);
+            sizeButton.Clicked += sizeButtonClicked;
+            win.Add(sizeButton);
+
+
+            rotationButton = new Button();
+            rotationButton.Text = "Rotation";
+            rotationButton.Size = new Size(100, 100);
+            rotationButton.Position = new Position(160, 850);
+            rotationButton.Clicked += rotationButtonClicked;
+            win.Add(rotationButton);
+
+            OverlayCamera();
+            // ImageCamera();
+
+        }
+
+        private int rotationCnt = 0;
+        private void rotationButtonClicked(object sender, ClickedEventArgs e)
+        {
+            int rotation = rotationCnt % 4;
+            Vector3 axis;
+            Degree degree;
+            if(overlayCamera != null) {
+                switch(rotation)
+                {
+                    case 0 :
+                        overlayCamera.DisplaySettings.Rotation = Tizen.Multimedia.Rotation.Rotate0;
+                        overlayCameraView.Update();
+                        break;
+                    case 1:
+                        overlayCamera.DisplaySettings.Rotation = Tizen.Multimedia.Rotation.Rotate90;
+                        overlayCameraView.Update();
+                        break;
+                    case 2:
+                        overlayCamera.DisplaySettings.Rotation = Tizen.Multimedia.Rotation.Rotate180;
+                        overlayCameraView.Update();
+                        break;
+                    case 3:
+                        overlayCamera.DisplaySettings.Rotation = Tizen.Multimedia.Rotation.Rotate270;
+                        overlayCameraView.Update();
+                        break;
+                    default:
+                        overlayCamera.DisplaySettings.Rotation = Tizen.Multimedia.Rotation.Rotate0;
+                        overlayCameraView.Update();
+                        break;
+                }
+            }
+            rotationCnt++;
+        }
+
+        private void OverlayButtonClicked(object sender, ClickedEventArgs e)
+        {
+            if(imageCamera != null)
+            {
+                imageCamera.StopPreview();
+                imageCamera.Dispose();
+                imageCamera = null;
+                win.Remove(imageCameraView);
+            }
+            if(overlayCamera == null)
+            {
+                OverlayCamera();
+            }
+        }
+
+        private void NativeButtonClicked(object sender, ClickedEventArgs e)
+        {
+            if(overlayCamera != null)
+            {
+                overlayCamera.StopPreview();
+                overlayCamera.Dispose();
+                overlayCamera = null;
+                win.Remove(overlayCameraView);
+            }
+            if(imageCamera == null)
+            {
+                ImageCamera();
+            }
+        }
+
+        private void OverlayCamera()
+        {
+
+            overlayCamera = new Tizen.Multimedia.Camera(Tizen.Multimedia.CameraDevice.Front);
+            // default display type is Window (Overlay mode)
+            overlayCameraView = new CameraView(overlayCamera.Handle);
+            overlayCameraView.PositionUsesPivotPoint = true;
+            overlayCameraView.ParentOrigin = ParentOrigin.TopLeft;
+            overlayCameraView.PivotPoint = PivotPoint.TopLeft;
+            overlayCameraView.Size = new Size(300, 400);
+            overlayCameraView.Position = new Position(100, 200);
+
+            overlayCamera.StartPreview();
+
+            win.Add(overlayCameraView);
+        }
+
+        private void ImageCamera()
+        {
+
+            imageCamera = new Tizen.Multimedia.Camera(Tizen.Multimedia.CameraDevice.Front);
+            // default display type is Window (Overlay mode)
+            imageCameraView = new CameraView(imageCamera.Handle, CameraView.DisplayType.Image);
+            imageCameraView.PositionUsesPivotPoint = true;
+            imageCameraView.ParentOrigin = ParentOrigin.TopLeft;
+            imageCameraView.PivotPoint = PivotPoint.TopLeft;
+            imageCameraView.Position = new Position(0, 400);
+            imageCameraView.Size = new Size(300, 300);
+
+            imageCamera.StartPreview();
+
+            win.Add(imageCameraView);
+        }
+
+        private int size = 300;
+        private void sizeButtonClicked(object sender, ClickedEventArgs e)
+        {
+            if(overlayCameraView != null)
+                overlayCameraView.Size = new Size(size, size);
+            if(imageCameraView != null)
+                imageCameraView.Size = new Size(size, size);
+            size += 20;
+        }
+
+
+        public void Deactivate()
+        {
+            win.KeyEvent -= Win_KeyEvent;
+
+             if(imageCamera != null)
+            {
+
+                imageCamera.StopPreview();
+                imageCamera.Dispose();
+                imageCamera = null;
+
+                win.Remove(imageCameraView);
+                imageCameraView.Dispose();
+            }
+
+            if(overlayCamera != null)
+            {
+                overlayCamera.StopPreview();
+                overlayCamera.Dispose();
+                overlayCamera = null;
+                win.Remove(overlayCameraView);
+                overlayCameraView.Dispose();
+            }
+
+
+            tlog.Fatal(tag, $"Deactivate()! cameraView disposed");
+        }
+
+
+        private void Win_KeyEvent(object sender, Window.KeyEventArgs e)
+        {
+            if (e.Key.State == Key.StateType.Down)
+            {
+                tlog.Fatal(tag, $"key pressed name={e.Key.KeyPressedName}");
+                if (e.Key.KeyPressedName == "XF86Back")
+                {
+                    Deactivate();
+                }
+            }
+        }
+
+    }
+}
+
+
+
index 9f75973..c438c10 100755 (executable)
@@ -32,6 +32,7 @@
     <ProjectReference Include="../../../src/Tizen.Applications.ComponentBased/Tizen.Applications.ComponentBased.csproj" />
     <ProjectReference Include="../../../src/Tizen.Log/Tizen.Log.csproj" />
     <ProjectReference Include="../../../src/Tizen.Multimedia.MediaPlayer/Tizen.Multimedia.MediaPlayer.csproj" />
+    <ProjectReference Include="../../../src/Tizen.Multimedia.Camera/Tizen.Multimedia.Camera.csproj" />
     <ProjectReference Include="../../../src/Tizen.System.SystemSettings/Tizen.System.SystemSettings.csproj" />
     <ProjectReference Include="../../../src/Tizen.NUI/Tizen.NUI.csproj" />
     <ProjectReference Include="../../../src/Tizen.NUI.Components/Tizen.NUI.Components.csproj" />
index a7840d0..d1cf732 100755 (executable)
@@ -13,4 +13,7 @@
     <icon>Tizen.NUI.Samples.png</icon>
     <metadata key="http://tizen.org/metadata/prefer_dotnet_aot" value="true" />
   </ui-application>
+       <privileges>
+        <privilege>http://tizen.org/privilege/camera</privilege>
+  </privileges>
 </manifest>