2010年11月14日 星期日

又寫了一個新的jquery plugin - jquery imgdesc

最近一個project是需要有產品瀏覽..
而每個產品還有詳細描述
為了增加畫面的簡潔度跟流暢感
所以又寫了一個jquery的plugin imgdesc
呈現的結果是這樣

要使用請按照下面的步驟
1. 首先先確定你的html的DOCTYPE如下

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

這樣跑出來的結果位置才會對

2. include需要的javascript以及css

<script type="text/javascript" src="js/jquery-1.4.3.min.js"></script>
<script type="text/javascript" src="js/jquery-imgdesc.js"></script>
<link type="text/css" rel="stylesheet" href="css/jquery-imgdesc.css">


3. 資料的部分,請這樣定義



Title

Your description




Title

Your description




Title

Your description




4. 最後一步就是呼叫imgdesc()套上imgdesc的plugin

$(function()
{
$(".modelImage").imgdesc();
});

2010年10月26日 星期二

用jquery來實作多層menu

最近開始玩jquery
拿了一個手中案子的多層Menu當作練習..
並且把這個menu做成jquery的plugin
出來的結果是這樣

使用方法很簡單...
首先用ul跟li來定義menutree




再來就是用jquery的語法來產生menu

$(function()
{
$("#menubar_content").menu();
});


用法很簡單吧..
這都歸功於menu強大又精簡的framework.. (y)

2010年7月13日 星期二

用AS3上傳圖片

前一篇文章討論到要怎麼擷取webcam的照片,但是照好的相片好像最常的應用是丟到server上吧。但是在AS3提共的FileReferene這個class只能提供讓使用者選檔案,而不能直接上傳我們擷取的bitmap。

好在google中找到不錯的文章(how to impress your friends by taking an image snapshot of a flash movie and automatically uploading the jpg to a server in three easy steps)。作者寫了一個Helper來達到這個需求。

首先你要把它的這個Helper Class抓下來,放在你的project當中。再來因為我們上傳可能需要encode成JPEG,所以我們需要用到as3corelib裡面的JPGEncoder轉成ByteArray。最後,你要在server端寫一個php或是server-side的程式去接你上傳的檔案。

以下是我的flex程式

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="800" minHeight="600">


<fx:Script>
<![CDATA[
import com.adobe.images.JPGEncoder;

import mx.controls.Alert;

private var bitmapData:BitmapData = new BitmapData(640, 480); // the current capture data
private var encoder:JPGEncoder = new JPGEncoder(80);
[Bindable] private var isCapture:Boolean = false;

public static const PATH:String = "http://your_url/";
public static const FILE:String = "my.jpg";


private function videoDisplay_creationComplete():void {
var camera:Camera = Camera.getCamera();
if (camera) {
camera.setQuality(0, 100);
videoDisplay.attachCamera(camera);
} else {
Alert.show("You don't seem to have a camera.");
}
}

private function capture(): void {
var bitmap:Bitmap = new Bitmap(bitmapData);

bitmapData.draw(videoDisplay);
bitmap new Bitmap(bitmapData);

while(panel.rawChildren.numChildren > 0)
{
panel.rawChildren.removeChildAt(0);
}
panel.rawChildren.addChild(bitmap);
isCapture = true;
}

private function update(): void {
var byteArray:ByteArray = encoder.encode(bitmapData);
var request:URLRequest = new URLRequest();
var loader:URLLoader = new URLLoader();
var parameters:Object = new Object();

request.url = PATH;
request.contentType = 'multipart/form-data; boundary=' + UploadPostHelper.getBoundary();
request.method = URLRequestMethod.POST;
request.data = UploadPostHelper.getPostData(FILE, byteArray, parameters);
request.requestHeaders.push( new URLRequestHeader('Cache-Control', 'no-cache'));

loader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, onComplete);
loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
loader.load(request);
}

private function onComplete(event:Event): void
{
var loader:URLLoader = event.target as URLLoader;
Alert.show("complete" + loader.data);
}

private function onError(event:Event): void
{
Alert.show("error");
}

]]>
</fx:Script>

<mx:VideoDisplay id="videoDisplay"
creationComplete="videoDisplay_creationComplete();"
width="640"
height="480" />

<mx:Panel id="panel" width="640" height="480" x="680" y="0"/>
<s:Button x="10" y="548" label="capture" click="capture()"/>
<s:Button x="100" y="548" label="update" click="update()" enabled="{isCapture==true}"/>
</s:Application>


再來就是接收端php的程式


<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2000000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>

2010年7月12日 星期一

AS3 - 用Webcam來照相

這個技巧很簡單,主要有三樣東西
1. Camera: 這個代表的就是某個Webcam元件。
2. VideoDisplay: 這個control可以attach一個Camera。也就是讓Camera的資料顯示在上面。
2. BitmapData: 這個Class可以用來包含我們截圖下來的bitmap data.

當capture動作開始時,我們只要先產生一個BitmapData,並且用draw這個方法去把VideoDisplay裡面的Camera資料畫在BitmapData就好了。有了BitmapData後,我們用Bitmap把它包起來就可以放到讓何一個container裡面了。

注意的是因為Bitmap並不屬於IUIComponent,所以我們要加他的話,必須要用rawChildren才可以加進它唷,這邊注意一下。

下面是程式碼

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="800" minHeight="600">
<fx:Script>
<![CDATA[
import mx.controls.Alert;

private function videoDisplay_creationComplete():void {
var camera:Camera = Camera.getCamera();
if (camera) {
videoDisplay.attachCamera(camera);
} else {
Alert.show("You don't seem to have a camera.");
}
}

private function capture(): void {
var bitmapData:BitmapData = new BitmapData(320, 240);
bitmapData.draw(videoDisplay);
var bitmap:Bitmap = new Bitmap(bitmapData);
while(panel.rawChildren.numChildren > 0)
{
panel.rawChildren.removeChildAt(0);
}
panel.rawChildren.addChild(bitmap);
}

]]>
</fx:Script>

<mx:VideoDisplay id="videoDisplay"
creationComplete="videoDisplay_creationComplete();"
width="320"
height="240" />

<mx:Panel id="panel" width="320" height="240" x="340" y="0" title="Hello"/>
<s:Button x="10" y="248" click="capture()"/>
</s:Application>

2010年7月9日 星期五

Flex使用Json

最近的project需要用Flex去跟webservice撈資料
而因為吐出來的格式是json..所以稍微研究了一下
下面是我做出的筆記

1. 請去抓js3corelib,我們需要用到裡面的json library
2. 使用JSON.decode(),它會把json object轉換成as3的object..

下面是一個sample code

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:mycomp="*">

<fx:Script>
<![CDATA[
import com.adobe.serialization.json.*;
import flash.net.*;


protected function onClick(event:MouseEvent):void
{
var path:String = "http://your_url/";

var ur:URLRequest = new URLRequest(path);
var ul:URLLoader = new URLLoader();
ul.load(ur);
ul.addEventListener(Event.COMPLETE, onComplete);
}

protected function onComplete(evt:Event): void {
var ul:URLLoader = evt.target as URLLoader;
var obj:Object = JSON.decode(ul.data);
lbl.text = obj.foo;
}
]]>
</fx:Script>

<mx:HBox>
<s:Button label="Get" click="onClick(event)"/>
<s:Label id="lbl"/>
</mx:HBox>

</s:Application>

2010年6月24日 星期四

Flash debug..

目前好像比較好的方法是用flashbug這個firefox的plugin.
而flashbug本身是firebug的plugin...所以也要裝上firebug..

plugin抓完...要debug flash當然也要裝debug版的flash player
並且要抓debug版的flash player..

都裝好後..
記得到
c:\users\yourname\mm.cfg
加上這兩行
ErrorReportingEnable=1
TraceOutputFileEnable=1