Problem with JPGEncoder and FileReference

2

I'm trying to save the contents of a MovieClip to an image, however when trying to save the image the following error occurs:

image with white space http://www.panrotas.com.br/v2/test.jpg

A blank space appears on all images saved using FileReference in conjunction with JPGEncoder .

I think the problem is related to JPGEncoder , although I'm not sure.

Here is the function I use to save the images:

private function fl_Salvar(event:MouseEvent)
        {
            try
            {
                var src:BitmapData = new BitmapData(imageViewer.width,imageViewer.height);


                var mtx:Matrix = DisplayUtils.fitIntoRect(imageViewer.mcImage.getChildAt(0),rect,true,Alignment.MIDDLE,false);

                src.draw(imageViewer,mtx,null,null,null,true);

                var jpgEncoder:JPGEncoder = new JPGEncoder(85);
                var imgStream:ByteArray = null; 
                imgStream = jpgEncoder.encode(src);

                var file:FileReference = new FileReference();
                file.addEventListener( IOErrorEvent.IO_ERROR, ioErrorHandler );
                file.save( imgStream, "TESTE.jpg");
            }
            catch (ioe:IllegalOperationError)
            {
                trace("Operação Ilegal.");
            }
            catch (ae:ArgumentError)
            {
                trace("Argumento Inválido.");
            }
            catch (me:MemoryError)
            {
                trace("Memória Insuficiente.");
            }
            catch (error:Error)
            {
                trace("Erro ao tentar salvar imagem : "
                              + " . Erro : " + error);
            }
        }

        private function ioErrorHandler( event:IOErrorEvent ):void
        {
            trace("Handler de erro I/O: " + event);
        }

I wonder if anyone knows what's causing this blank area in the images?

    
asked by anonymous 02.06.2014 / 15:43

1 answer

4

Well, I noticed that you're using this library , correct?

What I understand is that it cuts a DisplayObject object by changing its properties and returning Matrix of this clipping. I did some testing here and tried to emulate your mistake. What I got closer was where I changed the size of the Dimension Rectangle to a number MINOR that my DisplayObject ...

Below is the code with error , assuming my MovieClip (DisplayObject ) is 300x300px:

var rect:Rectangle = new Rectangle(0, 0, 100, 100);
var matrix:Matrix = DisplayUtils.fitIntoRect(movieclip, rect, true, Alignment.MIDDLE, false);
var bitData:BitmapData = new BitmapData(movieclip.width, movieclip.height);
bitData.draw(movieclip, matrix);

Of course, the DisplayUtils class is decreasing the size of the movieclip (to 100x100), but maintaining the proportion of the final object, which is 300x300px.

When you save this image, it will be saved as 300x300px, but with both vertical and horizontal blanks.

Now, try to perform this operation by putting the rectangle's value exactly the same as the original size of your MovieClip, I believe it will be saved correctly!

I made this code below for testing purposes only, please note that by increasing and decreasing the size of your rectangle, your final image will be either blank or missing, but when the size of the rectangle is the same as movieclip, it will work normally.

var rect:Rectangle = new Rectangle(0, 0, 100, 100);
var matrix:Matrix = DisplayUtils.fitIntoRect(imagem_movieclip, rect, true, Alignment.MIDDLE, false);

function mcToImage(mc:MovieClip):void {

    var bitData:BitmapData = new BitmapData(mc.width, mc.height);
    bitData.draw(mc, matrix);

    var jpge:JPGEncoder = new JPGEncoder(100);

    var ba:ByteArray = jpge.encode(bitData);

    var fileref:FileReference = new FileReference();
    fileref.save(ba, "teste.jpg");

}

mcToImage(imagem_movieclip);
    
03.06.2014 / 22:06