fix require() for json file;fix loadJar() not working; add loadDex(); add image.readPixels(); add util.java

This commit is contained in:
hyb1996
2018-09-08 22:43:37 +08:00
parent 94a17c3118
commit 32548ff9f4
7 changed files with 75 additions and 11 deletions

View File

@@ -78,6 +78,9 @@ module = (typeof module === 'undefined') ? {} : module;
if(builtInModules.indexOf(normalizePath) >= 0 && !files.exists(normalizePath)){
return NativeRequire.require(normalizePath);
}
if(id === "events"){
return events;
}
if(id.startsWith("http://") || id.startsWith("https://")){
return NativeRequire.require(id);
}
@@ -252,6 +255,9 @@ module = (typeof module === 'undefined') ? {} : module;
}
function normalizeName (fileName, ext) {
if(fileName.endsWith('.json')){
return fileName;
}
var extension = ext || '.js';
if (fileName.endsWith(extension)) {
return fileName;

View File

@@ -169,7 +169,23 @@ module.exports = function(__runtime__, scope){
format = format || "png";
quality = quality == undefined ? 100 : quality;
return rtImages.toBytes(img, format, quality);
}
}
images.readPixels = function(path){
var img = images.read(path);
var bitmap = img.getBitmap();
var w = bitmap.getWidth();
var h = bitmap.getHeight();
var pixels = util.java.array("int", w * h);
bitmap.getPixels(pixels, 0, w, 0, 0, w, h);
img.recycle();
return {
data: pixels,
width: w,
height: h
};
}
function getColorDetector(color, algorithm, threshold){
switch(algorithm){

View File

@@ -4,6 +4,32 @@ J.instanceOf = function(obj, clazz){
return java.lang.Class.forName(clazz).isAssignableFrom(obj.getClass());
}
function typeToClass(type) {
if (typeof(type) != 'string') {
return type;
}
var types = {
"int": "Integer",
"long": "Long",
"string": "String",
"double": "Double",
"char": "Character",
"byte": "Byte",
"float": "Float"
};
if (types[type]) {
return Packages["java.lang." + types[type]].TYPE;
}
return Packages[type];
}
function array(type) {
var clazz = typeToClass(type);
var args = arguments;
args[0] = clazz;
return java.lang.reflect.Array.newInstance.apply(null, args);
}
J.array = array;
module.exports = J;

View File

@@ -3,7 +3,7 @@ module.exports = function (runtime, global) {
require("object-observe-lite.min")();
require("array-observe.min")();
var J = require("__java_util__");
var J = util.java;
var ui = {};

View File

@@ -20,6 +20,7 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.
var formatRegExp = /%[sdj%]/g;
exports.java = require("__java_util__");
exports.format = function(f) {
if (!isString(f)) {
var objects = [];