/** * Capture Image by USB-Camera then Save it as JPEG file. * * @author Sho Hashimoto * @version 1.1 * http://shokai.org * FlashCS3 + AIR beta3 * 2008/01/12 */ package { import fl.controls.*; import flash.display.*; import flash.filesystem.*; import flash.media.*; import flash.events.*; import flash.utils.*; import com.adobe.images.JPGEncoder; public class CameraSaveJpeg extends MovieClip{ var cam:Camera; var video:Video; var _buttonShot:Button; var _textAreaLog:TextArea; var _buttonClose:Button; var _bg:MovieClip; public function CameraSaveJpeg() { stage.scaleMode = StageScaleMode.EXACT_FIT; // 伸縮する // stage.scaleMode = StageScaleMode.NO_SCALE; // 伸縮しない stage.align = StageAlign.TOP_LEFT; // 左上から link_ide_obj() // IDEが生成したオブジェクトを関連づけ cam = Camera.getCamera(); cam.setMode(800,600,24,true);// 24FPS cam.setQuality(0, 100); video = new Video(cam.width, cam.height); video.attachCamera(cam); this.addChild(video); video.width = 200; video.height = 150; _bg.alpha = 0.5; _buttonShot.addEventListener(MouseEvent.CLICK, savePicture); _buttonClose.addEventListener(MouseEvent.CLICK, window_onClose); _bg.addEventListener(MouseEvent.MOUSE_DOWN, window_onMove); } /* IDEが生成したオブジェクトを関連づけ */ public function link_ide_obj() { this._buttonShot = buttonShot; this._textAreaLog = textAreaLog; this._bg = bg; this._buttonClose = buttonClose; } /* 現在の表示をファイルに保存 */ public function savePicture(e:Event) { _buttonShot.enabled = false; var bmd:BitmapData = new BitmapData(cam.width, cam.height); bmd.draw(video); var encoder:JPGEncoder = new JPGEncoder(95); try{ var bytes:ByteArray = encoder.encode(bmd); var file:File = new File(); var now:Date = new Date(); file.nativePath = File.applicationDirectory.nativePath + File.separator + now.time + ".jpg"; var fs:FileStream = new FileStream(); fs.open(file, FileMode.UPDATE); fs.writeBytes(bytes, 0, bytes.length); fs.close(); trace("saved! => " + file.nativePath); } catch (e:Error) { trace(e.message); } _buttonShot.enabled = true; } public function window_onClose(e:Event) { stage.nativeWindow.close(); } public function window_onMove(e:Event) { stage.nativeWindow.startMove(); } public function trace(message:String) { _textAreaLog.text = message + "\n" + _textAreaLog.text; } } }