Flash 调整BitmapData对象大小的最佳方法是什么?

Flash 调整BitmapData对象大小的最佳方法是什么?,flash,actionscript-3,image,resize,bitmapdata,Flash,Actionscript 3,Image,Resize,Bitmapdata,假设我有一个600x600的位图数据,我想把它缩小到100x100。而不用自己编写代码。我的方法是创建一个所需大小的新BitmapData对象,然后使用bitmap.draw方法将大对象复制到小对象。bitmap.draw方法还接受一个矩阵参数,您可以在复制时使用该参数进行缩放。此操作: var scale:Number = 1.0/6.0; var matrix:Matrix = new Matrix(); matrix.scale(scale, scale); var smallBMD:B

假设我有一个600x600的位图数据,我想把它缩小到100x100。

而不用自己编写代码。我的方法是创建一个所需大小的新BitmapData对象,然后使用bitmap.draw方法将大对象复制到小对象。bitmap.draw方法还接受一个矩阵参数,您可以在复制时使用该参数进行缩放。

此操作:

var scale:Number = 1.0/6.0;
var matrix:Matrix = new Matrix();
matrix.scale(scale, scale);

var smallBMD:BitmapData = new BitmapData(bigBMD.width * scale, bigBMD.height * scale, true, 0x000000);
smallBMD.draw(bigBMD, matrix, null, null, null, true);

var bitmap:Bitmap = new Bitmap(smallBMD, PixelSnapping.NEVER, true);
是DisplayObject和BitmapData的接口


from:

使用矩阵缩放的问题是它不会进行任何抗锯齿或平滑处理-如果您确定只会进行缩小,这可能是可以的,但更通用的方法是使用Image类进行调整大小。在AS3中,这永远不会被添加到显示列表中,因此只能在“屏幕外”使用。类似这样的内容(位图数据为“sourceBitmapData”):

然后,您可以使用“scaledBitmapData”代替“sourceBitmapData”来执行任何操作。

平滑:

function BitmapScaled(source:IBitmapDrawable, thumbWidth:Number, thumbHeight:Number):BitmapData {
    var mat:Matrix = new Matrix();
    mat.scale(thumbWidth/source.width, thumbHeight/source.height);
    var bmpd_draw:BitmapData = new BitmapData(thumbWidth, thumbHeight, false);
    bmpd_draw.draw(source, mat, null, null, null, true);
    return bmpd_draw;
}

draw方法接受DisplayObject和BitmapData的接口

这是上面的一个变体,增加了对缩放、拉伸和信箱的支持。它可能不提供剪辑支持

var newSizeBitmapData:BitmapData = resizeBitmapData(myBitmapData, newWidth, newHeight);


/**
 * Resize display object or bitmap data to a new size
 **/
public static function resizeBitmapData(bitmapDrawable:IBitmapDrawable, width:Number, height:Number, scaleMode:String="none", 
                                      smooth:Boolean = true, transparent:Boolean = true, fillColor:Number = 0x00000000):BitmapData {
    var sizedBitmapData:BitmapData;
    var matrix:Matrix;
    matrix = getSizeByScaleMode(width, height, Object(bitmapDrawable).width, Object(bitmapDrawable).height, scaleMode);
    sizedBitmapData = new BitmapData(width, height, transparent, fillColor);
    sizedBitmapData.draw(bitmapDrawable, matrix, null, null, null, smooth);

    return sizedBitmapData;
}

// Get correct scale. Inspired from code in Apache Flex (license Apache 2.0) 
public static function getSizeByScaleMode(maxWidth:int, maxHeight:int, 
                                          width:int, height:int, 
                                          scaleMode:String="letterbox",
                                          dpi:Number=NaN):Matrix {

    var aspectRatio:String = (maxWidth < maxHeight) ? "portrait" : "landscape";
    var orientation:String = aspectRatio;

    var matrix:Matrix = new Matrix();

    var scaleX:Number = 1;
    var scaleY:Number = 1;

    switch(scaleMode) {
        case "zoom":
            scaleX = Math.max( maxWidth / width, maxHeight / height);
            scaleY = scaleX;
            break;

        case "letterbox":
            scaleX = Math.min( maxWidth / width, maxHeight / height);
            scaleY = scaleX;
            break;

        case "stretch":
            scaleX = maxWidth / width;
            scaleY = maxHeight / height;
            break;
    }

    if (scaleX != 1 || scaleY != 0) {
        width *= scaleX;
        height *= scaleY;
        matrix.scale(scaleX, scaleY);
    }

    matrix.translate(-width / 2, -height / 2);

    matrix.translate(maxWidth / 2, maxHeight / 2);

    return matrix;
}
var newsizebittmapdata:BitmapData=resizeBitmapData(myBitmapData,newWidth,newHeight);
/**
*将显示对象或位图数据调整为新大小
**/
公共静态函数resizeBitmapData(bitmapDrawable:IbitUpdate-Drawable,宽度:数字,高度:数字,scaleMode:String=“无”,
平滑:布尔值=true,透明:布尔值=true,fillColor:Number=0x00000000):位图数据{
变量sizedBitmapData:BitmapData;
var矩阵:矩阵;
矩阵=getSizeByScaleMode(宽度、高度、对象(可位图绘制)、宽度、对象(可位图绘制)、高度、缩放模式);
sizedBitmapData=新的BitmapData(宽度、高度、透明、填充颜色);
sizedBitmapData.draw(bitmapDrawable,矩阵,null,null,null,平滑);
返回sizedBitmapData;
}
//获得正确的刻度。灵感来自ApacheFlex(许可Apache2.0)中的代码
公共静态函数getSizeByScaleMode(maxWidth:int,maxHeight:int,
宽度:int,高度:int,
scaleMode:String=“信箱”,
dpi:Number=NaN):矩阵{
变量aspectRatio:String=(最大宽度<最大高度)?“纵向”:“横向”;
变量方向:String=aspectRatio;
var矩阵:矩阵=新矩阵();
var scaleX:Number=1;
变量scaleY:Number=1;
开关(scaleMode){
案例“缩放”:
scaleX=数学最大值(maxWidth/width,maxHeight/height);
scaleY=scaleX;
打破
“信箱”一案:
scaleX=Math.min(最大宽度/宽度,最大高度/高度);
scaleY=scaleX;
打破
案例“拉伸”:
scaleX=最大宽度/宽度;
scaleY=最大高度/高度;
打破
}
如果(scaleX!=1 | | scaleY!=0){
宽度*=scaleX;
高度*=scaleY;
矩阵.scale(scaleX,scaleY);
}
矩阵。平移(-宽度/2,-高度/2);
矩阵转换(最大宽度/2,最大高度/2);
收益矩阵;
}

实际上不是我想要的,因为我从bitmapdata开始,而不是从显示对象开始。谢谢你!通过使用BitmapData替换DisplayObject可以轻松修复;-)BitmapData.draw方法接受IBitmapData。IBitmapDrawable是DisplayObject和BitmapDataTable都使用的接口。该图像类来自何处?未将图像类列为AS3库的一部分。由于没有对图像类的引用而被否决。它是或var scale:uint-->var scale:Number,plz。此
0x000000
非常重要,没有它,thoose就没有透明度,使用StageXL dart时,默认矩阵具有构造函数矩阵。fromIdentity()。其他所有操作都与本例中相同。
function BitmapScaled(source:IBitmapDrawable, thumbWidth:Number, thumbHeight:Number):BitmapData {
    var mat:Matrix = new Matrix();
    mat.scale(thumbWidth/source.width, thumbHeight/source.height);
    var bmpd_draw:BitmapData = new BitmapData(thumbWidth, thumbHeight, false);
    bmpd_draw.draw(source, mat, null, null, null, true);
    return bmpd_draw;
}
var newSizeBitmapData:BitmapData = resizeBitmapData(myBitmapData, newWidth, newHeight);


/**
 * Resize display object or bitmap data to a new size
 **/
public static function resizeBitmapData(bitmapDrawable:IBitmapDrawable, width:Number, height:Number, scaleMode:String="none", 
                                      smooth:Boolean = true, transparent:Boolean = true, fillColor:Number = 0x00000000):BitmapData {
    var sizedBitmapData:BitmapData;
    var matrix:Matrix;
    matrix = getSizeByScaleMode(width, height, Object(bitmapDrawable).width, Object(bitmapDrawable).height, scaleMode);
    sizedBitmapData = new BitmapData(width, height, transparent, fillColor);
    sizedBitmapData.draw(bitmapDrawable, matrix, null, null, null, smooth);

    return sizedBitmapData;
}

// Get correct scale. Inspired from code in Apache Flex (license Apache 2.0) 
public static function getSizeByScaleMode(maxWidth:int, maxHeight:int, 
                                          width:int, height:int, 
                                          scaleMode:String="letterbox",
                                          dpi:Number=NaN):Matrix {

    var aspectRatio:String = (maxWidth < maxHeight) ? "portrait" : "landscape";
    var orientation:String = aspectRatio;

    var matrix:Matrix = new Matrix();

    var scaleX:Number = 1;
    var scaleY:Number = 1;

    switch(scaleMode) {
        case "zoom":
            scaleX = Math.max( maxWidth / width, maxHeight / height);
            scaleY = scaleX;
            break;

        case "letterbox":
            scaleX = Math.min( maxWidth / width, maxHeight / height);
            scaleY = scaleX;
            break;

        case "stretch":
            scaleX = maxWidth / width;
            scaleY = maxHeight / height;
            break;
    }

    if (scaleX != 1 || scaleY != 0) {
        width *= scaleX;
        height *= scaleY;
        matrix.scale(scaleX, scaleY);
    }

    matrix.translate(-width / 2, -height / 2);

    matrix.translate(maxWidth / 2, maxHeight / 2);

    return matrix;
}