Skip to content

Commit c7b4cb1

Browse files
committed
v0.0.3
Updated NatCam version to 2.0f1. Changed the preview data format to `RGBA32` on all platforms.
1 parent 0da1c44 commit c7b4cb1

File tree

5 files changed

+22
-42
lines changed

5 files changed

+22
-42
lines changed

Assets/NatCamWithOpenCVForUnityExample/NatCamPreviewOnlyExample/NatCamPreviewOnlyExample.cs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,9 @@ public enum ImageProcessingType
3333
public AspectRatioFitter aspectFitter;
3434

3535
bool didUpdateThisFrame = false;
36-
private Texture2D texture;
37-
private byte[] buffer;
38-
const TextureFormat textureFormat =
39-
#if UNITY_IOS && !UNITY_EDITOR
40-
TextureFormat.BGRA32;
41-
#else
42-
TextureFormat.RGBA32;
43-
#endif
36+
Texture2D texture;
37+
byte[] buffer;
38+
const TextureFormat textureFormat = TextureFormat.RGBA32;
4439

4540
int updateCount = 0;
4641
int onFrameCount = 0;
@@ -125,7 +120,7 @@ public override void OnStart ()
125120
Debug.Log(key + ": " + cameraProps[key]);
126121
}
127122

128-
if (fpsMonitor != null && verbose){
123+
if (fpsMonitor != null){
129124
fpsMonitor.boxWidth = 200;
130125
fpsMonitor.boxHeight = 620;
131126
fpsMonitor.LocateGUI();
@@ -139,7 +134,7 @@ public override void OnStart ()
139134
catch(Exception e)
140135
{
141136
Debug.Log ("Exception: " + e);
142-
if (fpsMonitor != null && verbose) {
137+
if (fpsMonitor != null) {
143138
fpsMonitor.consoleText = "Exception: " + e;
144139
}
145140
}
@@ -161,21 +156,26 @@ public override void OnFrame ()
161156
preview.texture = NatCam.Preview;
162157
} else {
163158

159+
// Size checking
160+
if (buffer != null && buffer.Length != NatCam.Preview.width * NatCam.Preview.height * 4) {
161+
buffer = null;
162+
}
163+
164164
// Create the managed buffer
165165
buffer = buffer ?? new byte[NatCam.Preview.width * NatCam.Preview.height * 4];
166166

167167
// Capture the current frame
168168
if (!NatCam.CaptureFrame (buffer)) return;
169169

170-
didUpdateThisFrame = true;
171-
172170
// Size checking
173171
if (texture && (texture.width != NatCam.Preview.width || texture.height != NatCam.Preview.height)) {
174172
Texture2D.Destroy (texture);
175173
texture = null;
176174
}
177175
// Create the texture
178176
texture = texture ?? new Texture2D (NatCam.Preview.width, NatCam.Preview.height, textureFormat, false, false);
177+
178+
didUpdateThisFrame = true;
179179
}
180180
}
181181

@@ -213,7 +213,7 @@ void Update()
213213

214214
} else {
215215

216-
if (texture.width * texture.height * 4 != buffer.Length)
216+
if (texture == null || buffer == null || texture.width * texture.height * 4 != buffer.Length)
217217
return;
218218

219219
// Process
@@ -257,11 +257,7 @@ private void ProcessImage (Byte[] buffer, int width, int height, int size, Image
257257
y = height-1;
258258
int p = (x * 4) + (y * width * 4);
259259
// Set pixels in the buffer
260-
#if UNITY_IOS && !UNITY_EDITOR
261-
buffer[p] = 0; buffer[p + 1] = 0; buffer[p + 2] = 255; buffer[p + 3] = 255;
262-
#else
263260
buffer[p] = 255; buffer[p + 1] = 0; buffer[p + 2] = 0; buffer[p + 3] = 255;
264-
#endif
265261
}
266262
}
267263

Assets/NatCamWithOpenCVForUnityExample/NatCamPreviewOnlyExample/NatCamPreviewOnlyExample.unity

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1954,7 +1954,6 @@ MonoBehaviour:
19541954
photoResolution:
19551955
width: 1920
19561956
height: 1080
1957-
verbose: 1
19581957
imageProcessingType: 0
19591958
imageProcessingTypeDropdown: {fileID: 1213656990}
19601959
requestedFPS: 30

Assets/NatCamWithOpenCVForUnityExample/NatCamPreviewToMatExample/NatCamPreviewToMatExample.cs

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,11 @@ public enum ImageProcessingType
5959
/// </summary>
6060
FpsMonitor fpsMonitor;
6161

62-
private Mat matrix;
63-
private Mat grayMatrix;
64-
private byte[] pixelBuffer;
65-
private Texture2D texture;
66-
private const TextureFormat textureFormat =
67-
#if UNITY_IOS && !UNITY_EDITOR
68-
TextureFormat.BGRA32;
69-
#else
70-
TextureFormat.RGBA32;
71-
#endif
62+
Mat matrix;
63+
Mat grayMatrix;
64+
byte[] pixelBuffer;
65+
Texture2D texture;
66+
const TextureFormat textureFormat = TextureFormat.RGBA32;
7267

7368
public override void Start ()
7469
{
@@ -267,11 +262,7 @@ private void ProcessImage (Mat matrix, ImageProcessingType imageProcessingType =
267262
switch (imageProcessingType) {
268263
case ImageProcessingType.DrawLine:
269264
// Draw a diagonal line on our image
270-
#if UNITY_IOS && !UNITY_EDITOR
271-
Imgproc.line (matrix, new Point (0, 0), new Point (matrix.cols (), matrix.rows ()), new Scalar (0, 0, 255, 255), 4);
272-
#else
273265
Imgproc.line (matrix, new Point (0, 0), new Point (matrix.cols (), matrix.rows ()), new Scalar (255, 0, 0, 255), 4);
274-
#endif
275266

276267
break;
277268
case ImageProcessingType.ConvertToGray:
@@ -281,14 +272,9 @@ private void ProcessImage (Mat matrix, ImageProcessingType imageProcessingType =
281272
grayMatrix = null;
282273
}
283274
grayMatrix = grayMatrix ?? new Mat(matrix.height(), matrix.width(), CvType.CV_8UC1);
284-
285-
#if UNITY_IOS && !UNITY_EDITOR
286-
Imgproc.cvtColor (matrix, grayMatrix, Imgproc.COLOR_BGRA2GRAY);
287-
Imgproc.cvtColor (grayMatrix, matrix, Imgproc.COLOR_GRAY2BGRA);
288-
#else
275+
289276
Imgproc.cvtColor (matrix, grayMatrix, Imgproc.COLOR_RGBA2GRAY);
290277
Imgproc.cvtColor (grayMatrix, matrix, Imgproc.COLOR_GRAY2RGBA);
291-
#endif
292278

293279
break;
294280
}

Assets/NatCamWithOpenCVForUnityExample/NatCamPreviewToMatExample/NatCamPreviewToMatExample.unity

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ MonoBehaviour:
210210
m_Value: 0
211211
m_Options:
212212
m_Options:
213-
- m_Text: 'Method: PreviewBuffer'
213+
- m_Text: 'Method: CaptureFrame'
214214
m_Image: {fileID: 0}
215215
- m_Text: 'Method: BlitWithReadPixels'
216216
m_Image: {fileID: 0}
@@ -2497,7 +2497,7 @@ MonoBehaviour:
24972497
m_HorizontalOverflow: 0
24982498
m_VerticalOverflow: 0
24992499
m_LineSpacing: 1
2500-
m_Text: 'Method: PreviewBuffer'
2500+
m_Text: 'Method: CaptureFrame'
25012501
--- !u!222 &1332287264
25022502
CanvasRenderer:
25032503
m_ObjectHideFlags: 0
@@ -2642,7 +2642,6 @@ MonoBehaviour:
26422642
photoResolution:
26432643
width: 1920
26442644
height: 1080
2645-
verbose: 1
26462645
matCaptureMethod: 0
26472646
matCaptureMethodDropdown: {fileID: 67966319}
26482647
imageProcessingType: 0

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ An example of a benchmark test integrating NatCam and OpenCVForUnity.
77
* Anddroid (Pixel, Nexus 7, Zenfone, SC-04E) / iOS (iPhone8, iPhone6)
88
* Unity >= 5.6.1f1
99
* Scripting backend MONO / IL2CPP
10-
* NatCam Pro 1.6f2 ([https://assetstore.unity.com/packages/tools/integration/natcam-pro-webcam-api-52154](https://assetstore.unity.com/packages/tools/integration/natcam-pro-webcam-api-52154))
10+
* NatCam - WebCam API 2.0f1 ([https://assetstore.unity.com/packages/tools/integration/natcam-pro-webcam-api-52154](https://assetstore.unity.com/packages/tools/integration/natcam-pro-webcam-api-52154))
1111
* OpenCV for Unity 2.2.8 ([https://assetstore.unity.com/packages/tools/integration/opencv-for-unity-21088](https://assetstore.unity.com/packages/tools/integration/opencv-for-unity-21088))
1212

1313

0 commit comments

Comments
 (0)