如何以原始尺寸保存图像?

时间:2011-05-07 21:33:52

标签: flex

在我的Flex应用程序中,我将图像加载到Image控件中。加载图像后,我可以应用滤色器,如颜色变换或灰度等,然后我想保存图像与应用过滤器。我在使用过滤器保存图像时遇到问题。使用以下代码,Image将在没有过滤器的情况下保存。但是,如果我使用

  

finalImagedata =   ImageSnapshot.captureBitmapData(MYIMAGE);

然后使用过滤器保存图像,但使用图像控件的尺寸。我想用原始尺寸保存图像。要清楚,如果图像是1000px X 1000px并在300px X 300px图像控件中显示,则保存时我仍然希望使用1000px X 1000px dimesnion保存图像。怎么做?我也尝试直接从像下面的图像控件内容获取BitmapData,但这似乎不起作用。有帮助吗?

  

finalImagedata =   位图(myImage.content).bitmapData;

            var filtersArray:Array = new Array;
            private var finalImagedata:BitmapData;

            private function loadImage(url:String):void
            {
                var request:URLRequest = new URLRequest(url);

                var imageLoader:Loader = new Loader();
                imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, image_completeHanlder);
                imageLoader.load(request);
            }
            private function image_completeHanlder(event:Event):void
            {
                var bmd:BitmapData = Bitmap(event.currentTarget.content).bitmapData;
                var bmpMy:Bitmap = new Bitmap(bmd);
                myImage.data=bmpMy;

            }
            protected function button1_clickHandler(event:MouseEvent):void
            {
                loadImage("http://localhost/main/image.jpg"); 
            }

            protected function prepareFinalImage():void
            {
                finalImagedata = new BitmapData(myImage.content.width, myImage.content.height, true);
                finalImagedata.draw (myImage.content);
            }
            protected function btnSave_clickHandler(event:MouseEvent):void
            {
                prepareFinalImage();

                var encoder:Object = new Object();
                var defaultName:String;

                        var je:*;
                        je = new JPGEncoder(100);
                        encoder.encode = je.encode;
                        defaultName = "myImage.jpg";


                var imageBytes:ByteArray = encoder.encode(finalImagedata);
                var fr:FileReference = new FileReference();
                fr.save(imageBytes,defaultName);
            }


            protected function btnGrayScale_clickHandler(event:MouseEvent):void
            {
                var red:Number = 0.3086; // luminance contrast value for red
                var green:Number = 0.694; // luminance contrast value for green
                var blue:Number = 0.0820; // luminance contrast value for blue
                var cmf1:ColorMatrixFilter = new ColorMatrixFilter([red, green, blue, 0, 0, red, green, blue, 0, 0, red, green, blue, 0, 0, 0, 0, 0, 1, 0]);
                filtersArray.push(cmf1);
                myImage.filters = filtersArray;
            }

1 个答案:

答案 0 :(得分:1)

您应该将从Loader检索到的bitmapdata存储为类成员:

private var bmd:BitmapData;
[...]
private function image_completeHanlder(event:Event):void
{
    bmd = Bitmap(event.currentTarget.content).bitmapData;
    var bmpMy:Bitmap = new Bitmap(bmd);
    myImage.data=bmpMy;
}

并且您的prepareFinalImage方法应该依赖于加载的bitmapdata:

protected function prepareFinalImage():void
{
    finalImagedata = new BitmapData(bmd.width, bmd.height, true);
    if (myImage.filters.length > 0)
        finalImagedata.applyFilter(bmd, 
            new Rectangle(0, 0, bmd.width, bmd.height), 
            new Point(0,0), myImage.filters[0] as BitmapFilter);
    if (myImage.filters.length > 1)
        for (var i:int = 1; i < myImage.filters.length; i++)
        {
            finalImagedata.applyFilter(finalImagedata, 
                new Rectangle(0, 0, bmd.width, bmd.height), 
                new Point(0,0), myImage.filters[i] as BitmapFilter);
        }
}

这样您就可以在BitmapData上应用过滤器,而不是缩放DisplayObject上的过滤器。