-
Notifications
You must be signed in to change notification settings - Fork 2
/
ImageSaver.hx
66 lines (44 loc) · 1.45 KB
/
ImageSaver.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package;
#if cpp
import cpp.Lib;
import haxe.io.Bytes;
#elseif neko
import neko.Lib;
#end
#if (android && openfl)
import openfl.utils.JNI;
#end
import flash.utils.ByteArray;
import flash.display.BitmapData;
import flash.geom.Rectangle;
class ImageSaver {
public static function saveImage(bmd:BitmapData):Void {
#if ios
var ba:ByteArray = bmd.getPixels(new Rectangle(0, 0, bmd.width, bmd.height));
cpp_call_save_image(ba.getData(), Std.int(bmd.width), Std.int(bmd.height));
#elseif android
if (jni_call_save_image == null) jni_call_save_image = JNI.createStaticMethod ("org.haxe.extension.ImageSaver", "saveImage", "(Ljava/lang/String;)V");
jni_call_save_image(getB64PngData(bmd));
#end
}
#if ios
private static var cpp_call_save_image = Lib.load ("imagesaver", "imagesaver_save_image", 3);
#end
#if (android && openfl)
private static inline var BASE_64_ENCODINGS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
private static inline var BASE_64_PADDING = "=";
static inline function getB64PngData(bmp:BitmapData):String{
var b:ByteArray = bmp.encode("png");
var base64:String = new haxe.crypto.BaseCode(Bytes.ofString(BASE_64_ENCODINGS)).encodeBytes(b).toString();
var remainder = base64.length % 4;
if (remainder > 1) {
base64 += BASE_64_PADDING;
}
if (remainder == 2) {
base64 += BASE_64_PADDING;
}
return base64;
}
private static var jni_call_save_image:Dynamic;
#end
}