Resize images in Flex using as3

跟風遠走 提交于 2020-01-02 21:51:07

问题


I am loading an image using a Loader. Once loaded, I can get the bitmap data using Bitmap(event.target.loader.content).bitmapData.

However since the images I am loading are quite large (around 2000 x 1600), I would like to reduce the size and create a new smaller bitmap maybe 200 or 300 pixels wide, sort of like a thumbnail.

I think it has to do with creating a new BitmapData with the new size. However I am not able to get that working properly.

Any ideas?


回答1:


Not tested, but this should give you an idea:

public function scaleBitmap(src: BitmapData, ratio: Number): BitmapData
{
    var bmd: BitmapData = new BitmapData(src.width * ratio, src.height * ratio);
    var m: Matrix = new Matrix();   
    m.scale(ratio, ratio);
    bmd.draw(src, m);
    return bmd;
}



回答2:


You can also use this code from the BitmapImage class. I've updated it to support the drawWithQuality method of the BitmapData class. See the StageQuality class for values.

Note: You'll need to set the SWF version to at least 16 to use the quality API. However, the compiler doesn't seem to throw an error either way.

    /**
     * @private
     * Utility function used for higher quality image scaling. Essentially we
     * simply step down our bitmap size by half resulting in a much higher result
     * though taking potentially multiple passes to accomplish.
     * 
     * source spark.primitives.BitmapImage
     */
    protected static function resample(bitmapData:BitmapData, newWidth:uint,
                                       newHeight:uint, quality:String = null):BitmapData
    {

        var finalScale:Number = Math.max(newWidth/bitmapData.width,
            newHeight/bitmapData.height);

        var finalData:BitmapData = bitmapData;

        // ERROR HERE MEANS
        // Property drawWithQuality not found on flash.display.BitmapData and there is no default value.
        // 
        // Solution
        // add -swf-version=16 or greater to your compiler arguments
        // 
        // https://bugbase.adobe.com/index.cfm?event=bug&id=3219149
        if (finalScale > 1)
        {
            finalData = new BitmapData(bitmapData.width * finalScale,
                bitmapData.height * finalScale, true, 0);

            if (quality) {
                finalData.drawWithQuality(bitmapData, new Matrix(finalScale, 0, 0,
                    finalScale), null, null, null, true, quality);
            }
            else {
                finalData.draw(bitmapData, new Matrix(finalScale, 0, 0,
                    finalScale), null, null, null, true);
            }

            return finalData;
        }

        var drop:Number = .5;
        var initialScale:Number = finalScale;

        while (initialScale/drop < 1)
            initialScale /= drop;

        var w:Number = Math.floor(bitmapData.width * initialScale);
        var h:Number = Math.floor(bitmapData.height * initialScale);
        var bd:BitmapData = new BitmapData(w, h, bitmapData.transparent, 0);

        if (quality) {
            bd.drawWithQuality(finalData, new Matrix(initialScale, 0, 0, initialScale),
                null, null, null, true, quality);
        }
        else {
            bd.draw(finalData, new Matrix(initialScale, 0, 0, initialScale),
                null, null, null, true);
        }

        finalData = bd;

        for (var scale:Number = initialScale * drop;
            Math.round(scale * 1000) >= Math.round(finalScale * 1000);
            scale *= drop)
        {
            w = Math.floor(bitmapData.width * scale);
            h = Math.floor(bitmapData.height * scale);
            bd = new BitmapData(w, h, bitmapData.transparent, 0);


            if (quality) {
                bd.drawWithQuality(finalData, new Matrix(drop, 0, 0, drop), null, null, null, true, quality);
            }
            else {
                bd.draw(finalData, new Matrix(drop, 0, 0, drop), null, null, null, true);
            }

            finalData.dispose();
            finalData = bd;
        }

        return finalData;
    }

And to use it:

    var bitmapData:BitmapData = resample(sourceBitmapData, desiredWidth, desiredHeight, StageQuality.HIGH_16X16);


来源:https://stackoverflow.com/questions/6623529/resize-images-in-flex-using-as3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!