Skip to content

Commit 0da1c44

Browse files
authored
Merge pull request #1 from olokobayusuf/natcam-20
Update to NatCam 2.0 API
2 parents 31f1792 + 3026c1d commit 0da1c44

File tree

2 files changed

+40
-58
lines changed

2 files changed

+40
-58
lines changed

Assets/NatCamWithOpenCVForUnityExample/NatCamPreviewOnlyExample/NatCamPreviewOnlyExample.cs

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
// Make sure to uncomment '#define OPENCV_API' in NatCam (Assets>NatCam>Pro>Plugins>Managed>NatCam.cs) and in OpenCVBehaviour
2-
//#define OPENCV_API // Uncomment this to run this example properly
3-
4-
using UnityEngine;
1+
using UnityEngine;
52
using NatCamU.Core;
6-
using NatCamU.Pro;
73
using System.Collections.Generic;
84
using System;
95
using UnityEngine.UI;
@@ -62,9 +58,12 @@ public enum ImageProcessingType
6258

6359
public override void Start ()
6460
{
65-
base.Start ();
66-
61+
// Set the active camera
62+
NatCam.Camera = useFrontCamera ? DeviceCamera.FrontCamera : DeviceCamera.RearCamera;
63+
// Set the camera framerate
6764
NatCam.Camera.SetFramerate (requestedFPS);
65+
// Perform remaining camera setup
66+
base.Start ();
6867

6968
fpsMonitor = GetComponent<FpsMonitor> ();
7069
if (fpsMonitor != null){
@@ -161,33 +160,22 @@ public override void OnFrame ()
161160
didUpdateThisFrame = true;
162161
preview.texture = NatCam.Preview;
163162
} else {
164-
// Declare buffer properties
165-
IntPtr handle;
166-
int width, height, size;
167-
// Read the preview buffer
168-
if (!NatCam.PreviewBuffer (out handle, out width, out height, out size))
169-
return;
170-
171-
didUpdateThisFrame = true;
172163

173-
// Size checking
174-
if (buffer != null && buffer.Length != size) {
175-
buffer = null;
176-
}
177164
// Create the managed buffer
178-
buffer = buffer ?? new byte[size];
165+
buffer = buffer ?? new byte[NatCam.Preview.width * NatCam.Preview.height * 4];
179166

180-
// Copy the pixel data from the native buffer into our managed bufffer
181-
// This is faster than accessing each byte using Marshal.ReadByte
182-
Marshal.Copy(handle, buffer, 0, size);
167+
// Capture the current frame
168+
if (!NatCam.CaptureFrame (buffer)) return;
169+
170+
didUpdateThisFrame = true;
183171

184172
// Size checking
185-
if (texture && (texture.width != width || texture.height != height)) {
173+
if (texture && (texture.width != NatCam.Preview.width || texture.height != NatCam.Preview.height)) {
186174
Texture2D.Destroy (texture);
187175
texture = null;
188176
}
189177
// Create the texture
190-
texture = texture ?? new Texture2D (width, height, textureFormat, false, false);
178+
texture = texture ?? new Texture2D (NatCam.Preview.width, NatCam.Preview.height, textureFormat, false, false);
191179
}
192180
}
193181

Assets/NatCamWithOpenCVForUnityExample/NatCamPreviewToMatExample/NatCamPreviewToMatExample.cs

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using UnityEngine;
22
using NatCamU.Core;
3-
using NatCamU.Pro;
43
using System.Collections.Generic;
54
using System;
65
using 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

Comments
 (0)