2007年8月8日 星期三

建立相片安全色XML資料庫

為了要為一堆相片,各決定一個像素值代表這張相片,所以我寫了一個連續載圖程式,並將他轉成WEB216安全色,然後再決定一個代表像素,並 trace 出來,未來這份結果就可存成一份 XML 資料檔案,作為其他應用之用!








import flash.display.BitmapData;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
//----------------------------------取得WEB216安全色的BitmapData
function get256bd(srcBD:BitmapData):BitmapData{
var safeColorArray:Array = [0x00, 0x33, 0x66, 0x99, 0xCC, 0xFF];

var redArray:Array = new Array(256);
var greenArray:Array = new Array(256);
var blueArray:Array = new Array(256);

for(var i:Number = 0; i < 256; i++) {
var c:Number = i;
for(j=0; j<safeColorArray.length; j++){
if(c <= safeColorArray[j]){
c = safeColorArray[j];
break;
}
}
redArray[i] = c << 16;
greenArray[i] = c << 8;
blueArray[i] = c;
}

var rect:Rectangle = new Rectangle(0, 0, srcBD.width, srcBD.height);
var pt:Point = new Point(0, 0);
var bd = new BitmapData(srcBD.width, srcBD.height, true, 0x00000000);
bd.paletteMap(srcBD, rect, pt, redArray, greenArray, blueArray);

return bd;
}
//----------------------------------由指定範圍取出一個像素值
function getPixelByRect(sourceBD:BitmapData, rect:Rectangle):Number{
var rectBD:BitmapData = new BitmapData(rect.width, rect.height, true, 0x00000000);
rectBD.copyPixels(sourceBD, rect, new Point(0, 0));

var bd1Pixel:BitmapData = new BitmapData(1, 1, true, 0x00000000);
var matrix:Matrix = new Matrix();
matrix.scale(1/rect.width, 1/rect.height);
bd1Pixel.draw(rectBD, matrix);
return bd1Pixel.getPixel32(0, 0);
}
//----------------------------------載入圖檔
//圖檔路徑,其中{?}會自動換成1~totalCount
var imagePath:String = "http://ben.rb.chang.googlepages.com/photos{?}.JPG";
var totalCount:Number = 9;

//var imagePath:String = "images\\photo ({?}).JPG";
//var totalCount:Number = 308;

var idx:Number = 0;

var _mcl:MovieClipLoader = new MovieClipLoader();
var _mclListener:Object = {};
_mclListener.onLoadInit = function(target_mc:MovieClip){
//trace(target_mc._width + ", " + target_mc._height);

var bd:BitmapData = new BitmapData(target_mc._width, target_mc._height, true, 0x00000000);
bd.draw(target_mc);

bd = get256bd(bd);

var rect:Rectangle = new Rectangle(o, o, target_mc._width, target_mc._height);
var color:Number = getPixelByRect(bd, rect);

var path:String = imagePath.split("{?}").join(idx.toString());
trace("<photo fileName=\"" + path + "\" color=\"" + color + "\" />");

var img2_mc:MovieClip = _root.createEmptyMovieClip("img2_mc", 2);
img2_mc.attachBitmap(bd, 2);

var colorBD:BitmapData = new BitmapData(100, 100);
colorBD.fillRect(new Rectangle(0, 0, 100, 100), color);
var img3_mc:MovieClip = _root.createEmptyMovieClip("img3_mc", 3);
img3_mc.attachBitmap(colorBD, 2);

loadNext();
}
_mcl.addListener(_mclListener);

var oraImg_mc:MovieClip = _root.createEmptyMovieClip("oraImg_mc", _root.getNextHighestDepth());

function loadNext(){
idx++;
if(idx<=totalCount){
var path:String = imagePath.split("{?}").join(idx.toString());
_mcl.loadClip(path, oraImg_mc);
}
}
loadNext();





沒有留言: