The params don’t seem to match up with the actual function. I need to encode a png but what do I use for the compressor parameter ? Any alternative will also help .
Taken from the github
/**
* @param format The encoding format, either "png" or "jpg".
* @param quality The encoding quality, when encoding with the JPG format.
* @return A ByteArray in the specified encoding format
*/
public function encode (rect:Rectangle, compressor:Dynamic, byteArray:ByteArray = null):ByteArray
{
openfl.Lib.notImplemented ("BitmapData.encode");
return null;
}
I just added it. Now you should be able to (from the source repository or in the next release) use this as expected. For now, the rectangle argument is not supported, but for a full image this should work
var bitmapData = Assets.getBitmapData ("assets/openfl.png");
var png = bitmapData.encode (bitmapData.rect, new PNGEncoderOptions ());
trace (png.length);
var jpeg = bitmapData.encode (bitmapData.rect, new JPEGEncoderOptions (90));
trace (jpeg.length);
The “fast” PNG encoder option is also ignored at the moment (I’d like to think it’s always fast ;)) but the “quality” JPEG option should be supported properly on all targets. In Flash Player, it should use the built-in version added in Flash Player 11.3, and on HTML5 I believe encoding may require that you use <haxelib name="format" /> and might be slow
Hmm, maybe there’s just a little bug. I believe that this should work (in principal) in HTML5, and no, it is in OpenFL 3 without -Dlegacy, just try a desktop build and does it work?
So encoding always returns null on html…or maybe I’m just doing something wrong Anyway I have found a way to “extract” HTMLImageElement from bdm which can be converted to base64 and this suits me fine but I’d prefer to use encode function.
i am looking for the solution for this problem. I am using HTML5, and I get null from the bitmapdata.encode() method. I tried using both “png” and new PNGEncoderOptions(), but the result was the same.
I cannot confirm that this is working on the HTML5 target unfortunately. Some environment info:
OpenFL: 3.5.3-LQCE4x
Lime:2.8.3
I was using a Flixel-based workaround, and switched to pure OpenFL this morning. I can confirm that the BitmapData is properly converting to Bitmap (it can be rendered on the screen.
It does not list the image property which is a public property on BitmapData
It says the encode function is not avaialble on HTML5 targets, but I think your above message says that it should be.
function encode(rect:Rectangle, compressor:Object, byteArray:ByteArray):ByteArray
Encodes the current image as a JPG or PNG format ByteArray.
This method is not available to the HTML5 and Flash targets.
@param format The encoding format, either “png” or “jpg”. @param quality The encoding quality, when encoding with the JPG format. @return A ByteArray in the specified encoding format