flex+php在線拍照
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="100%" xmlns:ns1="*" backgroundGradientColors="[#ecf9ff, #ecf9ff]" height="100%" xmlns:ns2="view.*" creationComplete="initApp()">
<mx:Style>
Alert{font-size:12px;}
</mx:Style>
<mx:Script>
<![CDATA[
import mx.events.CloseEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;
import mx.managers.CursorManager;
private static const DEFAULT_CAMERA_WIDTH:Number = 160; //攝像頭顯示寬度
private static const DEFAULT_CAMERA_HEIGHT:Number = 120; //攝像頭顯示高度
private static const DEFAULT_WEBSERVICE_URL:String = "http://localhost:1888/Web/TestWebService.asmx?WSDL"; //WebService地址
private var m_camera:Camera; //定義一個攝像頭
private var m_localVideo:Video; //定義一個本地視頻
private var m_pictureBitmapData:BitmapData //定義視頻截圖
private var pic_width:int;
private var pic_height:int;
//[Bindable]
private var m_pictureData:String;
private function initApp():void
{
t_btn_Shooting.enabled = false;
t_ban_Save.enabled = false;
initCamera();
pic_height=m_camera.height;
pic_width=m_camera.width;
}
//初始化攝像頭
private function initCamera():void
{
m_camera = Camera.getCamera();
if(m_camera != null)
{
m_camera.addEventListener(StatusEvent.STATUS,__onCameraStatusHandler);
m_camera.setMode(DEFAULT_CAMERA_WIDTH,DEFAULT_CAMERA_HEIGHT,30);
m_localVideo = new Video();
m_localVideo.width = DEFAULT_CAMERA_WIDTH;
m_localVideo.height = DEFAULT_CAMERA_HEIGHT;
m_localVideo.attachCamera(m_camera);
t_vd_Video.addChild(m_localVideo);
}
else
{
Alert.show("沒有找到攝像頭,是否重新查找。","提示:",Alert.OK|Alert.NO,this,__InitCamera);
return;
}
}
//拍照按鈕事件,進行視頻截圖
private function SnapshotPicture():void
{
m_pictureBitmapData = new BitmapData(DEFAULT_CAMERA_WIDTH,DEFAULT_CAMERA_HEIGHT);
m_pictureBitmapData.draw(t_vd_Video,new Matrix());
var m_pictureBitmap:Bitmap = new Bitmap(m_pictureBitmapData);
t_img_Picture.addChild(m_pictureBitmap);
t_panel_Picture.visible = true;
t_ban_Save.enabled = true;
}
//保存按鈕事件,保存視頻截圖
//通過WebService保存
private function SavePicture():void
{
m_pictureData = "";
//m_pictureBitmapData.setPixel(1,1,6558750);
//label1.text=m_pictureBitmapData.getPixel(1,1).toString();
for(var i:int = 0; i < DEFAULT_CAMERA_WIDTH; i++)
{
for(var j:int = 0; j < DEFAULT_CAMERA_HEIGHT; j++)
{
if(m_pictureData.length > 0)
{
m_pictureData += "," + m_pictureBitmapData.getPixel(i,j).toString();
}
else
{
m_pictureData = m_pictureBitmapData.getPixel(i,j).toString();
}
}
}
service.getOperation("createjpeg").send(pic_width,pic_height,m_pictureData);
// t_ws_SavePicture.SavePicture.send();
}
internal function faultHandler(evt:FaultEvent):void{
//labelresult.text="error";
CursorManager.removeBusyCursor();
Alert.show("保存出錯","提示",Alert.YES,this);
}
internal function createImage(evt:ResultEvent):void{
//dg_article.dataProvider=evt.result;
CursorManager.removeBusyCursor();
Alert.show("保存成功","提示",Alert.YES,this);
var date:Date=new Date();
this.left.headerphoto.source="http://www.tiyi88.com/image/header/0.jpg?id="+date.getMilliseconds();
}
//檢測攝像頭權限事件
private function __onCameraStatusHandler(event:StatusEvent):void
{
if(!m_camera.muted)
{
t_btn_Shooting.enabled = true;
}
else
{
Alert.show("無法鏈接到活動攝像頭,是否重新檢測。","提示:",Alert.OK|Alert.NO,this,__InitCamera);
}
m_camera.removeEventListener(StatusEvent.STATUS,__onCameraStatusHandler);
}
//當攝像頭不存在,或連接不正常時重新獲取
private function __InitCamera(event:CloseEvent):void
{
if(event.detail == Alert.OK)
{
initApp();
}
}
]]>
</mx:Script>
<mx:RemoteObject id="service" fault="faultHandler(event)" showBusyCursor="true"
source="image" destination="amfphp">
<!--定義遠程方法-->
<mx:method name="createjpeg" result="createImage(event)"/>
</mx:RemoteObject>
<ns1:header x="137" y="10">
</ns1:header>
<ns1:left1 x="137" y="158" id="left">
</ns1:left1>
<mx:Panel x="406" y="158" width="180" height="232" layout="absolute" title="視頻拍照" fontSize="12">
<mx:VideoDisplay id="t_vd_Video" width="160" height="146"/>
<mx:ControlBar horizontalAlign="right">
<mx:Button id="t_btn_Shooting" label="拍照" click="SnapshotPicture()"/>
</mx:ControlBar>
</mx:Panel>
<mx:Panel id="t_panel_Picture" x="647" y="158" width="180" height="232" layout="absolute" title="拍照圖片" fontSize="12" visible="false">
<mx:Image id="t_img_Picture" x="0" y="0" width="160" height="144"/>
<mx:ControlBar horizontalAlign="right">
<mx:Button id="t_ban_Save" label="保存" click="SavePicture()" />
</mx:ControlBar>
</mx:Panel>
</mx:Application>
繪制頭像文件:
<?php
class Image{
public function createjpeg($width,$height,$bitmap_data)
{
$img=imagecreatetruecolor($width,$height);
$m_tempPics=explode(',',$bitmap_data);
for ($i = 0; $i < $width; $i++)
{
for ($j = 0; $j < $height; $j++)
{
$pic_argb =(int) $m_tempPics[$i * $height + $j];
imagesetpixel($img,$i,$j,$pic_argb);
}
}
imagejpeg($img,"../../image/header/0.jpg");
imagedestroy($img);
return true;
}
}
?>