1- using UnityEngine ;
2- using UnityEngine . SceneManagement ;
3- using UnityEngine . UI ;
4- using System ;
5- using System . Collections ;
6- using System . Collections . Generic ;
7- using NatCam ;
8- using NatShareU ;
1+ using NatShareU ;
92using OpenCVForUnity . CoreModule ;
103using OpenCVForUnity . ImgprocModule ;
114using OpenCVForUnity . UnityUtils ;
5+ using UnityEngine ;
6+ using UnityEngine . UI ;
127
138namespace NatCamWithOpenCVForUnityExample
149{
@@ -27,40 +22,55 @@ public class IntegrationWithNatShareExample : ExampleBase<NatCamSource>
2722
2823 FpsMonitor fpsMonitor ;
2924
30- protected override void Start ( )
25+ string exampleTitle = "" ;
26+ string exampleSceneTitle = "" ;
27+ string settingInfo1 = "" ;
28+ Scalar textColor = new Scalar ( 255 , 255 , 255 , 255 ) ;
29+ Point textPos = new Point ( ) ;
30+
31+ protected override void Start ( )
3132 {
3233 // Load global camera benchmark settings.
3334 int width , height , framerate ;
34- NatCamWithOpenCVForUnityExample . CameraConfiguration ( out width , out height , out framerate ) ;
35- NatCamWithOpenCVForUnityExample . ExampleSceneConfiguration ( out performImageProcessingEachTime ) ;
35+ NatCamWithOpenCVForUnityExample . CameraConfiguration ( out width , out height , out framerate ) ;
36+ NatCamWithOpenCVForUnityExample . ExampleSceneConfiguration ( out performImageProcessingEachTime ) ;
3637 // Create camera source
37- cameraSource = new NatCamSource ( width , height , framerate , useFrontCamera ) ;
38- cameraSource . StartPreview ( OnStart , OnFrame ) ;
38+ cameraSource = new NatCamSource ( width , height , framerate , useFrontCamera ) ;
39+ if ( ! cameraSource . activeCamera )
40+ cameraSource = new NatCamSource ( width , height , framerate , ! useFrontCamera ) ;
41+ cameraSource . StartPreview ( OnStart , OnFrame ) ;
3942 // Create comic filter
40- comicFilter = new ComicFilter ( ) ;
41-
42- fpsMonitor = GetComponent < FpsMonitor > ( ) ;
43- if ( fpsMonitor != null ) {
44- fpsMonitor . Add ( "Name" , "IntegrationWithNatShareExample" ) ;
45- fpsMonitor . Add ( "performImageProcessingEveryTime" , performImageProcessingEachTime . ToString ( ) ) ;
46- fpsMonitor . Add ( "onFrameFPS" , onFrameFPS . ToString ( "F1" ) ) ;
47- fpsMonitor . Add ( "drawFPS" , drawFPS . ToString ( "F1" ) ) ;
48- fpsMonitor . Add ( "width" , "" ) ;
49- fpsMonitor . Add ( "height" , "" ) ;
50- fpsMonitor . Add ( "orientation" , "" ) ;
43+ comicFilter = new ComicFilter ( ) ;
44+
45+ exampleTitle = "[NatCamWithOpenCVForUnity Example] (" + NatCamWithOpenCVForUnityExample . GetNatCamVersion ( ) + ")" ;
46+ exampleSceneTitle = "- Integration With NatShare Example" ;
47+
48+ fpsMonitor = GetComponent < FpsMonitor > ( ) ;
49+ if ( fpsMonitor != null )
50+ {
51+ fpsMonitor . Add ( "Name" , "IntegrationWithNatShareExample" ) ;
52+ fpsMonitor . Add ( "performImageProcessingEveryTime" , performImageProcessingEachTime . ToString ( ) ) ;
53+ fpsMonitor . Add ( "onFrameFPS" , onFrameFPS . ToString ( "F1" ) ) ;
54+ fpsMonitor . Add ( "drawFPS" , drawFPS . ToString ( "F1" ) ) ;
55+ fpsMonitor . Add ( "width" , "" ) ;
56+ fpsMonitor . Add ( "height" , "" ) ;
57+ fpsMonitor . Add ( "isFrontFacing" , "" ) ;
58+ fpsMonitor . Add ( "orientation" , "" ) ;
5159 }
5260 }
5361
54- protected override void OnStart ( )
62+ protected override void OnStart ( )
5563 {
64+ settingInfo1 = "- resolution: " + cameraSource . width + "x" + cameraSource . height ;
65+
5666 // Create matrix
5767 if ( frameMatrix != null )
58- frameMatrix . Dispose ( ) ;
59- frameMatrix = new Mat ( cameraSource . height , cameraSource . width , CvType . CV_8UC4 ) ;
68+ frameMatrix . Dispose ( ) ;
69+ frameMatrix = new Mat ( cameraSource . height , cameraSource . width , CvType . CV_8UC4 ) ;
6070 // Create texture
6171 if ( texture != null )
62- Texture2D . Destroy ( texture ) ;
63- texture = new Texture2D (
72+ Texture2D . Destroy ( texture ) ;
73+ texture = new Texture2D (
6474 cameraSource . width ,
6575 cameraSource . height ,
6676 TextureFormat . RGBA32 ,
@@ -70,67 +80,79 @@ protected override void OnStart ()
7080 // Display preview
7181 rawImage . texture = texture ;
7282 aspectFitter . aspectRatio = cameraSource . width / ( float ) cameraSource . height ;
73- Debug . Log ( "NatCam camera source started with resolution: " + cameraSource . width + "x" + cameraSource . height ) ;
74-
75- if ( fpsMonitor != null ) {
76- fpsMonitor . Add ( "width" , cameraSource . width . ToString ( ) ) ;
77- fpsMonitor . Add ( "height" , cameraSource . height . ToString ( ) ) ;
78- fpsMonitor . Add ( "orientation" , Screen . orientation . ToString ( ) ) ;
83+ Debug . Log ( "NatCam camera source started with resolution: " + cameraSource . width + "x" + cameraSource . height + " isFrontFacing: " + cameraSource . isFrontFacing ) ;
84+
85+ if ( fpsMonitor != null )
86+ {
87+ fpsMonitor . Add ( "width" , cameraSource . width . ToString ( ) ) ;
88+ fpsMonitor . Add ( "height" , cameraSource . height . ToString ( ) ) ;
89+ fpsMonitor . Add ( "isFrontFacing" , cameraSource . isFrontFacing . ToString ( ) ) ;
90+ fpsMonitor . Add ( "orientation" , Screen . orientation . ToString ( ) ) ;
7991 }
8092 }
8193
82- protected override void Update ( )
94+ protected override void Update ( )
8395 {
84- base . Update ( ) ;
85-
86- if ( updateCount == 0 ) {
87- if ( fpsMonitor != null ) {
88- fpsMonitor . Add ( "onFrameFPS" , onFrameFPS . ToString ( "F1" ) ) ;
89- fpsMonitor . Add ( "drawFPS" , drawFPS . ToString ( "F1" ) ) ;
90- fpsMonitor . Add ( "orientation" , Screen . orientation . ToString ( ) ) ;
96+ base . Update ( ) ;
97+
98+ if ( updateCount == 0 )
99+ {
100+ if ( fpsMonitor != null )
101+ {
102+ fpsMonitor . Add ( "onFrameFPS" , onFrameFPS . ToString ( "F1" ) ) ;
103+ fpsMonitor . Add ( "drawFPS" , drawFPS . ToString ( "F1" ) ) ;
104+ fpsMonitor . Add ( "orientation" , Screen . orientation . ToString ( ) ) ;
91105 }
92106 }
93107 }
94108
95- protected override void UpdateTexture ( )
109+ protected override void UpdateTexture ( )
96110 {
97- cameraSource . CaptureFrame ( frameMatrix ) ;
111+ cameraSource . CaptureFrame ( frameMatrix ) ;
98112
99113 if ( applyComicFilterToggle . isOn )
100- comicFilter . Process ( frameMatrix , frameMatrix ) ;
114+ comicFilter . Process ( frameMatrix , frameMatrix ) ;
115+
116+ textPos . x = 5 ;
117+ textPos . y = frameMatrix . rows ( ) - 50 ;
118+ Imgproc . putText ( frameMatrix , exampleTitle , textPos , Imgproc . FONT_HERSHEY_SIMPLEX , 0.5 , textColor , 1 , Imgproc . LINE_AA , false ) ;
119+ textPos . y = frameMatrix . rows ( ) - 30 ;
120+ Imgproc . putText ( frameMatrix , exampleSceneTitle , textPos , Imgproc . FONT_HERSHEY_SIMPLEX , 0.5 , textColor , 1 , Imgproc . LINE_AA , false ) ;
121+ textPos . y = frameMatrix . rows ( ) - 10 ;
122+ Imgproc . putText ( frameMatrix , settingInfo1 , textPos , Imgproc . FONT_HERSHEY_SIMPLEX , 0.5 , textColor , 1 , Imgproc . LINE_AA , false ) ;
101123
102- Imgproc . putText ( frameMatrix , "[NatCam With OpenCVForUnity Example]" , new Point ( 5 , frameMatrix . rows ( ) - 50 ) , Imgproc . FONT_HERSHEY_SIMPLEX , 1.0 , new Scalar ( 255 , 255 , 255 , 255 ) , 2 , Imgproc . LINE_AA , false ) ;
103- Imgproc . putText ( frameMatrix , "- Integration With NatShare Example" , new Point ( 5 , frameMatrix . rows ( ) - 10 ) , Imgproc . FONT_HERSHEY_SIMPLEX , 1.0 , new Scalar ( 255 , 255 , 255 , 255 ) , 2 , Imgproc . LINE_AA , false ) ;
104124 // Convert to Texture2D
105- Utils . fastMatToTexture2D ( frameMatrix , texture , true , 0 , false ) ;
125+ Utils . fastMatToTexture2D ( frameMatrix , texture , true , 0 , false ) ;
106126 }
107127
108- protected override void OnDestroy ( )
128+ protected override void OnDestroy ( )
109129 {
110- if ( cameraSource != null ) {
111- cameraSource . Dispose ( ) ;
130+ if ( cameraSource != null )
131+ {
132+ cameraSource . Dispose ( ) ;
112133 cameraSource = null ;
113134 }
114135 if ( frameMatrix != null )
115- frameMatrix . Dispose ( ) ;
136+ frameMatrix . Dispose ( ) ;
116137 frameMatrix = null ;
117- Texture2D . Destroy ( texture ) ;
138+ Texture2D . Destroy ( texture ) ;
118139 texture = null ;
119- comicFilter . Dispose ( ) ;
140+ comicFilter . Dispose ( ) ;
120141 comicFilter = null ;
121142 }
122143
123- public void OnShareButtonClick ( )
144+ public void OnShareButtonClick ( )
124145 {
125- NatShare . Share ( texture ,
126- ( ) => {
127- Debug . Log ( "sharing is complete." ) ;
146+ NatShare . Share ( texture ,
147+ ( ) =>
148+ {
149+ Debug . Log ( "sharing is complete." ) ;
128150 } ) ;
129151 }
130152
131- public void OnSaveToCameraRollButtonClick ( )
153+ public void OnSaveToCameraRollButtonClick ( )
132154 {
133- NatShare . SaveToCameraRoll ( texture , "NatCamWithOpenCVForUnityExample" ) ;
155+ NatShare . SaveToCameraRoll ( texture , "NatCamWithOpenCVForUnityExample" ) ;
134156 }
135157 }
136158}
0 commit comments