MobileFlex调用摄像头.docx
- 文档编号:560285
- 上传时间:2023-04-29
- 格式:DOCX
- 页数:19
- 大小:20.59KB
MobileFlex调用摄像头.docx
《MobileFlex调用摄像头.docx》由会员分享,可在线阅读,更多相关《MobileFlex调用摄像头.docx(19页珍藏版)》请在冰点文库上搜索。
MobileFlex调用摄像头
MobileFlex调用摄像头
利用flashair技术可跨平台实现摄像头调用功能,并且实现了拍照并保存照片。
(基于flex4.6开发)
Mxml文件代码如下:
xmlversion="1.0"encoding="GBK"?
>
Applicationxmlns: fx=" xmlns: s="library: //applicationDPI="240"width="480"height="800"> Declarations> --将非可视元素(例如服务、值对象)放在此处--> Declarations> 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(); } ]]> Script> Buttonx="43"y="585"label="照相"width="168"height="55"click="ini()"fontSize="24"/> Buttonx="256"y="585"id="SavePic"label="保存照片"width="160"height="55"click="save_pic()"fontSize="24"/> Imageid="Img"x="43"y="30"width="371"height="494"scaleMode="stretch"smooth="true" smoothingQuality="high"> source> MultiDPIBitmapSourcesource240dpi="@Embed('../bin-debug/dd.jpg')"/> source> Image> Buttonx="145"y="678"width="157"label="退出"click="Close()"/> Application> App配置xml文件内容如下: xmlversion="1.0"encoding="utf-8"standalone="no"? > --AdobeAIRApplicationDescriptorFileTemplate. Specifiesparametersforidentifying,installing,andlaunchingAIRapplications. xmlns-TheAdobeAIRnamespace: Thelastsegmentofthenamespacespecifiestheversion oftheAIRruntimerequiredforthisapplicationtorun. minimumPatchLevel-TheminimumpatchleveloftheAIRruntimerequiredtorun theapplication.Optional. --> --Auniversallyuniqueapplicationidentifier.MustbeuniqueacrossallAIRapplications. UsingareverseDNS-stylenameastheidisrecommended.(Eg.com.example.ExampleApplication.)Required.--> --Usedasthefilenamefortheapplication.Required.--> --ThenamethatisdisplayedintheAIRapplicationinstaller. Mayhavemultiplevaluesforeachlanguage.Seesamplesorxsdschemafile.Optional.--> --Astringvalueoftheformat<0-999>.<0-999>.<0-999>thatrepresentsapplicationversionwhichcanbeusedtocheckforapplicationupgrade. Valuescanalsobe1-partor2-part.Itisnotnecessarytohavea3-partvalue. AnupdatedversionofapplicationmusthaveaversionNumbervaluehigherthanthepreviousversion.Requiredfornamespace>=2.5.--> --Astringvalue(suchas"v1","2.5",or"Alpha1")thatrepresentstheversionoftheapplication,asitshouldbeshowntousers.Optional.--> -- --Description,displayedintheAIRapplicationinstaller. Mayhavemultiplevaluesforeachlanguage.Seesamplesorxsdschemafile.Optional.--> -- --Copyrightinformation.Optional--> -- --PublisherID.Usedifyou'reupdatinganapplicationcreatedpriorto1.5.3--> -- --Settingsfortheapplication'sinitialwindow.Required.--> --ThemainSWForHTMLfileoftheapplication.Required.--> --Note: InFlashBuilder,theSWFreferenceissetautomatically.--> --Thetitleofthemainwindow.Optional.--> -- --Thetypeofsystemchrometouse(either"standard"or"none").Optional.Defaultstandard.--> -- --Whetherthewindowistransparent.OnlyapplicablewhensystemChromeisnone.Optional.Defaultfalse.--> -- --Whetherthewindowisinitiallyvisible.Optional.Defaultfalse.--> -- --Whethertheusercanminimizethewindow.Optional.Defaulttrue.--> -- --Whethertheusercanmaximizethewindow.Optional.Defaulttrue.--> -- --Whethertheusercanresizethewindow.Optional.Defaulttrue.--> -- --Thewindow'sinitialwidthinpixels.Optional.--> -- --Thewindow'sinitialheightinpixels.Optional.--> -- --Thewindow'sinitialxposition.Optional.--> -- --Thewindow'sinitialyposition.Optional.--> -- --Thewindow'sminimumsize,specifiedasawidth/heightpairinpixels,suchas"400200".Optional.--> -- --Thewindow'sinitialmaximumsize,specifiedasawidth/heightpairinpixels,suchas"16001200".Optional.--> -- --Theinitialaspect
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- MobileFlex 调用 摄像头