11using UnityEngine ;
22using NatCamU . Core ;
3- using NatCamU . Pro ;
43using System . Collections . Generic ;
54using System ;
65using UnityEngine . UI ;
@@ -21,7 +20,7 @@ public class NatCamPreviewToMatExample : NatCamBehaviour
2120 {
2221 public enum MatCaptureMethod
2322 {
24- NatCam_PreviewBuffer ,
23+ NatCam_CaptureFrame ,
2524 BlitWithReadPixels ,
2625 OpenCVForUnity_LowLevelTextureToMat ,
2726 Graphics_CopyTexture ,
@@ -35,7 +34,7 @@ public enum ImageProcessingType
3534 }
3635
3736 [ Header ( "OpenCV" ) ]
38- public MatCaptureMethod matCaptureMethod = MatCaptureMethod . NatCam_PreviewBuffer ;
37+ public MatCaptureMethod matCaptureMethod = MatCaptureMethod . NatCam_CaptureFrame ;
3938 public Dropdown matCaptureMethodDropdown ;
4039 public ImageProcessingType imageProcessingType = ImageProcessingType . None ;
4140 public Dropdown imageProcessingTypeDropdown ;
@@ -62,6 +61,7 @@ public enum ImageProcessingType
6261
6362 private Mat matrix ;
6463 private Mat grayMatrix ;
64+ private byte [ ] pixelBuffer ;
6565 private Texture2D texture ;
6666 private const TextureFormat textureFormat =
6767 #if UNITY_IOS && ! UNITY_EDITOR
@@ -72,9 +72,12 @@ public enum ImageProcessingType
7272
7373 public override void Start ( )
7474 {
75- base . Start ( ) ;
76-
75+ // Set the active camera
76+ NatCam . Camera = useFrontCamera ? DeviceCamera . FrontCamera : DeviceCamera . RearCamera ;
77+ // Set the camera framerate
7778 NatCam . Camera . SetFramerate ( requestedFPS ) ;
79+ // Perform remaining camera setup
80+ base . Start ( ) ;
7881
7982 fpsMonitor = GetComponent < FpsMonitor > ( ) ;
8083 if ( fpsMonitor != null ) {
@@ -97,20 +100,22 @@ public override void OnStart ()
97100 {
98101 // Initialize the texture
99102 // 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 ) ) {
108112 matrix . Dispose ( ) ;
109113 matrix = null ;
110114 }
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 ) ;
113117
118+ // Create display texture
114119 if ( texture && ( texture . width != matrix . cols ( ) || texture . height != matrix . rows ( ) ) ) {
115120 Texture2D . Destroy ( texture ) ;
116121 texture = null ;
@@ -192,30 +197,19 @@ void LateUpdate ()
192197 /// <summary>
193198 /// Gets the current camera preview frame that converted to the correct direction in OpenCV Matrix format.
194199 /// </summary>
195- private Mat GetMat ( MatCaptureMethod matCaptureMethod = MatCaptureMethod . NatCam_PreviewBuffer )
200+ private Mat GetMat ( MatCaptureMethod matCaptureMethod = MatCaptureMethod . NatCam_CaptureFrame )
196201 {
197202 if ( matrix . cols ( ) != NatCam . Preview . width || matrix . rows ( ) != NatCam . Preview . height )
198203 return null ;
199204
200205 switch ( matCaptureMethod ) {
201206 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 ) ;
219213
220214 break ;
221215 case MatCaptureMethod . BlitWithReadPixels :
0 commit comments