forked from mailru/FileAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileAPI_flash_camera.as
More file actions
125 lines (111 loc) · 2.96 KB
/
FileAPI_flash_camera.as
File metadata and controls
125 lines (111 loc) · 2.96 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
117
118
119
120
121
122
123
124
125
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.*;
import flash.media.Camera;
import flash.media.Video;
import flash.text.TextField;
public class FileAPI_flash_camera extends Sprite
{
private var video:Video;
private var camera:Camera;
public function FileAPI_flash_camera()
{
if (stage)
init();
else
addEventListener(Event.ADDED_TO_STAGE, init);
}
public function init(e:Event = null):void
{
if(e)
removeEventListener(Event.ADDED_TO_STAGE, init);
stage.scaleMode = StageScaleMode.SHOW_ALL;
stage.align = StageAlign.TOP_LEFT;
// init camera
camera = Camera.getCamera();
if (camera != null) {
camera.addEventListener(StatusEvent.STATUS, onCameraStatus);
// we need to show settings dialog, so we attach camera to a video
video = new Video();
video.attachCamera(camera);
} else {
// callback with error
}
// test
/*var tf:TextField = new TextField();
tf.text = 'on';
tf.x = 10;
tf.width = 30;
tf.addEventListener(MouseEvent.CLICK, function(event:MouseEvent):void {
toggleCamera(true);
});
addChild(tf);
tf = new TextField();
tf.text = 'off';
tf.x = 50;
tf.width = 30;
tf.addEventListener(MouseEvent.CLICK, function(event:MouseEvent):void {
toggleCamera(false);
});
addChild(tf);
tf = new TextField();
tf.text = 'photo';
tf.x = 100;
tf.width = 30;
tf.addEventListener(MouseEvent.CLICK, function(event:MouseEvent):void {
shot();
});
addChild(tf);*/
}
public function toggleCamera(on:Boolean):void {
trace('toggleCamera',on);
if (on) {
if (video != null) {
// turn current video off
toggleCamera(false);
}
trace('stage width',stage.stageWidth,'stage w',stage.width,'camera width',camera.width);
var w:Number = stage.stageWidth;
var h:Number = stage.stageHeight; //stage.stageWidth * camera.height / camera.width;
camera.setMode(w, h, camera.fps);
video = new Video(w, h);
video.attachCamera(camera);
video.addEventListener(Event.ADDED_TO_STAGE, function(event:Event):void {
trace('video addedToStage');
// now the camera is visible
dispatchEvent(new Event('Camera.On'));
});
addChildAt(video,0);
} else {
if(video)
removeChild(video);
video = null;
}
}
public function shot():BitmapData {
if (video) {
trace('click!', video.width, stage.stageWidth);
try{
var bm:BitmapData = new BitmapData(video.width,video.height);
bm.draw(video);
return bm;
} catch(error:Error) {
trace('error',error.toString() );
return null;
}
}
return null;
}
private function onCameraStatus(event:StatusEvent):void {
trace('status event:',event.toString() );
video = null; // turn off video
// redispatch
dispatchEvent(event.clone());
}
}
}