SoFunction
Updated on 2025-04-02

Flex implements uploading camera to take photos and save the UI as a picture


<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="/mxml/2009"
xmlns:s="library:///flex/spark"
xmlns:mx="library:///flex/mx" minWidth="955" minHeight="600" creationComplete="application1_creationCompleteHandler(event)">
<fx:Script>
<![CDATA[
import ;
import ;
import ;
import ;
import ;
import ;
import ;

protected function application1_creationCompleteHandler(event:FlexEvent):void
{
initCamera(videoDis);
}

//Initialize the Camera control and add it in VideoDisplay
public function initCamera(videoDis:UIComponent):void
{
var myCamera:Camera = ();//Get client camera
(500,500,30);

var myVideo:Video = new Video(500,500);
(myCamera);//Get the video stream of the camera

(myVideo);
}

//Convert the visual UIComponent component to a picture
public function UItoBitmap(source:UIComponent,target:UIComponent):void
{
var width :int = ;
var height :int = ;
var bitmapData:BitmapData =new BitmapData(width,height);
(source,new Matrix());

var bitmap:Bitmap=new Bitmap(bitmapData);

var uic:UIComponent = new UIComponent();
(bitmap);
(uic);
}


//Save the visible UIComponent component as a local image
public function UISaveAsImg(imgID:UIComponent):void
{
var width :int = ;
var height :int = ;
var bitmapData:BitmapData =new BitmapData(width,height);
(imgID);

var byteArr:ByteArray = (new Rectangle(0,0,width,height));
var byteArr123:ByteArray =new JPEGEncoder().encodeByteArray(byteArr,width,height);

var fileRefer:FileReference = new FileReference();
(byteArr123,()+".png");
(,function completeHandler():void{
("Save locally successful");
});
}


//Upload photos to the server
protected function upLoadImg(imgID:UIComponent):void
{
var width :int = ;
var height :int = ;
var bitmapData:BitmapData =new BitmapData(width,height);
(imgID);

var byteArr:ByteArray = (new Rectangle(0,0,width,height));
var byteArr123:ByteArray =new JPEGEncoder().encodeByteArray(byteArr,width,height);

(byteArr123,"");
}


protected function webService_faultHandler(event:FaultEvent):void
{
(());
}
protected function webService_successHandler(event:ResultEvent):void
{
(());
}

]]>
</fx:Script>
<fx:Declarations>
<!-- Put non-visual elements (such as services, value objects) here -->
<s:WebService wsdl="http://10.19.1.48/upImg/?WSDL" fault="webService_faultHandler(event)">
<s:operation name="UploadFile" result="webService_successHandler(event)"></s:operation>
</s:WebService>
</fx:Declarations>
<s:VideoDisplay width="500" height="500" click="UItoBitmap(videoDis,t_img_Picture),UItoBitmap(videoDis,content)" toolTip="Click to take a photo"></s:VideoDisplay>
<mx:DateChooser x="62" y="508" click="UItoBitmap(myDate,t_img_Picture),UItoBitmap(myDate,content)" toolTip="Click to take a photo"/>

<mx:Image x="522" y="0" width="500" height="500" click="UISaveAsImg(t_img_Picture)" toolTip="Click to save local"/>
<mx:Canvas x="500" y="300" width="500" height="500" click="UISaveAsImg(content)" toolTip="Click to save local"></mx:Canvas>

<s:Button x="305" y="537" label="upload" width="130" height="64" click="upLoadImg(t_img_Picture)"/>

</s:Application>