昨天用flex+php做了一個在線拍照的小東東,可以實現會員頭像的實時在線拍照更新。
首先來講一講原理:
1、將camera的內容顯示在video中,這個不懂的參考actionscript的手冊,裡面有詳細的講解以及代碼,
2、定義一下BitmapData對象,
m_pictureBitmapData = new BitmapData(DEFAULT_CAMERA_WIDTH,DEFAULT_CAMERA_HEIGHT);
然後將video的內容寫進BitmapData對象裡, m_pictureBitmapData.draw(video,new Matrix());
3、從左到右,從上到下,一個像素一個像素的讀取BitmapData的rgb值,所有的rgb值用","分開,寫進一個字符串裡,
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);//用amfphp進行保存
4、在服務端就把那些rgb值提取出來,一個像素一個像素的畫點:
$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;
5、詳細的源碼在附件裡面,前面只是些重要提示代碼。嗯,要懂得amfphp,還有flex。
還有什麼不清楚的,
下面來看看 test.html文件
if ( hasProductInstall && !hasRequestedVersion ) {
// MMdoctitle is the stored document.title value used by the installation process to close the window that started the process
// This is necessary in order to close browser windows that are still utilizing the older version of the player after installation has completed
// DO NOT MODIFY THE FOLLOWING FOUR LINES
// Location visited after installation is complete if installation is required
var MMPlayerType = (isIE == true) ? "ActiveX" : "PlugIn";
var MMredirectURL = window.location;
document.title = document.title.slice(0, 47) + " - Flash Player Installation";
var MMdoctitle = document.title;
AC_FL_RunContent(
"src", "playerProductInstall",
"FlashVars", "MMredirectURL="+MMredirectURL+'&MMplayerType='+MMPlayerType+'&MMdoctitle="+MMdoctitle+"",
"width", "100%",
"height", "100%",
"align", "middle",
"id", "hphoto",
"quality", "high",
"bgcolor", "#869ca7",
"name", "hphoto",
"allowScriptAccess","sameDomain",
"type", "application/x-shockwave-flash",
"pluginspage", "http://www.adobe.com/go/getflashplayer"
);
} else if (hasRequestedVersion) {
// if we've detected an acceptable version
// embed the Flash Content SWF when all tests are passed
AC_FL_RunContent(
"src", "hphoto",
"width", "100%",
"height", "100%",
"align", "middle",
"id", "hphoto",
"quality", "high",
"bgcolor", "#869ca7",
"name", "hphoto",
"flashvars",'historyUrl=history.htm%3F&lconid=' + lc_id + '',
"allowScriptAccess","sameDomain",
"type", "application/x-shockwave-flash",
"pluginspage", "http://www.adobe.com/go/getflashplayer"
);
} else { // flash is too old or we can't detect the plugin
var alternateContent = 'Alternate HTML content should be placed here. '
+ 'This content requires the Adobe Flash Player. '
+ '<a href=http://www.adobe.com/go/getflash/>Get Flash</a>';
document.write(alternateContent); // insert non-flash content
}
// -->
</script>
<noscript>
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
id="hphoto" width="100%" height="100%"
codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">
<param name="movie" value="hphoto.swf" />
<param name="quality" value="high" />
<param name="bgcolor" value="#869ca7" />
<param name="allowScriptAccess" value="sameDomain" />
<embed src="hphoto.swf" quality="high" bgcolor="#869ca7"
width="100%" height="100%" name="hphoto" align="middle"
play="true"
loop="false"
quality="high"
allowScriptAccess="sameDomain"
type="application/x-shockwave-flash"
pluginspage="http://www.adobe.com/go/getflashplayer">
</embed>
</object>