Advertisement
Guest User

Untitled

a guest
Sep 16th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package {
  2.     import com.pfp.events.JPEGAsyncCompleteEvent;
  3.     import com.pfp.utils.JPEGAsyncEncoder;
  4.    
  5.     import flash.display.Bitmap;
  6.     import flash.display.BitmapData;
  7.     import flash.display.Sprite;
  8.     import flash.display.StageAlign;
  9.     import flash.display.StageScaleMode;
  10.     import flash.events.MouseEvent;
  11.     import flash.filesystem.File;
  12.     import flash.filesystem.FileMode;
  13.     import flash.filesystem.FileStream;
  14.     import flash.media.Camera;
  15.     import flash.media.Video;
  16.     import flash.utils.ByteArray;
  17.    
  18.     import qnx.dialog.AlertDialog;
  19.     import qnx.ui.buttons.LabelButton;
  20.     import qnx.ui.text.TextInput;
  21.    
  22.     public class fishyLightningCam extends Sprite {
  23.         private var bitmapData:BitmapData = new BitmapData(972, 546);
  24.         private var bitmap:Bitmap;
  25.         private var byteArray:ByteArray;
  26.        
  27.         private var file:File = File.documentsDirectory;
  28.         private var fstream:FileStream;
  29.        
  30.         private var captureBTN:LabelButton = new LabelButton();
  31.         private var discardBTN:LabelButton = new LabelButton();
  32.         private var saveBTN:LabelButton = new LabelButton();
  33.         private var fileName:TextInput = new TextInput();
  34.        
  35.         private var cam:Camera = Camera.getCamera("1");
  36.         private var vid:Video = new Video(972, 546);
  37.        
  38.         private var jpgEncoder:JPEGAsyncEncoder;
  39.        
  40.         public function fishyLightningCam() {
  41.             super();
  42.            
  43.             // support autoOrients
  44.             stage.align = StageAlign.TOP_LEFT;
  45.             stage.scaleMode = StageScaleMode.NO_SCALE;
  46.  
  47.             cam.setMode(2592, 1456, 30, true);
  48.             cam.setQuality(0, 100);    
  49.            
  50.             takePictures();
  51.         }
  52.         private function takePictures():void {
  53.             if (cam != null) {
  54.                 vid.attachCamera(cam);
  55.                 vid.x = 26;
  56.                 vid.y = 5;
  57.                 addChild(vid);
  58.             } else {
  59.                 var noCamAlert:AlertDialog = new AlertDialog();
  60.                 noCamAlert.title = "Camera Error";
  61.                 noCamAlert.message = "No camera was detected. Please ensure no other apps are using the camera.";
  62.                 noCamAlert.addButton("Okay");
  63.                 noCamAlert.show();
  64.             }
  65.            
  66.             captureBTN.label = "Capture!";
  67.             captureBTN.setPosition(400, 550);
  68.             captureBTN.width = 224;
  69.             captureBTN.addEventListener(MouseEvent.CLICK, captureImage);
  70.             addChild(captureBTN);
  71.            
  72.             fileName.prompt = "File Name";
  73.             fileName.setPosition(36, 555);
  74.             fileName.width = 350;
  75.             fileName.visible = false;
  76.             addChild(fileName);
  77.            
  78.             saveBTN.label = "Save";
  79.             saveBTN.setPosition(400, 550);
  80.             saveBTN.width = 100;
  81.             saveBTN.visible = false;
  82.             saveBTN.addEventListener(MouseEvent.CLICK, saveCapture);
  83.             addChild(saveBTN);
  84.            
  85.             discardBTN.label = "Discard";
  86.             discardBTN.setPosition(524, 550);
  87.             discardBTN.width = 100;
  88.             discardBTN.visible = false;
  89.             discardBTN.addEventListener(MouseEvent.CLICK, discardCapture);
  90.             addChild(discardBTN);
  91.         }
  92.         private function captureImage(e:MouseEvent):void {
  93.             bitmapData.draw(vid);
  94.             bitmap = new Bitmap(bitmapData);
  95.             bitmap.x = 26;
  96.             bitmap.y = 5;
  97.             addChild(bitmap);
  98.             removeChild(vid);
  99.            
  100.             captureBTN.visible = false;
  101.             saveBTN.visible = true;
  102.             discardBTN.visible = true;
  103.             fileName.visible = true;
  104.         }
  105.         private function saveCapture(e:MouseEvent):void {
  106.             jpgEncoder = new JPEGAsyncEncoder(100);
  107.             jpgEncoder.addEventListener(JPEGAsyncCompleteEvent.JPEGASYNC_COMPLETE, encodeWIN);
  108.             jpgEncoder.encode(bitmapData);
  109.         }
  110.         private function encodeWIN(e:JPEGAsyncCompleteEvent):void {
  111.             addChild(vid);
  112.             removeChild(bitmap);
  113.             fstream = new FileStream();
  114.             fstream.openAsync(file.resolvePath(fileName.text + ".jpg"), FileMode.WRITE);
  115.             fstream.writeBytes(e.ImageData);
  116.            
  117.             /* byteArray = new ByteArray();
  118.             byteArray = e.ImageData;
  119.            
  120.             byteArray.position = 0;
  121.             fstream.writeBytes(byteArray);
  122.             fstream.close();
  123.             */
  124.            
  125.             captureBTN.visible = true;
  126.             saveBTN.visible = false;
  127.             discardBTN.visible = false;
  128.             fileName.text = "";
  129.             fileName.visible = false;
  130.         }
  131.         private function discardCapture(e:MouseEvent):void {
  132.             addChild(vid);
  133.             removeChild(bitmap);
  134.            
  135.             captureBTN.visible = true;
  136.             saveBTN.visible = false;
  137.             discardBTN.visible = false;
  138.             fileName.text = "";
  139.             fileName.visible = false;
  140.         }
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement