1
1
using UnityEngine ;
2
2
using NatCamU . Core ;
3
- using NatCamU . Pro ;
4
3
using System . Collections . Generic ;
5
4
using System ;
6
5
using UnityEngine . UI ;
@@ -21,7 +20,7 @@ public class NatCamPreviewToMatExample : NatCamBehaviour
21
20
{
22
21
public enum MatCaptureMethod
23
22
{
24
- NatCam_PreviewBuffer ,
23
+ NatCam_CaptureFrame ,
25
24
BlitWithReadPixels ,
26
25
OpenCVForUnity_LowLevelTextureToMat ,
27
26
Graphics_CopyTexture ,
@@ -35,7 +34,7 @@ public enum ImageProcessingType
35
34
}
36
35
37
36
[ Header ( "OpenCV" ) ]
38
- public MatCaptureMethod matCaptureMethod = MatCaptureMethod . NatCam_PreviewBuffer ;
37
+ public MatCaptureMethod matCaptureMethod = MatCaptureMethod . NatCam_CaptureFrame ;
39
38
public Dropdown matCaptureMethodDropdown ;
40
39
public ImageProcessingType imageProcessingType = ImageProcessingType . None ;
41
40
public Dropdown imageProcessingTypeDropdown ;
@@ -62,6 +61,7 @@ public enum ImageProcessingType
62
61
63
62
private Mat matrix ;
64
63
private Mat grayMatrix ;
64
+ private byte [ ] pixelBuffer ;
65
65
private Texture2D texture ;
66
66
private const TextureFormat textureFormat =
67
67
#if UNITY_IOS && ! UNITY_EDITOR
@@ -72,9 +72,12 @@ public enum ImageProcessingType
72
72
73
73
public override void Start ( )
74
74
{
75
- base . Start ( ) ;
76
-
75
+ // Set the active camera
76
+ NatCam . Camera = useFrontCamera ? DeviceCamera . FrontCamera : DeviceCamera . RearCamera ;
77
+ // Set the camera framerate
77
78
NatCam . Camera . SetFramerate ( requestedFPS ) ;
79
+ // Perform remaining camera setup
80
+ base . Start ( ) ;
78
81
79
82
fpsMonitor = GetComponent < FpsMonitor > ( ) ;
80
83
if ( fpsMonitor != null ) {
@@ -97,20 +100,22 @@ public override void OnStart ()
97
100
{
98
101
// Initialize the texture
99
102
// NatCam.PreviewMatrix(ref matrix);
100
- IntPtr ptr ; int width , height , size ;
101
- if ( ! NatCam . PreviewBuffer ( out ptr , out width , out height , out size ) ) {
102
- if ( fpsMonitor != null ) {
103
- fpsMonitor . consoleText = "OnStart (): NatCam.PreviewBuffer() returned false." ;
104
- }
105
- return ;
106
- }
107
- if ( matrix != null && ( matrix . cols ( ) != width || matrix . rows ( ) != height ) ) {
103
+
104
+ // Create pixel buffer
105
+ pixelBuffer = new byte [ NatCam . Preview . width * NatCam . Preview . height * 4 ] ;
106
+
107
+ // Get the preview data
108
+ NatCam . CaptureFrame ( pixelBuffer , true ) ;
109
+
110
+ // Create preview matrix
111
+ if ( matrix != null && ( matrix . cols ( ) != NatCam . Preview . width || matrix . rows ( ) != NatCam . Preview . height ) ) {
108
112
matrix . Dispose ( ) ;
109
113
matrix = null ;
110
114
}
111
- matrix = matrix ?? new Mat ( height , width , CvType . CV_8UC4 ) ;
112
- Utils . copyToMat ( ptr , matrix ) ;
115
+ matrix = matrix ?? new Mat ( NatCam . Preview . height , NatCam . Preview . width , CvType . CV_8UC4 ) ;
116
+ matrix . put ( 0 , 0 , pixelBuffer ) ;
113
117
118
+ // Create display texture
114
119
if ( texture && ( texture . width != matrix . cols ( ) || texture . height != matrix . rows ( ) ) ) {
115
120
Texture2D . Destroy ( texture ) ;
116
121
texture = null ;
@@ -192,30 +197,19 @@ void LateUpdate ()
192
197
/// <summary>
193
198
/// Gets the current camera preview frame that converted to the correct direction in OpenCV Matrix format.
194
199
/// </summary>
195
- private Mat GetMat ( MatCaptureMethod matCaptureMethod = MatCaptureMethod . NatCam_PreviewBuffer )
200
+ private Mat GetMat ( MatCaptureMethod matCaptureMethod = MatCaptureMethod . NatCam_CaptureFrame )
196
201
{
197
202
if ( matrix . cols ( ) != NatCam . Preview . width || matrix . rows ( ) != NatCam . Preview . height )
198
203
return null ;
199
204
200
205
switch ( matCaptureMethod ) {
201
206
default :
202
- case MatCaptureMethod . NatCam_PreviewBuffer :
203
-
204
- // Get the current preview frame as an OpenCV matrix
205
- // NatCam.PreviewMatrix(ref matrix);
206
- IntPtr ptr ; int width , height , size ;
207
- if ( ! NatCam . PreviewBuffer ( out ptr , out width , out height , out size ) ) {
208
- return null ;
209
- }
210
- if ( matrix != null && ( matrix . cols ( ) != width || matrix . rows ( ) != height ) ) {
211
- matrix . Dispose ( ) ;
212
- matrix = null ;
213
- }
214
- matrix = matrix ?? new Mat ( height , width , CvType . CV_8UC4 ) ;
215
- Utils . copyToMat ( ptr , matrix ) ;
216
-
217
- // OpenCV uses an inverted coordinate system. Y-0 is the top of the image, whereas in OpenGL (and so NatCam), Y-0 is the bottom of the image.
218
- Core . flip ( matrix , matrix , 0 ) ;
207
+ case MatCaptureMethod . NatCam_CaptureFrame :
208
+ // Get the preview data
209
+ // Set `flip` flag to true because OpenCV uses inverted Y-coordinate system
210
+ NatCam . CaptureFrame ( pixelBuffer , true ) ;
211
+
212
+ matrix . put ( 0 , 0 , pixelBuffer ) ;
219
213
220
214
break ;
221
215
case MatCaptureMethod . BlitWithReadPixels :
0 commit comments