這篇其實是參考qop's blog的AIR 存圖測試變形而來,只是多加了Webcam去做抓圖的媒介,請先下載安裝as3corelib,它會以component的形式存在,使用時直接從component視窗拉出來後刪掉保留在library即可。
package {
import flash.utils.ByteArray;
import flash.media.Camera;
import flash.media.Video;
import flash.events.*;
import flash.display.*;
import com.adobe.images.JPGEncoder;
import flash.filesystem.*;
public class WebcamTest extends Sprite{
private var video:Video;
public function WebcamTest() {
stage.scaleMode = StageScaleMode.NO_SCALE;
showVideo();
btn.addEventListener(MouseEvent.CLICK, doClick);
}
public function showVideo() {
var camera:Camera = Camera.getCamera();
if (camera != null) {
video = new Video(320,240);
video.attachCamera(camera);
video.x=40;
video.y=20;
addChild(video);
} else {
trace("You need a camera.");
}
}
private function doClick(e:MouseEvent):void {
var file:File = File.applicationResourceDirectory;
var bd:BitmapData=new BitmapData(320,240, false, 0xffffff);
bd.draw(video);
var jpg:JPGEncoder = new JPGEncoder(80);
var ba:ByteArray = jpg.encode(bd);
file = file.resolve("test.jpg");
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.WRITE);
fileStream.writeBytes(ba);
fileStream.close();
}
}
}
輸出圖片:test.jpg
前輩你好XD 可以請問一下as3corelib要怎麼匯入組件裡呢?我爬了一下官方連結好像沒看到,麻煩指點一下,謝謝
ReplyDelete我是將corelib\bin\corelib.swc 放到
ReplyDeleteC:\Program Files\Adobe\Adobe Flash CS3\en\Configuration\Components 或 C:\Documents and Settings\[USER NAME]\Local Settings\Application Data\Adobe\Flash CS3\en\Configuration\Components 都可以,就可以在 Flash CS3組件視窗看到DateFormatter ,把它拉到library就可以用了...=P
成功了~太感謝了
ReplyDelete