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 ;
9
2
using OpenCVForUnity . CoreModule ;
10
3
using OpenCVForUnity . ImgprocModule ;
11
4
using OpenCVForUnity . UnityUtils ;
5
+ using UnityEngine ;
6
+ using UnityEngine . UI ;
12
7
13
8
namespace NatCamWithOpenCVForUnityExample
14
9
{
@@ -27,40 +22,55 @@ public class IntegrationWithNatShareExample : ExampleBase<NatCamSource>
27
22
28
23
FpsMonitor fpsMonitor ;
29
24
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 ( )
31
32
{
32
33
// Load global camera benchmark settings.
33
34
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 ) ;
36
37
// 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 ) ;
39
42
// 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" , "" ) ;
51
59
}
52
60
}
53
61
54
- protected override void OnStart ( )
62
+ protected override void OnStart ( )
55
63
{
64
+ settingInfo1 = "- resolution: " + cameraSource . width + "x" + cameraSource . height ;
65
+
56
66
// Create matrix
57
67
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 ) ;
60
70
// Create texture
61
71
if ( texture != null )
62
- Texture2D . Destroy ( texture ) ;
63
- texture = new Texture2D (
72
+ Texture2D . Destroy ( texture ) ;
73
+ texture = new Texture2D (
64
74
cameraSource . width ,
65
75
cameraSource . height ,
66
76
TextureFormat . RGBA32 ,
@@ -70,67 +80,79 @@ protected override void OnStart ()
70
80
// Display preview
71
81
rawImage . texture = texture ;
72
82
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 ( ) ) ;
79
91
}
80
92
}
81
93
82
- protected override void Update ( )
94
+ protected override void Update ( )
83
95
{
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 ( ) ) ;
91
105
}
92
106
}
93
107
}
94
108
95
- protected override void UpdateTexture ( )
109
+ protected override void UpdateTexture ( )
96
110
{
97
- cameraSource . CaptureFrame ( frameMatrix ) ;
111
+ cameraSource . CaptureFrame ( frameMatrix ) ;
98
112
99
113
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 ) ;
101
123
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 ) ;
104
124
// Convert to Texture2D
105
- Utils . fastMatToTexture2D ( frameMatrix , texture , true , 0 , false ) ;
125
+ Utils . fastMatToTexture2D ( frameMatrix , texture , true , 0 , false ) ;
106
126
}
107
127
108
- protected override void OnDestroy ( )
128
+ protected override void OnDestroy ( )
109
129
{
110
- if ( cameraSource != null ) {
111
- cameraSource . Dispose ( ) ;
130
+ if ( cameraSource != null )
131
+ {
132
+ cameraSource . Dispose ( ) ;
112
133
cameraSource = null ;
113
134
}
114
135
if ( frameMatrix != null )
115
- frameMatrix . Dispose ( ) ;
136
+ frameMatrix . Dispose ( ) ;
116
137
frameMatrix = null ;
117
- Texture2D . Destroy ( texture ) ;
138
+ Texture2D . Destroy ( texture ) ;
118
139
texture = null ;
119
- comicFilter . Dispose ( ) ;
140
+ comicFilter . Dispose ( ) ;
120
141
comicFilter = null ;
121
142
}
122
143
123
- public void OnShareButtonClick ( )
144
+ public void OnShareButtonClick ( )
124
145
{
125
- NatShare . Share ( texture ,
126
- ( ) => {
127
- Debug . Log ( "sharing is complete." ) ;
146
+ NatShare . Share ( texture ,
147
+ ( ) =>
148
+ {
149
+ Debug . Log ( "sharing is complete." ) ;
128
150
} ) ;
129
151
}
130
152
131
- public void OnSaveToCameraRollButtonClick ( )
153
+ public void OnSaveToCameraRollButtonClick ( )
132
154
{
133
- NatShare . SaveToCameraRoll ( texture , "NatCamWithOpenCVForUnityExample" ) ;
155
+ NatShare . SaveToCameraRoll ( texture , "NatCamWithOpenCVForUnityExample" ) ;
134
156
}
135
157
}
136
158
}
0 commit comments