Script>
[CDATA[
importponents.android.alert.AlertDialog;
importflash.events.*;
importflash.media.CameraUI;
importflash.media.MediaType;
importmx.formatters.DateFormatter;
importmx.graphics.codec.JPEGEncoder;
privatevarimgBD:
BitmapData=null;
privatevaralert:
AlertDialog;
privatefunctionini():
void{
SavePic.enabled=false;
if(CameraUI.isSupported==false){
trace("您似乎没有安装摄像头!
");
alert=newAlertDialog();
alert.title="提示";
alert.message="您似乎没有安装摄像头!
!
";
alert.open(this,true);
}else{
varcameraUI:
CameraUI=newCameraUI();
cameraUI.launch(MediaType.IMAGE);
cameraUI.addEventListener(MediaEvent.COMPLETE,onComplete);
cameraUI.addEventListener(Event.CANCEL,onCancel);
cameraUI.addEventListener(ErrorEvent.ERROR,onError);
}
}
privatevarbitmapData:
BitmapData;
privatevarfileSource:
IEventDispatcher;
privatevarfileDataSource:
IDataInput;
privatevarfileData:
ByteArray;
privatefunctiononComplete(event:
MediaEvent):
void{
varpromise:
MediaPromise=event.dataasMediaPromise;
varloader:
Loader=newLoader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onImageLoaded);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onError);
loader.loadFilePromise(promise);
//如果我不想显示,只是想得到该图片的二进制数据,就象打开一个File一样.用下面的方法.如果不用这个,请把下面的代码注释掉.不然会造成不能显示图片
//--------------Start
fileDataSource=promise.open();
if(promise.isAsync){
fileSource=fileDataSourceasIEventDispatcher;
fileSource.addEventListener(Event.COMPLETE,dataCompleteHandler);
}else{
readFileData();
}
//---------------end
}
privatefunctiondataCompleteHandler(event:
Event):
void{
fileData=newByteArray();
fileDataSource.readBytes(fileData);
}
//读取二进制数据
privatefunctionreadFileData():
void{
fileData=newByteArray();
fileDataSource.readBytes(fileData);
//sendDataToServer(fileData);
}
privatefunctiononCancel(event:
MediaEvent):
void{
trace("您取消了本次拍照!
");
alert=newAlertDialog();
alert.title="提示";
alert.message="您取消了本次拍照!
";
alert.open(this,true);
}
/*
privatefunctiononImageLoaded(event:
Event):
void{
bitmapData=Bitmap(event.currentTarget.content).bitmapData;
varbitmap:
Bitmap=newBitmap(bitmapData);
//determinetheimageorientation
varisPortrait:
Boolean=(bitmapData.height/bitmapData.width)>1.0;
//choosesmallestvaluebetweenstagewidthandheight
varforRatio:
int=Math.min(stage.stageHeight,stage.stageWidth);
//calculatethescalingratiotoapplytotheimage
varratio:
Number;
if(isPortrait){
ratio=forRatio/bitmapData.width;
}else{
ratio=forRatio/bitmapData.height;
}
bitmap.width=bitmapData.width*ratio;
bitmap.height=bitmapData.height*ratio;
if(!
isPortrait){
bitmap.y=bitmap.width;
bitmap.rotation=-90;
}
addChild(bitmap);
Img.source=bitmap;
SavePic.enabled=true;
}
*/
privatefunctiononImageLoaded(e:
Event):
void{
varmediaLoaderInfo:
LoaderInfo=e.targetasLoaderInfo;
mediaLoaderInfo.removeEventListener(Event.COMPLETE,onImageLoaded);
mediaLoaderInfo.loader.removeEventListener(IOErrorEvent.IO_ERROR,onError);
this.Img.source=mediaLoaderInfo.loader;
bitmapData=this.Img.bitmapData;
SavePic.enabled=true;
}
privatefunctiononError(event:
Event):
void{
trace("图像加载错误:
"+event.toString(),"");
alert=newAlertDialog();
alert.title="提示";
alert.message="图像加载错误!
";
alert.open(this,true);
}
privatefunctionsave_pic():
void{
//if(bitmapData){
//varjpg:
JPEGEncoder=newJPEGEncoder();
//varbyt:
ByteArray=jpg.encode(bitmapData);
vardateFormatter:
DateFormatter=newDateFormatter();
dateFormatter.formatString="YYYYMMDDJJNNSS";
varnow:
String=dateFormatter.format(newDate());
varfl:
File=newFile(File.desktopDirectory.resolvePath(now+'.jpg').nativePath);
varfs:
FileStream=newFileStream();
try{
//openfileinwritemode
fs.open(fl,FileMode.WRITE);
//writebytesfromthebytearray
//fs.writeBytes(byt);
fs.writeBytes(fileData);
//closethefile
fs.close();
trace("成功保存图片到本地!
"+File.desktopDirectory.resolvePath(now+'.jpg').nativePath);
alert=newAlertDialog();
alert.title="提示";
alert.message="成功保存图片到本地!
\r\n"+File.desktopDirectory.resolvePath(now+'.jpg').nativePath;
alert.open(this,true);
}catch(e:
Error){
trace(e.message);
}
//}
}
privatefunctiondownloadComplete(event:
Event):
void{
trace("成功保存图片到本地!
");
}
privatefunctionClose():
void{
NativeApplication.nativeApplication.exit();
}
]]>