Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?xml version="1.0" encoding="utf-8"?>
- <s:VGroup xmlns:fx="http://ns.adobe.com/mxml/2009"
- xmlns:s="library://ns.adobe.com/flex/spark" width="100%" height="100%" verticalAlign="middle" horizontalAlign="center">
- <fx:Script>
- <![CDATA[
- import com.milkmangames.nativeextensions.CoreMobile;
- import mx.core.FlexGlobals;
- import mx.core.UIComponent;
- import mx.graphics.codec.JPEGEncoder;
- public var position:String;
- private var video:Video;
- //private var _device:CaptureDevice;
- [Bindable]
- public var wid:Number;
- [Bindable]
- public var hei:Number;
- [Bindable]
- public var orientation:int;
- public function activateCamera():void
- {
- if(Camera.isSupported)
- {
- //wid=FlexGlobals.topLevelApplication.width;
- //hei=(hei/wid)*FlexGlobals.topLevelApplication.width;
- wid=FlexGlobals.topLevelApplication.width;
- hei=FlexGlobals.topLevelApplication.width;
- videoDisplay.width=wid;
- videoDisplay.height=hei;
- this.findCamera();
- }
- trace("videodisplay: "+videoDisplay.width);
- trace(wid);
- trace(hei);
- }
- public function deactivateCamera():void
- {
- if (this.video != null)
- {
- this.video.attachCamera(null);
- this.videoDisplay.removeAllElements();
- this.video = null;
- }
- }
- public var m:Matrix;
- private function findCamera():void
- {
- if (!Camera.isSupported)
- {
- return;
- }
- var cam:Camera = this.getCamera(this.position);
- cam.setMode(videoDisplay.width, videoDisplay.height, 30, true);
- cam.setMotionLevel(0);
- this.video = new Video(this.videoDisplay.width, this.videoDisplay.height);
- var s:Number = 1.0;
- m = new Matrix();
- if (orientation == 90 || orientation == 270)
- {
- s = this.videoDisplay.width / this.videoDisplay.height;
- trace(s);
- m.translate( -wid * 0.5, -this.videoDisplay.height * 0.5 );
- m.rotate( DEG2RAD(orientation) );
- if (CoreMobile.isSupported())
- {
- if (CoreMobile.mobile.getDeviceDetails().deviceManufacturer =="Apple")
- {
- m.scale( s, s );
- }
- else
- {
- m.scale( -s, s );
- }
- }
- else
- {
- m.scale(s,s);
- }
- m.translate( this.videoDisplay.height * s * 0.5, this.videoDisplay.width * s * 0.5 );
- }
- else
- {
- s = this.videoDisplay.height / this.videoDisplay.width;
- trace(s);
- m.translate( - this.videoDisplay.width * 0.5, - this.videoDisplay.height * 0.5 );
- m.rotate( DEG2RAD(orientation) );
- if (CoreMobile.isSupported())
- {
- if (CoreMobile.mobile.getDeviceDetails().deviceManufacturer =="Apple")
- {
- m.scale( s, s );
- }
- else
- {
- m.scale( -s, s );
- }
- }
- else
- {
- m.scale(s,s);
- }
- m.translate( this.videoDisplay.width * s * 0.5, this.videoDisplay.height * s * 0.5 );
- }
- //m.translate(-50,-50);
- //m.rotate(Math.PI*90/360); // 90 degrees
- //m.rotate(Math.PI*45/180);
- //m.translate(this.video.x+150,this.video.y+150);
- this.video.transform.matrix = m;
- this.video.attachCamera(cam);
- var uic:UIComponent = new UIComponent();
- uic.addChild(this.video);
- /*uic.x = ((videoDisplay.width/2) - (this.video.width/2)) + this.video.width;
- uic.y = ((videoDisplay.height/2) - (this.video.height/2)) - 50;*/
- uic.x = 0;
- uic.y = 0;
- this.videoDisplay.addElement(uic);
- }
- private function DEG2RAD( degrees:Number ):Number
- {
- return degrees * Math.PI / 180;
- }
- // Get the requested camera. If it cannot be found,
- // return the device's default camera.
- private function getCamera(position:String):Camera
- {
- for (var i:uint = 0; i < Camera.names.length; ++i)
- {
- var cam:Camera = Camera.getCamera(String(i));
- if (cam.position == position) return cam;
- }
- return Camera.getCamera();
- }
- [Bindable] public var bitmap:Bitmap;
- public function takeSnapshot():void
- {
- trace("saving image");
- var _previewBitmapData:BitmapData = new BitmapData(this.video.width, this.video.height);
- //_previewBitmapData.draw(this.video);
- var scaleFactor:int = 1;
- //var tempBitmap:BitmapData = new BitmapData(videoDisplay.width/4, videoDisplay.height/4);
- //var rotationMatrix:Matrix = new Matrix();
- //rotationMatrix.rotate( DEG2RAD(270) );
- //rotationMatrix.translate(videoDisplay.width, videoDisplay.height);
- _previewBitmapData.draw(this.video, m);
- //_previewBitmapData.draw(tempBitmap, m);
- var encoder:JPEGEncoder = new JPEGEncoder(30);
- var byteArray:ByteArray = encoder.encode(_previewBitmapData);
- var f:File = File.documentsDirectory.resolvePath("IMG.jpg");
- var stream:FileStream = new FileStream()
- stream.open(f, FileMode.WRITE);
- stream.writeBytes(byteArray,0,byteArray.bytesAvailable);
- stream.close();
- trace(f.nativePath);
- /* var fileReference:FileReference = new FileReference();
- fileReference.save(byteArray);*/
- }
- public function takeSnapshot2():void
- {
- trace("saving image");
- var _previewBitmapData:BitmapData = new BitmapData(this.video.width, this.video.height);
- var scaleFactor:int = 1;
- _previewBitmapData.draw(this.video, m);
- var _previewBitmapRect:Rectangle = _previewBitmapData.rect;
- var _previewData:ByteArray = _previewBitmapData.encode(_previewBitmapRect, new JPEGEncoderOptions(100));
- var f:File = File.applicationStorageDirectory.resolvePath("IMG.jpg");
- var stream:FileStream = new FileStream()
- stream.open(f, FileMode.WRITE);
- stream.writeBytes(_previewData,0,_previewData.bytesAvailable);
- stream.close();
- _previewBitmapData.dispose();
- _previewData.clear();
- trace(f.nativePath);
- }
- ]]>
- </fx:Script>
- <s:Group id="videoDisplay" x="0" y="0" width="1080" height="1440"/>
- </s:VGroup>
Add Comment
Please, Sign In to add comment