2010-06-16 21 views
-2

這是(我認爲)一個相對簡單的數學問題,但我已經花了一天撞我的頭,並具有唯一的凹痕和無解決方案...映射矩形到較大的圖像(爲了做一個copyPixels操作)

我編碼在ActionScript 3 - 的功能是:在運行時

  1. 大的圖像加載。 bitmapData被存儲,並創建一個較小的版本,以顯示在可用的屏幕區域(我可能最終只是縮放大圖像,因爲它在內存中)。

  2. 用戶可以創建小圖像上的矩形熱點(功能將更加複雜:多個rects具有透明度:例如炸面圈的形狀與孔等)

3當用戶點擊上所述熱點,所述熱點的矩形被映射到較大的圖像,並創建一個新位圖「標註」,利用較大的位圖數據。這樣做的原因是,「標註」的質量會比擴大熱點區域的質量更好。

下圖顯示了我在這裏這麼遠的藍色矩形是點擊的熱點。左上角是「標註」 - 從較大的圖像中複製而來。我的寬高比正確,但我沒有正確映射到較大的圖像。

下面醜陋的代碼...很抱歉這個帖子這麼久 - 我只是想我應該提供儘可能多的信息成爲可能。感謝您的任何提示!

--trace我的數據的值

*源BitmapDada 1152 864

縮放到RECT 800 600

縮放的BitmapData 800 600

選擇的BitmapData 58 56

縮放選擇83 80

比1.44

之前(X = 544,Y = 237,W = 58,H = 56)

(X = 544,Y = 237,W = 225.04,H = 217.28) *

的形象在這裏:http://i795.photobucket.com/albums/yy237/skinnyTOD/exampleST.jpg

public function onExpandCallout(event:MouseEvent):void{ 
    if (maskBitmapData.getPixel32(event.localX, event.localY) != 0){ 
     var maskClone:BitmapData = maskBitmapData.clone(); 

     //amount to scale callout - this will vary/can be changed by user 
     var scale:Number =150 //scale percentage 
     var normalizedScale :Number = scale/=100; 

     var w:Number = maskBitmapData.width*normalizedScale; 
     var h:Number = maskBitmapData.height*normalizedScale; 

     var ratio:Number = (sourceBD.width /targetRect.width); 

     //creat bmpd of the scaled size to copy source into 
     var scaledBitmapData:BitmapData = new BitmapData(maskBitmapData.width * ratio, maskBitmapData.height * ratio, true, 0xFFFFFFFF); 

     trace("source BitmapDada " + sourceBD.width, sourceBD.height); 
     trace("scaled to rect " + targetRect.width, targetRect.height); 
     trace("scaled BitmapData", bkgnImageSprite.width, bkgnImageSprite.height); 
     trace("selection BitmapData", maskBitmapData.width, maskBitmapData.height); 
     trace("scaled selection", scaledBitmapData.width, scaledBitmapData.height); 
     trace("ratio", ratio); 

     var scaledBitmap:Bitmap = new Bitmap(scaledBitmapData); 

     var scaleW:Number = sourceBD.width/scaledBitmapData.width; 
     var scaleH:Number = sourceBD.height/scaledBitmapData.height; 

     var scaleMatrix:Matrix = new Matrix(); 
     scaleMatrix.scale(ratio,ratio); 

     var sRect:Rectangle = maskSprite.getBounds(bkgnImageSprite); 
     var sR:Rectangle = sRect.clone(); 
     var ss:Sprite = new Sprite(); 
     ss.graphics.lineStyle(8, 0x0000FF); 
     //ss.graphics.beginFill(0x000000, 1); 
     ss.graphics.drawRect(sRect.x, sRect.y, sRect.width, sRect.height); 
     //ss.graphics.endFill(); 
     this.addChild(ss); 

     trace("before " + sRect); 

     w = uint(sRect.width * scaleW); 
     h = uint(sRect.height * scaleH); 

     sRect.inflate(maskBitmapData.width * ratio, maskBitmapData.height * ratio); 
     sRect.offset(maskBitmapData.width * ratio, maskBitmapData.height * ratio); 

     trace(sRect); 

     scaledBitmapData.copyPixels(sourceBD, sRect, new Point()); 
     addChild(scaledBitmap); 
     scaledBitmap.x = offsetPt.x; 
     scaledBitmap.y = offsetPt.y; 
    } 
} 

回答

0

謝謝!

public function onExpandCallout(event:MouseEvent):void{ 
// TODO: build this on startup or only on click? Speed vs memory 

if (calloutState == true) return; 

if (maskBitmapData.getPixel32(event.localX, event.localY) != 0){ 

    calloutState = true; 

    //create bitmap from source using scaled selection rect 
    var ratio:Number = (sourceBMD.width /targetRect.width); 
    var sRect:Rectangle = hotSpotSprite.getBounds(bkgnImageSprite); 
    var destRect:Rectangle = new Rectangle(sRect.x * ratio, sRect.y * ratio, sRect.width * ratio, sRect.height * ratio); 
    calloutBitmapData = new BitmapData(destRect.width, destRect.height, true, 0xFFFFFFFF); 
    calloutBitmap = new Bitmap(calloutBitmapData); 

    //-- scale alpha mask 
    var scaledMaskBitmapData:BitmapData = new BitmapData(destRect.width, destRect.height, true, 0x00000000); 
    var maskScale:Number = scaledMaskBitmapData.width/maskBitmapData.width; 
    var mMatrix:Matrix = new Matrix(maskScale, 0, 0, maskScale); 
    scaledMaskBitmapData.draw(maskBitmapData,mMatrix,null,null,null, false); 

    // copy source with scaled alpha 
    calloutBitmapData.copyPixels(sourceBMD, destRect, new Point(), scaledMaskBitmapData, new Point()); 

    scaledMaskBitmapData = null; 

    // apply filter to bitmap 
    var myDropShadowFilter:DropShadowFilter = new DropShadowFilter(); 
    myDropShadowFilter.distance = 12; 
    myDropShadowFilter.alpha = .3 
    myDropShadowFilter.strength = 1; 
    myDropShadowFilter.blurX = 8; 
    myDropShadowFilter.blurY = 8; 
    calloutBitmap.filters = [myDropShadowFilter]; 

    //place on screen 
    calloutSprite = new Sprite(); 
    calloutSprite.addChild(calloutBitmap) 
    calloutSprite.x = offsetPt.x; 
    calloutSprite.y = offsetPt.y; 
    // ADD TO PARENT DisplayContainer 
    calloutLayer.addChild(calloutSprite); 

    //    calloutSprite.scaleX = 2; 
    //    calloutSprite.scaleY = 2; 
    calloutSprite.doubleClickEnabled = true; 

    calloutSprite.addEventListener(MouseEvent.DOUBLE_CLICK, onCollapseCallout); 
    calloutSprite.addEventListener(MouseEvent.MOUSE_DOWN, onStartDrag); 
    calloutSprite.addEventListener(MouseEvent.MOUSE_UP, onStopDrag);  
} 

}

相關問題