leokan23

Untitled

Jun 13th, 2017
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MXML 6.15 KB | None | 0 0
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <s:VGroup xmlns:fx="http://ns.adobe.com/mxml/2009"
  3.           xmlns:s="library://ns.adobe.com/flex/spark" width="100%" height="100%" verticalAlign="middle" horizontalAlign="center">
  4.    
  5.     <fx:Script>
  6.         <![CDATA[
  7.             import com.milkmangames.nativeextensions.CoreMobile;
  8.            
  9.             import mx.core.FlexGlobals;
  10.             import mx.core.UIComponent;
  11.             import mx.graphics.codec.JPEGEncoder;
  12.            
  13.             public var position:String;
  14.             private var video:Video;
  15.             //private var _device:CaptureDevice;
  16.            
  17.             [Bindable]
  18.             public var wid:Number;
  19.             [Bindable]
  20.             public var hei:Number;
  21.            
  22.             [Bindable]
  23.             public var orientation:int;
  24.            
  25.             public function activateCamera():void
  26.             {
  27.                 if(Camera.isSupported)
  28.                 {
  29.                     //wid=FlexGlobals.topLevelApplication.width;
  30.                     //hei=(hei/wid)*FlexGlobals.topLevelApplication.width;
  31.                    
  32.                     wid=FlexGlobals.topLevelApplication.width;
  33.                     hei=FlexGlobals.topLevelApplication.width;
  34.                    
  35.                     videoDisplay.width=wid;
  36.                     videoDisplay.height=hei;
  37.                    
  38.                     this.findCamera();
  39.                 }
  40.                
  41.                
  42.                
  43.                
  44.                 trace("videodisplay: "+videoDisplay.width);
  45.                 trace(wid);
  46.                 trace(hei);
  47.             }
  48.            
  49.             public function deactivateCamera():void
  50.             {
  51.                 if (this.video != null)
  52.                 {
  53.                     this.video.attachCamera(null);
  54.                     this.videoDisplay.removeAllElements();
  55.                     this.video = null;
  56.                 }
  57.             }
  58.             public var m:Matrix;
  59.             private function findCamera():void
  60.             {
  61.                 if (!Camera.isSupported)
  62.                 {
  63.                     return;
  64.                 }
  65.                
  66.                 var cam:Camera = this.getCamera(this.position);
  67.                 cam.setMode(videoDisplay.width, videoDisplay.height, 30, true);
  68.                 cam.setMotionLevel(0);
  69.                 this.video = new Video(this.videoDisplay.width, this.videoDisplay.height);
  70.                
  71.                 var s:Number = 1.0;
  72.                 m = new Matrix();
  73.                
  74.                 if (orientation == 90 || orientation == 270)
  75.                 {
  76.                     s = this.videoDisplay.width / this.videoDisplay.height;
  77.                     trace(s);
  78.                     m.translate( -wid  * 0.5, -this.videoDisplay.height * 0.5 );
  79.                     m.rotate( DEG2RAD(orientation) );
  80.                    
  81.                     if (CoreMobile.isSupported())
  82.                     {
  83.                         if (CoreMobile.mobile.getDeviceDetails().deviceManufacturer =="Apple")
  84.                         {
  85.                             m.scale( s, s );
  86.                         }
  87.                         else
  88.                         {
  89.                             m.scale( -s, s );
  90.                         }
  91.                     }
  92.                     else
  93.                     {
  94.                         m.scale(s,s);
  95.                     }
  96.                    
  97.                     m.translate( this.videoDisplay.height * s * 0.5, this.videoDisplay.width * s * 0.5 );
  98.                 }
  99.                 else
  100.                 {
  101.                     s = this.videoDisplay.height / this.videoDisplay.width;
  102.                     trace(s);
  103.                     m.translate( - this.videoDisplay.width  * 0.5, - this.videoDisplay.height * 0.5 );
  104.                     m.rotate( DEG2RAD(orientation) );
  105.                     if (CoreMobile.isSupported())
  106.                     {
  107.                         if (CoreMobile.mobile.getDeviceDetails().deviceManufacturer =="Apple")
  108.                         {
  109.                             m.scale( s, s );
  110.                         }
  111.                         else
  112.                         {
  113.                             m.scale( -s, s );
  114.                         }
  115.                     }
  116.                     else
  117.                     {
  118.                         m.scale(s,s);
  119.                     }
  120.                     m.translate( this.videoDisplay.width * s * 0.5, this.videoDisplay.height * s * 0.5 );
  121.                 }
  122.                
  123.                
  124.                
  125.                
  126.                 //m.translate(-50,-50);
  127.                 //m.rotate(Math.PI*90/360); // 90 degrees
  128.                 //m.rotate(Math.PI*45/180);
  129.                 //m.translate(this.video.x+150,this.video.y+150);
  130.                 this.video.transform.matrix = m;
  131.                 this.video.attachCamera(cam);
  132.                 var uic:UIComponent = new UIComponent();
  133.                 uic.addChild(this.video);
  134.                 /*uic.x = ((videoDisplay.width/2) - (this.video.width/2)) + this.video.width;
  135.                 uic.y = ((videoDisplay.height/2) - (this.video.height/2)) - 50;*/
  136.                 uic.x = 0;
  137.                 uic.y = 0;
  138.                
  139.                 this.videoDisplay.addElement(uic);
  140.             }
  141.            
  142.             private function DEG2RAD( degrees:Number ):Number
  143.             {
  144.                 return degrees * Math.PI / 180;
  145.             }
  146.            
  147.             // Get the requested camera. If it cannot be found,
  148.             // return the device's default camera.
  149.             private function getCamera(position:String):Camera
  150.             {
  151.                 for (var i:uint = 0; i < Camera.names.length; ++i)
  152.                 {
  153.                     var cam:Camera = Camera.getCamera(String(i));
  154.                     if (cam.position == position) return cam;
  155.                 }
  156.                 return Camera.getCamera();
  157.             }
  158.            
  159.             [Bindable] public var bitmap:Bitmap;
  160.            
  161.             public function takeSnapshot():void
  162.             {
  163.                 trace("saving image");
  164.                 var _previewBitmapData:BitmapData = new BitmapData(this.video.width, this.video.height);
  165.                 //_previewBitmapData.draw(this.video);
  166.                
  167.                 var scaleFactor:int = 1;
  168.                
  169.                 //var tempBitmap:BitmapData = new BitmapData(videoDisplay.width/4, videoDisplay.height/4);
  170.                
  171.                 //var rotationMatrix:Matrix = new Matrix();
  172.                 //rotationMatrix.rotate( DEG2RAD(270) );
  173.                 //rotationMatrix.translate(videoDisplay.width, videoDisplay.height);
  174.                
  175.                 _previewBitmapData.draw(this.video, m);
  176.                 //_previewBitmapData.draw(tempBitmap, m);
  177.                
  178.                 var encoder:JPEGEncoder = new JPEGEncoder(30);
  179.                 var byteArray:ByteArray = encoder.encode(_previewBitmapData);
  180.                
  181.                 var f:File = File.documentsDirectory.resolvePath("IMG.jpg");                                    
  182.                 var stream:FileStream = new FileStream()
  183.                 stream.open(f, FileMode.WRITE);
  184.                
  185.                 stream.writeBytes(byteArray,0,byteArray.bytesAvailable);
  186.                 stream.close();
  187.                
  188.                 trace(f.nativePath);
  189.                
  190.                 /*              var fileReference:FileReference = new FileReference();
  191.                 fileReference.save(byteArray);*/
  192.             }
  193.            
  194.             public function takeSnapshot2():void
  195.             {
  196.                 trace("saving image");
  197.                 var _previewBitmapData:BitmapData = new BitmapData(this.video.width, this.video.height);
  198.                
  199.                 var scaleFactor:int = 1;
  200.                
  201.                 _previewBitmapData.draw(this.video, m);
  202.                
  203.                 var _previewBitmapRect:Rectangle = _previewBitmapData.rect;
  204.                 var _previewData:ByteArray = _previewBitmapData.encode(_previewBitmapRect, new JPEGEncoderOptions(100));
  205.                
  206.                 var f:File = File.applicationStorageDirectory.resolvePath("IMG.jpg");                                    
  207.                 var stream:FileStream = new FileStream()
  208.                 stream.open(f, FileMode.WRITE);
  209.                
  210.                 stream.writeBytes(_previewData,0,_previewData.bytesAvailable);
  211.                 stream.close();
  212.                
  213.                 _previewBitmapData.dispose();
  214.                 _previewData.clear();
  215.                
  216.                 trace(f.nativePath);
  217.             }
  218.            
  219.         ]]>
  220.     </fx:Script>
  221.    
  222.     <s:Group id="videoDisplay"  x="0" y="0" width="1080" height="1440"/>
  223.    
  224. </s:VGroup>
Add Comment
Please, Sign In to add comment