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
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 ());
var jpeg = bitmapData.encode (bitmapData.rect, new JPEGEncoderOptions (90));
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
I’m getting error on html5 (Cannot read property ‘length’ of null). Adding format doesn’t fix this. Is there other way to encode bitmapdata on html5?
edit: it seems that this was added only in “v2” package which is not available in html as far as I know
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?
This works fine on desktop (newest openfl/lime, no -Dlegacy)
var bdm:BitmapData = new BitmapData(200, 100);
trace(bdm.encode(bdm.rect, new PNGEncoderOptions ()));
encode function in BitmapData does something like that
return byteArray = __image.encode ("png");
and encode function in lime’s image does that:
#if (!html5 && !flash)
stuff that actually encodes :)
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.
Oh, have you tried adding
<haxelib name="format" />?
Yeah, but it doesn’t change anything.
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.
What version of OpenFL and Lime are you using? I believe this was just recently added, without needing format, either
Yeah I was using earlier version. But I still get null ByteArray as a result, even after changing the changes in this 3 files.
flixel-tools: git [dev:C:\HaxeToolkit/flixel-tools/git]
flixel: 3.3.11 git [dev:C:\HaxeToolkit/flixel/git]
lime: 1.0.1 [2.6.2]
openfl-bitfive: 2.0.1 [3.3.3]
openfl: 2.0.1 [3.3.3]
Could you try the latest?
I tried the latest, but
var bitmap:Bitmap = FlxScreenGrab.grab(new Rectangle(0, 0, 798, 438), false, true);
var byteArray:ByteArray = bitmap.bitmapData.encode(new Rectangle(0, 0, 798, 438), PNGEncoderOptions);
Resulting byteArray is null.
new PNGEncoderOptions () instead of passing the class name
I cannot confirm that this is working on the HTML5 target unfortunately. Some environment info:
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.
bitmapData.encode( bitmapData.rect, 'png' )
This returns null.
bitmapData.encode( bitmapData.rect, new PNGEncoderOptions () )
This throws an error
"bytes is null" at
We may want to update these docs when/if this is resolved: http://www.openfl.org/documentation/api/openfl/display/BitmapData.html
Issues with the docs:
- 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
@CharlesAnifowose Hi, it’s available and it’s working on html5 target.
On lime 2.9.1 it doesn’t work.
Got a pull request with the fix ready: https://github.com/openfl/lime/pull/732
There was a misplaced #end macro tag that prevent the html5 conversion from working.