-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCameraFeedBehavior.cs
More file actions
116 lines (95 loc) · 3.46 KB
/
CameraFeedBehavior.cs
File metadata and controls
116 lines (95 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using UnityEngine;
using Vuforia;
public class CameraFeedBehavior : MonoBehaviour {
private Renderer rend;
private Vector3 videoTexSize = Vector3.one;
private Vuforia.Image image;
#region PRIVATE_MEMBERS
private Vuforia.Image.PIXEL_FORMAT mPixelFormat = Vuforia.Image.PIXEL_FORMAT.UNKNOWN_FORMAT;
private bool mAccessCameraImage = true;
private bool mFormatRegistered = false;
#endregion // PRIVATE_MEMBERS
#region MONOBEHAVIOUR_METHODS
void Start() {
rend = GetComponent<Renderer>();
#if UNITY_EDITOR
mPixelFormat = Vuforia.Image.PIXEL_FORMAT.RGBA8888;
#else
mPixelFormat = Vuforia.Image.PIXEL_FORMAT.RGB888; // Use RGB888 for mobile
#endif
// Register Vuforia life-cycle callbacks:
VuforiaARController.Instance.RegisterVuforiaStartedCallback(OnVuforiaStarted);
VuforiaARController.Instance.RegisterTrackablesUpdatedCallback(OnTrackablesUpdated);
VuforiaARController.Instance.RegisterOnPauseCallback(OnPause);
}
#endregion // MONOBEHAVIOUR_METHODS
#region PRIVATE_METHODS
void OnVuforiaStarted() {
// Try register camera image format
if (CameraDevice.Instance.SetFrameFormat(mPixelFormat, true)) {
Debug.Log("Successfully registered pixel format " + mPixelFormat.ToString());
mFormatRegistered = true;
} else {
Debug.LogError(
"\nFailed to register pixel format: " + mPixelFormat.ToString() +
"\nThe format may be unsupported by your device." +
"\nConsider using a different pixel format.\n");
mFormatRegistered = false;
}
}
/// <summary>
/// Called each time the Vuforia state is updated
/// </summary>
void OnTrackablesUpdated() {
if (mFormatRegistered) {
if (mAccessCameraImage) {
//get camera image
image = CameraDevice.Instance.GetCameraImage(mPixelFormat);
}
}
}
/// <summary>
/// Called when app is paused / resumed
/// </summary>
void OnPause(bool paused) {
if (paused) {
Debug.Log("App was paused");
UnregisterFormat();
} else {
Debug.Log("App was resumed");
RegisterFormat();
}
}
/// <summary>
/// Register the camera pixel format
/// </summary>
void RegisterFormat() {
if (CameraDevice.Instance.SetFrameFormat(mPixelFormat, true)) {
Debug.Log("Successfully registered camera pixel format " + mPixelFormat.ToString());
mFormatRegistered = true;
} else {
Debug.LogError("Failed to register camera pixel format " + mPixelFormat.ToString());
mFormatRegistered = false;
}
}
/// <summary>
/// Unregister the camera pixel format (e.g. call this when app is paused)
/// </summary>
void UnregisterFormat() {
Debug.Log("Unregistering camera pixel format " + mPixelFormat.ToString());
CameraDevice.Instance.SetFrameFormat(mPixelFormat, false);
mFormatRegistered = false;
}
public Color32[] GetImage(){
Texture2D camTex = new Texture2D(image.Width, image.Height);
//copy to texture
image.CopyToTexture (camTex);
//crop
var cropped = TextureTools.CropTexture (camTex);
//scale
var scaled = TextureTools.scaled (cropped, 224, 224, FilterMode.Bilinear);
//return scaled color32[]
return scaled.GetPixels32();
}
#endregion //PRIVATE_METHODS
}