Add Image encode on browser
This commit is contained in:
@@ -347,8 +347,6 @@ class Image {
|
||||
|
||||
public function encode (format:String = "png", quality:Int = 90):ByteArray {
|
||||
|
||||
#if (!html5 && !flash)
|
||||
|
||||
switch (format) {
|
||||
|
||||
case "png":
|
||||
@@ -367,8 +365,6 @@ class Image {
|
||||
|
||||
}
|
||||
|
||||
#end
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
@@ -2,11 +2,18 @@ package lime.graphics.format;
|
||||
|
||||
|
||||
import haxe.io.Bytes;
|
||||
import lime.graphics.utils.ImageCanvasUtil;
|
||||
import lime.graphics.Image;
|
||||
import lime.graphics.ImageBuffer;
|
||||
import lime.system.CFFI;
|
||||
import lime.utils.ByteArray;
|
||||
|
||||
#if (js && html5)
|
||||
import js.Browser;
|
||||
#end
|
||||
|
||||
@:access(lime.graphics.ImageBuffer)
|
||||
|
||||
#if !macro
|
||||
@:build(lime.system.CFFI.build())
|
||||
#end
|
||||
@@ -77,6 +84,26 @@ class JPEG {
|
||||
var bytes = @:privateAccess new Bytes (data.length, data.b);
|
||||
return ByteArray.fromBytes (bytes);
|
||||
|
||||
#elseif html5
|
||||
|
||||
ImageCanvasUtil.sync (image, false);
|
||||
|
||||
if (image.buffer.__srcCanvas != null) {
|
||||
|
||||
var data = image.buffer.__srcCanvas.toDataURL ("image/jpeg", quality / 100);
|
||||
var buffer = Browser.window.atob (data.split (";base64,")[1]);
|
||||
var byteArray = new ByteArray (buffer.length);
|
||||
|
||||
for (i in 0...buffer.length) {
|
||||
|
||||
byteArray.byteView[i] = buffer.charCodeAt (i);
|
||||
|
||||
}
|
||||
|
||||
return byteArray;
|
||||
|
||||
}
|
||||
|
||||
#end
|
||||
|
||||
return null;
|
||||
|
||||
@@ -6,6 +6,10 @@ import lime.graphics.Image;
|
||||
import lime.system.CFFI;
|
||||
import lime.utils.ByteArray;
|
||||
|
||||
#if (js && html5)
|
||||
import js.Browser;
|
||||
#end
|
||||
|
||||
#if format
|
||||
import format.png.Data;
|
||||
import format.png.Writer;
|
||||
@@ -14,6 +18,8 @@ import haxe.io.Bytes;
|
||||
import haxe.io.BytesOutput;
|
||||
#end
|
||||
|
||||
@:access(lime.graphics.ImageBuffer)
|
||||
|
||||
#if !macro
|
||||
@:build(lime.system.CFFI.build())
|
||||
#end
|
||||
@@ -133,6 +139,26 @@ class PNG {
|
||||
|
||||
}
|
||||
|
||||
#elseif html5
|
||||
|
||||
ImageCanvasUtil.sync (image, false);
|
||||
|
||||
if (image.buffer.__srcCanvas != null) {
|
||||
|
||||
var data = image.buffer.__srcCanvas.toDataURL ("image/png");
|
||||
var buffer = Browser.window.atob (data.split (";base64,")[1]);
|
||||
var byteArray = new ByteArray (buffer.length);
|
||||
|
||||
for (i in 0...buffer.length) {
|
||||
|
||||
byteArray.byteView[i] = buffer.charCodeAt (i);
|
||||
|
||||
}
|
||||
|
||||
return byteArray;
|
||||
|
||||
}
|
||||
|
||||
#end
|
||||
#end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user