Flex,按顺序对两个位图进行jpegencoding时出现问题

时间:2010-09-17 20:09:03

标签: flex flash

我正在创建这个“绘图应用程序”,用户可以点击“预览”,它将拍摄他们制作的内容,绘制位图,然后我想将该位图分割成左右图像。我创建第一个位图,将其编码为jpeg,然后使用它来使用copypixels左右切割。然后我将图像与一个画布中间的空间一起改造,然后画出画布。那里的一切都很好。

当我对新位图进行编码,然后将其保存时,保存的图像为空白。到目前为止,我已经测试了一切,一切正常。我看到屏幕上的图像,我可以保存第一个位图很好..但第二个总是空白。这是一些示例代码,可能有人可以帮助我。

_mainBmd = new BitmapData(_jacketWidth, _jacketHeight);
_mainBmd.draw(_imageHolder);
startEncode(_mainBmd);


private function startEncode(imageBitmapData:BitmapData):void 
        {
            var encoder:JPEGAsyncEncoder = new JPEGAsyncEncoder(100);
            encoder.PixelsPerIteration = 150;
            encoder.addEventListener(JPEGAsyncCompleteEvent.JPEGASYNC_COMPLETE, encodeDone);

            encoder.encode(imageBitmapData);


        }

private function encodeDone(event:JPEGAsyncCompleteEvent):void
        {

_leftBmd = new BitmapData(sideWidth, sideHeight);
            var lRect:Rectangle = new Rectangle(0,0, sideWidth, sideHeight);
            var lPoint:Point = new Point(0,0);
            _leftBmd.copyPixels(_mainBmd, lRect, lPoint);


            _rightBmd = new BitmapData(sideWidth, sideHeight);
            var bWidth:Number = 200;
            var sWidth:Number = 111;
            var rRectWidth:Number = (bWidth/2 + sWidth) *  Constants.print_dpi;
            var rRect:Rectangle = new Rectangle(rRectWidth, 0, sideWidth, sideHeight);
            var rPoint:Point = new Point(0, 0);
            _rightBmd.copyPixels(_mainBmd, rRect, rPoint);

var lbm:Bitmap = new Bitmap(_leftBmd);
            var rbm:Bitmap = new Bitmap(_rightBmd);

            //now combine the two images into one holder with a space in the middle 


            //left Image
            var l_Image:Image = new Image();
            l_Image.source = lbm;

            //right image
            var r_Image:Image = new Image();
            r_Image.source = rbm;

var newRender:Canvas = new Canvas();
            newRender.clipContent = false;
            newRender.minHeight = 0;
            newRender.minWidth = 0;

            newRender.addChild(l_Image);
            r_Image.x = 500;
            newRender.addChild(r_Image);

fcBMD = new BitmapData(renderW, renderH);
            fcBMD.draw(newRender);

startEncode2(fcBMD);


}

private function startEncode2(imageBitmapData:BitmapData):void 
        {
            var encoder:JPEGAsyncEncoder = new JPEGAsyncEncoder(100);
            encoder.PixelsPerIteration = 150;
            encoder.addEventListener(JPEGAsyncCompleteEvent.JPEGASYNC_COMPLETE, encode2Done);

            encoder.encode(imageBitmapData);


        }

private function encode2Done(event:JPEGAsyncCompleteEvent):void
        {
            _data  = event.ImageData;


        }

private function onSaveRenderClick(e:MouseEvent):void  //save button listener
        {
            var fileRef:FileReference = new FileReference();
            fileRef.addEventListener(Event.SELECT, onSaveComplete);
            fileRef.save(_data, 'testImage.jpg');
        }

1 个答案:

答案 0 :(得分:0)

为什么要通过创建Canvas持有者和添加图像的所有循环有特殊原因?

为什么不直接在要编码的最终BitmapData上使用copyPixels:

var backgroundColor:uint = 0xffffff;
fcBMD = new BitmapData(renderW, renderH, false, backgroundColor);

var lPoint:Point = new Point(0,0);
var lRect:Rectangle = new Rectangle(0,0, sideWidth, sideHeight);
fcBMD.copyPixels(_mainBmd, lRect, lPoint);

var rRect:Rectangle = new Rectangle(rRectWidth, 0, sideWidth, sideHeight);
var rPoint:Point = new Point(renderW - sideWidth, 0);
fcBMD.copyPixels(_mainBmd, rRect, rPoint);

startEncode2(fcBMD);