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

发布于 2024-07-22 17:52:56 字数 51 浏览 3 评论 0原文

假设我有一个 600x600 的 BitmapData,我想将其缩小到 100x100。

Say I have a BitmapData of 600x600 and I want to scale it down to 100x100.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

情未る 2024-07-29 17:52:56

这有效:

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);

This works:

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);
北风几吹夏 2024-07-29 17:52:56
public function drawScaled(obj:IBitmapDrawable, thumbWidth:Number, thumbHeight:Number):Bitmap {
    var m:Matrix = new Matrix();
    m.scale(WIDTH / obj.width, HEIGHT / obj.height);
    var bmp:BitmapData = new BitmapData(thumbWidth, thumbHeight, false);
    bmp.draw(obj, m);
    return new Bitmap(bmp);
}

IBitmapDrawable 是 DisplayObject 的接口和位图数据。

来自: http://www .nightdrops.com/2009/02/quick-reference-drawing-a-scaled-object-in-actionscript/

public function drawScaled(obj:IBitmapDrawable, thumbWidth:Number, thumbHeight:Number):Bitmap {
    var m:Matrix = new Matrix();
    m.scale(WIDTH / obj.width, HEIGHT / obj.height);
    var bmp:BitmapData = new BitmapData(thumbWidth, thumbHeight, false);
    bmp.draw(obj, m);
    return new Bitmap(bmp);
}

IBitmapDrawable is an interface for DisplayObject and BitmapData.

from: http://www.nightdrops.com/2009/02/quick-reference-drawing-a-scaled-object-in-actionscript/

好菇凉咱不稀罕他 2024-07-29 17:52:56

平滑处理:

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 方法接受 IBitmapDrawable 这是 DisplayObject 和 BitmapData 的接口。

With smoothing:

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;
}

The draw method accepts IBitmapDrawable which is an interface for DisplayObject and BitmapData.

无人接听 2024-07-29 17:52:56

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

Without writing the code myself. The way i would approach this would be to create a new BitmapData object of the desired size and then use the bitmap.draw method to copy the large one to the small one. The bitmap.draw method also accepts a matrix argument that you can use to scale when you copy.

怂人 2024-07-29 17:52:56

使用矩阵缩放的问题在于它不执行任何抗锯齿或平滑操作 - 如果您确定只会缩小尺寸,那么这可能没问题,但更通用的方法是使用 Image 类来调整大小。 在 AS3 中,它永远不会被添加到显示列表中,因此只会在“屏幕外”使用。 像这样的东西(您的位图数据为“sourceBitmapData”):

var image:Image = new Image();
image.load(new Bitmap(sourceBitmapData, PixelSnapping.NEVER, true));

var scale:uint = 100/600; // this is from your example of 600x600 => 100x100
var scaledWidth:uint = sourceBitmapData.width * scale;
var scaledHeight:uint = sourceBitmapData.height * scale;

image.content.width = scaledWidth;
image.content.height = scaledHeight;

var scaledBitmapData:BitmapData = new BitmapData(scaledWidth, scaledHeight);
scaledBitmapData.draw(image.content); 

image = null;

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

The problem with using matrix scaling is that it doesn't do any antialiasing or smoothing - this is probably OK if you're sure you will only ever been downscaling, but a more general method would use the Image class to do the resizing. In AS3 this would never be added to the display list, so would just be used "offscreen". Something like this (with your bitmap data as "sourceBitmapData"):

var image:Image = new Image();
image.load(new Bitmap(sourceBitmapData, PixelSnapping.NEVER, true));

var scale:uint = 100/600; // this is from your example of 600x600 => 100x100
var scaledWidth:uint = sourceBitmapData.width * scale;
var scaledHeight:uint = sourceBitmapData.height * scale;

image.content.width = scaledWidth;
image.content.height = scaledHeight;

var scaledBitmapData:BitmapData = new BitmapData(scaledWidth, scaledHeight);
scaledBitmapData.draw(image.content); 

image = null;

You can then use "scaledBitmapData" in place of "sourceBitmapData" to do whatever with.

恰似旧人归 2024-07-29 17:52:56

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

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;
}

Here is a variation of the above that adds support for zoom, stretch and letterbox. It may not provide the clipping support.

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文