88using NatCamU . Core ;
99using NatShareU ;
1010
11- namespace NatCamWithOpenCVForUnityExample {
11+ namespace NatCamWithOpenCVForUnityExample
12+ {
1213
1314 /// <summary>
1415 /// Integration With NatShare Example
1516 /// An example of the native sharing and save to the camera roll using NatShare.
1617 /// </summary>
17- public class IntegrationWithNatShareExample : ExampleBase < NatCamSource > {
18+ public class IntegrationWithNatShareExample : ExampleBase < NatCamSource >
19+ {
20+ public Toggle applyComicFilterToggle ;
1821
1922 Mat frameMatrix ;
2023 Texture2D texture ;
2124 ComicFilter comicFilter ;
2225
23- protected override void Start ( ) {
26+ FpsMonitor fpsMonitor ;
27+
28+ protected override void Start ( )
29+ {
2430 // Load global camera benchmark settings.
2531 int width , height , framerate ;
26- NatCamWithOpenCVForUnityExample . CameraConfiguration ( out width , out height , out framerate ) ;
32+ NatCamWithOpenCVForUnityExample . CameraConfiguration ( out width , out height , out framerate ) ;
33+ NatCamWithOpenCVForUnityExample . ExampleSceneConfiguration ( out performImageProcessingEachTime ) ;
2734 // Create camera source
28- cameraSource = new NatCamSource ( width , height , framerate , useFrontCamera ) ;
29- cameraSource . StartPreview ( OnStart , OnFrame ) ;
35+ cameraSource = new NatCamSource ( width , height , framerate , useFrontCamera ) ;
36+ cameraSource . StartPreview ( OnStart , OnFrame ) ;
3037 // Create comic filter
3138 comicFilter = new ComicFilter ( ) ;
39+
40+ fpsMonitor = GetComponent < FpsMonitor > ( ) ;
41+ if ( fpsMonitor != null ) {
42+ fpsMonitor . Add ( "Name" , "IntegrationWithNatShareExample" ) ;
43+ fpsMonitor . Add ( "performImageProcessingEveryTime" , performImageProcessingEachTime . ToString ( ) ) ;
44+ fpsMonitor . Add ( "onFrameFPS" , onFrameFPS . ToString ( "F1" ) ) ;
45+ fpsMonitor . Add ( "drawFPS" , drawFPS . ToString ( "F1" ) ) ;
46+ fpsMonitor . Add ( "width" , "" ) ;
47+ fpsMonitor . Add ( "height" , "" ) ;
48+ fpsMonitor . Add ( "orientation" , "" ) ;
49+ }
3250 }
3351
34- protected override void OnStart ( ) {
52+ protected override void OnStart ( )
53+ {
3554 // Create matrix
36- frameMatrix = new Mat ( cameraSource . height , cameraSource . width , CvType . CV_8UC4 ) ;
55+ if ( frameMatrix != null )
56+ frameMatrix . Dispose ( ) ;
57+ frameMatrix = new Mat ( cameraSource . height , cameraSource . width , CvType . CV_8UC4 ) ;
3758 // Create texture
38- texture = new Texture2D (
59+ if ( texture != null )
60+ Texture2D . Destroy ( texture ) ;
61+ texture = new Texture2D (
3962 cameraSource . width ,
4063 cameraSource . height ,
4164 TextureFormat . RGBA32 ,
@@ -45,20 +68,47 @@ protected override void OnStart () {
4568 // Display preview
4669 rawImage . texture = texture ;
4770 aspectFitter . aspectRatio = NatCam . Preview . width / ( float ) NatCam . Preview . height ;
48- Debug . Log ( "NatCam camera source started with resolution: " + cameraSource . width + "x" + cameraSource . height ) ;
71+ Debug . Log ( "NatCam camera source started with resolution: " + cameraSource . width + "x" + cameraSource . height ) ;
72+
73+ if ( fpsMonitor != null ) {
74+ fpsMonitor . Add ( "width" , cameraSource . width . ToString ( ) ) ;
75+ fpsMonitor . Add ( "height" , cameraSource . height . ToString ( ) ) ;
76+ fpsMonitor . Add ( "orientation" , Screen . orientation . ToString ( ) ) ;
77+ }
4978 }
5079
51- protected override void OnFrame ( ) {
52- cameraSource . CaptureFrame ( frameMatrix ) ;
53- comicFilter . Process ( frameMatrix , frameMatrix ) ;
54- Imgproc . putText ( frameMatrix , "[NatCam With OpenCVForUnity Example]" , new Point ( 5 , frameMatrix . rows ( ) - 50 ) , Core . FONT_HERSHEY_SIMPLEX , 1.0 , new Scalar ( 255 , 255 , 255 , 255 ) , 2 , Imgproc . LINE_AA , false ) ;
55- Imgproc . putText ( frameMatrix , "- Integration With NatShare Example" , new Point ( 5 , frameMatrix . rows ( ) - 10 ) , Core . FONT_HERSHEY_SIMPLEX , 1.0 , new Scalar ( 255 , 255 , 255 , 255 ) , 2 , Imgproc . LINE_AA , false ) ;
56- Utils . fastMatToTexture2D ( frameMatrix , texture , true , 0 , false ) ;
80+ protected override void Update ( )
81+ {
82+ base . Update ( ) ;
83+
84+ if ( updateCount == 0 ) {
85+ if ( fpsMonitor != null ) {
86+ fpsMonitor . Add ( "onFrameFPS" , onFrameFPS . ToString ( "F1" ) ) ;
87+ fpsMonitor . Add ( "drawFPS" , drawFPS . ToString ( "F1" ) ) ;
88+ fpsMonitor . Add ( "orientation" , Screen . orientation . ToString ( ) ) ;
89+ }
90+ }
5791 }
5892
59- protected override void OnDestroy ( ) {
60- cameraSource . Dispose ( ) ;
61- cameraSource = null ;
93+ protected override void UpdateTexture ( )
94+ {
95+ cameraSource . CaptureFrame ( frameMatrix ) ;
96+
97+ if ( applyComicFilterToggle . isOn )
98+ comicFilter . Process ( frameMatrix , frameMatrix ) ;
99+
100+ Imgproc . putText ( frameMatrix , "[NatCam With OpenCVForUnity Example]" , new Point ( 5 , frameMatrix . rows ( ) - 50 ) , Core . FONT_HERSHEY_SIMPLEX , 1.0 , new Scalar ( 255 , 255 , 255 , 255 ) , 2 , Imgproc . LINE_AA , false ) ;
101+ Imgproc . putText ( frameMatrix , "- Integration With NatShare Example" , new Point ( 5 , frameMatrix . rows ( ) - 10 ) , Core . FONT_HERSHEY_SIMPLEX , 1.0 , new Scalar ( 255 , 255 , 255 , 255 ) , 2 , Imgproc . LINE_AA , false ) ;
102+ // Convert to Texture2D
103+ Utils . fastMatToTexture2D ( frameMatrix , texture , true , 0 , false ) ;
104+ }
105+
106+ protected override void OnDestroy ( )
107+ {
108+ if ( cameraSource != null ) {
109+ cameraSource . Dispose ( ) ;
110+ cameraSource = null ;
111+ }
62112 if ( frameMatrix != null )
63113 frameMatrix . Dispose ( ) ;
64114 frameMatrix = null ;
@@ -67,5 +117,18 @@ protected override void OnDestroy () {
67117 comicFilter . Dispose ( ) ;
68118 comicFilter = null ;
69119 }
120+
121+ public void OnShareButtonClick ( )
122+ {
123+ NatShare . Share ( texture ,
124+ ( ) => {
125+ Debug . Log ( "sharing is complete." ) ;
126+ } ) ;
127+ }
128+
129+ public void OnSaveToCameraRollButtonClick ( )
130+ {
131+ NatShare . SaveToCameraRoll ( texture , "NatCamWithOpenCVForUnityExample" ) ;
132+ }
70133 }
71134}
0 commit comments