8
8
using NatCamU . Core ;
9
9
using NatShareU ;
10
10
11
- namespace NatCamWithOpenCVForUnityExample {
11
+ namespace NatCamWithOpenCVForUnityExample
12
+ {
12
13
13
14
/// <summary>
14
15
/// Integration With NatShare Example
15
16
/// An example of the native sharing and save to the camera roll using NatShare.
16
17
/// </summary>
17
- public class IntegrationWithNatShareExample : ExampleBase < NatCamSource > {
18
+ public class IntegrationWithNatShareExample : ExampleBase < NatCamSource >
19
+ {
20
+ public Toggle applyComicFilterToggle ;
18
21
19
22
Mat frameMatrix ;
20
23
Texture2D texture ;
21
24
ComicFilter comicFilter ;
22
25
23
- protected override void Start ( ) {
26
+ FpsMonitor fpsMonitor ;
27
+
28
+ protected override void Start ( )
29
+ {
24
30
// Load global camera benchmark settings.
25
31
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 ) ;
27
34
// 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 ) ;
30
37
// Create comic filter
31
38
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
+ }
32
50
}
33
51
34
- protected override void OnStart ( ) {
52
+ protected override void OnStart ( )
53
+ {
35
54
// 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 ) ;
37
58
// Create texture
38
- texture = new Texture2D (
59
+ if ( texture != null )
60
+ Texture2D . Destroy ( texture ) ;
61
+ texture = new Texture2D (
39
62
cameraSource . width ,
40
63
cameraSource . height ,
41
64
TextureFormat . RGBA32 ,
@@ -45,20 +68,47 @@ protected override void OnStart () {
45
68
// Display preview
46
69
rawImage . texture = texture ;
47
70
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
+ }
49
78
}
50
79
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
+ }
57
91
}
58
92
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
+ }
62
112
if ( frameMatrix != null )
63
113
frameMatrix . Dispose ( ) ;
64
114
frameMatrix = null ;
@@ -67,5 +117,18 @@ protected override void OnDestroy () {
67
117
comicFilter . Dispose ( ) ;
68
118
comicFilter = null ;
69
119
}
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
+ }
70
133
}
71
134
}
0 commit comments