/
WebcamTexture.as
169 lines (145 loc) · 4.12 KB
/
WebcamTexture.as
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package away3d.textures
{
import away3d.tools.utils.TextureUtils;
import flash.display.BitmapData;
import flash.display3D.textures.TextureBase;
import flash.geom.Matrix;
import flash.media.Camera;
import flash.media.Video;
public class WebcamTexture extends BitmapTexture
{
private var _materialSize:uint;
private var _video:Video;
private var _camera:Camera;
private var _matrix:Matrix;
private var _smoothing:Boolean;
private var _playing:Boolean;
private var _autoUpdate:Boolean;
public function WebcamTexture(cameraWidth:uint = 320, cameraHeight:uint = 240, materialSize:uint = 256, autoStart:Boolean = true, camera:Camera = null, smoothing:Boolean = true)
{
_materialSize = validateMaterialSize(materialSize);
super(new BitmapData(_materialSize, _materialSize, false, 0));
// Use default camera if none supplied
_camera = camera || Camera.getCamera();
_video = new Video(cameraWidth, cameraHeight);
_matrix = new Matrix();
_matrix.scale(_materialSize/cameraWidth, _materialSize/cameraHeight);
if (autoStart) {
_autoUpdate = true;
start();
}
_smoothing = smoothing;
}
/**
* Defines whether the texture should automatically update while camera stream is
* playing. If false, the update() method must be invoked for the texture to redraw.
*/
public function get autoUpdate():Boolean
{
return _autoUpdate;
}
public function set autoUpdate(val:Boolean):void
{
_autoUpdate = val;
if (_autoUpdate && _playing)
invalidateContent();
}
/**
* The Camera instance (webcam) used by this texture.
*/
public function get camera():Camera
{
return _camera;
}
/**
* Toggles smoothing on the texture as it's drawn (and potentially scaled)
* from the video stream to a BitmapData object.
*/
public function get smoothing():Boolean
{
return _smoothing;
}
public function set smoothing(value:Boolean):void
{
_smoothing = value;
}
/**
* Start subscribing to camera stream. For the texture to update the update()
* method must be repeatedly invoked, or autoUpdate set to true.
*/
public function start():void
{
_video.attachCamera(_camera);
_playing = true;
invalidateContent();
}
/**
* Detaches from the camera stream.
*/
public function stop():void
{
_playing = false;
_video.attachCamera(null);
}
/**
* Draws the video and updates the bitmap texture
* If autoUpdate is false and this function is not called the bitmap texture will not update!
*/
public function update():void
{
// draw
bitmapData.lock();
bitmapData.fillRect(bitmapData.rect, 0);
bitmapData.draw(_video, _matrix, null, null, bitmapData.rect, _smoothing);
bitmapData.unlock();
invalidateContent();
}
/**
* Flips the image from the webcam horizontally
*/
public function flipHorizontal():void
{
_matrix.a = -1*_matrix.a;
_matrix.a > 0? _matrix.tx = _video.x - _video.width*Math.abs(_matrix.a) : _matrix.tx = _video.width*Math.abs(_matrix.a) + _video.x;
}
/**
* Flips the image from the webcam vertically
*/
public function flipVertical():void
{
_matrix.d = -1*_matrix.d;
_matrix.d > 0? _matrix.ty = _video.y - _video.height*Math.abs(_matrix.d) : _matrix.ty = _video.height*Math.abs(_matrix.d) + _video.y;
}
/**
* Clean up used resources.
*/
override public function dispose():void
{
super.dispose();
stop();
bitmapData.dispose();
_video.attachCamera(null);
_camera = null;
_video = null;
_matrix = null;
}
override protected function uploadContent(texture:TextureBase):void
{
super.uploadContent(texture);
if (_playing && _autoUpdate) {
// Keep content invalid so that it will
// be updated again next render cycle
update();
}
}
private function validateMaterialSize(size:uint):int
{
if (!TextureUtils.isDimensionValid(size)) {
var oldSize:uint = size;
size = TextureUtils.getBestPowerOf2(size);
trace("Warning: " + oldSize + " is not a valid material size. Updating to the closest supported resolution: " + size);
}
return size;
}
}
}