新增 images.inRange(), images.findCircles, images.matToImage
修复 调用模块时模块本身的错误提醒不明显的问题
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package org.autojs.autojs;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.usage.UsageStatsManager;
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.drawable.Drawable;
|
||||
|
||||
@@ -1,330 +1,379 @@
|
||||
|
||||
module.exports = function (__runtime__, scope) {
|
||||
const defaultColorThreshold = 4;
|
||||
importPackage(org.opencv.core);
|
||||
const Imgproc = org.opencv.imgproc.Imgproc;
|
||||
module.exports = function (runtime, scope) {
|
||||
function images(){
|
||||
}
|
||||
util.__assignFunctions__(runtime.images, images, ['requestScreenCapture', 'captureScreen', 'read', 'copy', 'load', 'clip', 'pixel'])
|
||||
images.opencvImporter = JavaImporter(
|
||||
org.opencv.core.Point,
|
||||
org.opencv.core.Point3,
|
||||
org.opencv.core.Rect,
|
||||
org.opencv.core.Algorithm,
|
||||
org.opencv.core.Scalar,
|
||||
org.opencv.core.Size,
|
||||
org.opencv.core.Core,
|
||||
org.opencv.core.CvException,
|
||||
org.opencv.core.CvType,
|
||||
org.opencv.core.TermCriteria,
|
||||
org.opencv.core.RotatedRect,
|
||||
org.opencv.core.Range,
|
||||
org.opencv.imgproc.Imgproc,
|
||||
com.stardust.autojs.core.opencv
|
||||
);
|
||||
with (images.opencvImporter) {
|
||||
const defaultColorThreshold = 4;
|
||||
|
||||
var images = {};
|
||||
var colors = Object.create(__runtime__.colors);
|
||||
colors.alpha = function (color) {
|
||||
color = parseColor(color);
|
||||
return color >>> 24;
|
||||
}
|
||||
colors.red = function (color) {
|
||||
color = parseColor(color);
|
||||
return (color >> 16) & 0xFF;
|
||||
}
|
||||
colors.green = function (color) {
|
||||
color = parseColor(color);
|
||||
return (color >> 8) & 0xFF;
|
||||
}
|
||||
colors.blue = function (color) {
|
||||
color = parseColor(color);
|
||||
return color & 0xFF;
|
||||
}
|
||||
var colors = Object.create(runtime.colors);
|
||||
colors.alpha = function (color) {
|
||||
color = parseColor(color);
|
||||
return color >>> 24;
|
||||
}
|
||||
colors.red = function (color) {
|
||||
color = parseColor(color);
|
||||
return (color >> 16) & 0xFF;
|
||||
}
|
||||
colors.green = function (color) {
|
||||
color = parseColor(color);
|
||||
return (color >> 8) & 0xFF;
|
||||
}
|
||||
colors.blue = function (color) {
|
||||
color = parseColor(color);
|
||||
return color & 0xFF;
|
||||
}
|
||||
|
||||
colors.isSimilar = function (c1, c2, threshold, algorithm) {
|
||||
c1 = parseColor(c1);
|
||||
c2 = parseColor(c2);
|
||||
threshold = threshold == undefined ? 4 : threshold;
|
||||
algorithm = algorithm == undefined ? "diff" : algorithm;
|
||||
var colorDetector = getColorDetector(c1, algorithm, threshold);
|
||||
return colorDetector.detectsColor(colors.red(c2), colors.green(c2), colors.blue(c2));
|
||||
}
|
||||
colors.isSimilar = function (c1, c2, threshold, algorithm) {
|
||||
c1 = parseColor(c1);
|
||||
c2 = parseColor(c2);
|
||||
threshold = threshold == undefined ? 4 : threshold;
|
||||
algorithm = algorithm == undefined ? "diff" : algorithm;
|
||||
var colorDetector = getColorDetector(c1, algorithm, threshold);
|
||||
return colorDetector.detectsColor(colors.red(c2), colors.green(c2), colors.blue(c2));
|
||||
}
|
||||
|
||||
if (android.os.Build.VERSION.SDK_INT < 19) {
|
||||
var javaImages = runtime.getImages();
|
||||
|
||||
var colorFinder = javaImages.colorFinder;
|
||||
|
||||
images.save = function (img, path, format, quality) {
|
||||
format = format || "png";
|
||||
quality = quality == undefined ? 100 : quality;
|
||||
return javaImages.save(img, path, format, quality);
|
||||
}
|
||||
|
||||
images.saveImage = images.save;
|
||||
|
||||
images.grayscale = function (img, dstCn) {
|
||||
return images.cvtColor(img, "BGR2GRAY", dstCn);
|
||||
}
|
||||
|
||||
images.threshold = function (img, threshold, maxVal, type) {
|
||||
var mat = new Mat();
|
||||
type = type || "BINARY";
|
||||
type = Imgproc["THRESH_" + type];
|
||||
Imgproc.threshold(img.mat, mat, threshold, maxVal, type);
|
||||
return images.matToImage(mat);
|
||||
}
|
||||
|
||||
images.inRange = function (img, lowerBound, upperBound) {
|
||||
var lb, ub;
|
||||
if (typeof (lowerBound) == 'string') {
|
||||
if (typeof (upperBound) == 'string') {
|
||||
lb = new Scalar(colors.red(lowerBound), colors.green(lowerBound),
|
||||
colors.blue(lowerBound), colors.alpha(lowerBound));
|
||||
ub = new Scalar(colors.red(upperBound), colors.green(upperBound),
|
||||
colors.blue(upperBound), colors.alpha(lowerBound));
|
||||
} else if (typeof (upperBound) == 'number') {
|
||||
var color = lowerBound;
|
||||
var threshold = upperBound;
|
||||
lb = new Scalar(colors.red(color) - threshold, colors.green(color) - threshold,
|
||||
colors.blue(color) - threshold, colors.alpha(color));
|
||||
ub = new Scalar(colors.red(color) + threshold, colors.green(color) + threshold,
|
||||
colors.blue(color) + threshold, colors.alpha(color));
|
||||
}else{
|
||||
throw new TypeError('lowerBound = ' + lowerBound, + 'upperBound = ' + upperBound);
|
||||
}
|
||||
}
|
||||
var bi = new Mat();
|
||||
Core.inRange(img.mat, lb, ub, bi);
|
||||
return images.matToImage(bi);
|
||||
}
|
||||
|
||||
|
||||
images.adaptiveThreshold = function(img, maxValue, adaptiveMethod, thresholdType, blockSize, C){
|
||||
var mat = new Mat();
|
||||
adaptiveMethod = Imgproc["ADAPTIVE_THRESH_" + adaptiveMethod];
|
||||
thresholdType = Imgproc["THRESH_" + thresholdType];
|
||||
Imgproc.adaptiveThreshold(img.mat, mat, maxValue, adaptiveMethod, thresholdType, blockSize, C);
|
||||
return images.matToImage(mat);
|
||||
|
||||
}
|
||||
images.blur = function (img, size, point, type) {
|
||||
var mat = new Mat();
|
||||
size = newSize(size);
|
||||
type = Imgproc["BORDER_" + (type || "CONSTANT")];
|
||||
if (point == undefined) {
|
||||
Imgproc.blur(img.mat, mat, size);
|
||||
} else {
|
||||
Imgproc.blur(img.mat, mat, size, new Point(point[0], point[1]), type);
|
||||
}
|
||||
return images.matToImage(mat);
|
||||
}
|
||||
|
||||
images.medianBlur = function (img, size) {
|
||||
var mat = new Mat();
|
||||
Imgproc.medianBlur(img.mat, mat, size);
|
||||
return images.matToImage(mat);
|
||||
}
|
||||
|
||||
|
||||
images.gaussianBlur = function (img, size, sigmaX, sigmaY, type) {
|
||||
var mat = new Mat();
|
||||
size = newSize(size);
|
||||
sigmaX = sigmaX == undefined ? 0 : sigmaX;
|
||||
sigmaY = sigmaY == undefined ? 0 : sigmaY;
|
||||
type = Imgproc["BORDER_" + (type || "DEFAULT")];
|
||||
Imgproc.GaussianBlur(img.mat, mat, size, sigmaX, sigmaY, type);
|
||||
return images.matToImage(mat);
|
||||
}
|
||||
|
||||
images.cvtColor = function (img, code, dstCn) {
|
||||
var mat = new Mat();
|
||||
code = Imgproc["COLOR_" + code];
|
||||
if (dstCn == undefined) {
|
||||
Imgproc.cvtColor(img.mat, mat, code);
|
||||
} else {
|
||||
Imgproc.cvtColor(img.mat, mat, code, dstCn);
|
||||
}
|
||||
return images.matToImage(mat);
|
||||
}
|
||||
|
||||
images.findCircles = function(grayImg, options) {
|
||||
options = options || {};
|
||||
var mat = options.region == undefined ? grayImg.mat : new Mat(grayImg.mat, buildRegion(options.region, grayImg));
|
||||
var resultMat = new Mat()
|
||||
var dp = options.dp == undefined ? 1 : options.dp;
|
||||
var minDst = options.minDst == undefined ? grayImg.height / 8 : options.minDst;
|
||||
var param1 = options.param1 == undefined ? 100 : options.param1;
|
||||
var param2 = options.param2 == undefined ? 100 : options.param2;
|
||||
var minRadius = options.minRadius == undefined ? 0 : options.minRadius;
|
||||
var maxRadius = options.maxRadius == undefined ? 0 : options.maxRadius;
|
||||
Imgproc.HoughCircles(mat, resultMat, Imgproc.CV_HOUGH_GRADIENT, dp, minDst, param1, param2, minRadius, maxRadius);
|
||||
var result = [];
|
||||
for (var i = 0; i < resultMat.rows(); i++) {
|
||||
for (var j = 0; j < resultMat.cols(); j++) {
|
||||
var d = resultMat.get(i, j);
|
||||
result.push({
|
||||
x: d[0],
|
||||
y: d[1],
|
||||
radius: d[2]
|
||||
});
|
||||
}
|
||||
}
|
||||
if(options.region != undefined){
|
||||
mat.release();
|
||||
}
|
||||
resultMat.release();
|
||||
return result;
|
||||
}
|
||||
|
||||
images.resize = function(img, size, interpolation) {
|
||||
var mat = new Mat();
|
||||
interpolation = Imgproc["INTER_" + (interpolation || "LINEAR")];
|
||||
Imgproc.resize(img.mat, mat, newSize(size), 0, 0, interpolation);
|
||||
return images.matToImage(mat);
|
||||
}
|
||||
|
||||
images.scale = function(img, fx, fy, interpolation) {
|
||||
var mat = new Mat();
|
||||
interpolation = Imgproc["INTER_" + (interpolation || "LINEAR")];
|
||||
Imgproc.resize(img.mat, mat, newSize([0, 0]), fx, fy, interpolation);
|
||||
return images.matToImage(mat);
|
||||
}
|
||||
|
||||
images.detectsColor = function (img, color, x, y, threshold, algorithm) {
|
||||
color = parseColor(color);
|
||||
algorithm = algorithm || "diff";
|
||||
threshold = threshold || defaultColorThreshold;
|
||||
var colorDetector = getColorDetector(color, algorithm, threshold);
|
||||
var pixel = images.pixel(img, x, y);
|
||||
return colorDetector.detectsColor(colors.red(pixel), colors.green(pixel), colors.blue(pixel));
|
||||
}
|
||||
|
||||
images.findColor = function (img, color, options) {
|
||||
color = parseColor(color);
|
||||
options = options || {};
|
||||
var region = options.region || [];
|
||||
if (options.similarity) {
|
||||
var threshold = parseInt(255 * (1 - options.similarity));
|
||||
} else {
|
||||
var threshold = options.threshold || defaultColorThreshold;
|
||||
}
|
||||
if (options.region) {
|
||||
return colorFinder.findColor(img, color, threshold, buildRegion(options.region, img));
|
||||
} else {
|
||||
return colorFinder.findColor(img, color, threshold, null);
|
||||
}
|
||||
}
|
||||
|
||||
images.findColorInRegion = function (img, color, x, y, width, height, threshold) {
|
||||
return findColor(img, color, {
|
||||
region: [x, y, width, height],
|
||||
threshold: threshold
|
||||
});
|
||||
}
|
||||
|
||||
images.findColorEquals = function (img, color, x, y, width, height) {
|
||||
return findColor(img, color, {
|
||||
region: [x, y, width, height],
|
||||
threshold: 0
|
||||
});
|
||||
}
|
||||
|
||||
images.findAllPointsForColor = function (img, color, options) {
|
||||
color = parseColor(color);
|
||||
options = options || {};
|
||||
if (options.similarity) {
|
||||
var threshold = parseInt(255 * (1 - options.similarity));
|
||||
} else {
|
||||
var threshold = options.threshold || defaultColorThreshold;
|
||||
}
|
||||
if (options.region) {
|
||||
return toPointArray(colorFinder.findAllPointsForColor(img, color, threshold, buildRegion(options.region, img)));
|
||||
} else {
|
||||
return toPointArray(colorFinder.findAllPointsForColor(img, color, threshold, null));
|
||||
}
|
||||
}
|
||||
|
||||
images.findMultiColors = function (img, firstColor, paths, options) {
|
||||
options = options || {};
|
||||
firstColor = parseColor(firstColor);
|
||||
var list = java.lang.reflect.Array.newInstance(java.lang.Integer.TYPE, paths.length * 3);
|
||||
for (var i = 0; i < paths.length; i++) {
|
||||
var p = paths[i];
|
||||
list[i * 3] = p[0];
|
||||
list[i * 3 + 1] = p[1];
|
||||
list[i * 3 + 2] = parseColor(p[2]);
|
||||
}
|
||||
var region = options.region ? buildRegion(options.region, img) : null;
|
||||
var threshold = options.threshold === undefined ? defaultColorThreshold : options.threshold;
|
||||
return colorFinder.findMultiColors(img, firstColor, threshold, region, list);
|
||||
}
|
||||
|
||||
images.findImage = function (img, template, options) {
|
||||
options = options || {};
|
||||
var threshold = options.threshold || 0.9;
|
||||
var maxLevel = -1;
|
||||
if (typeof (options.level) == 'number') {
|
||||
maxLevel = options.level;
|
||||
}
|
||||
var weakThreshold = options.weakThreshold || 0.7;
|
||||
if (options.region) {
|
||||
return javaImages.findImage(img, template, weakThreshold, threshold, buildRegion(options.region, img), maxLevel);
|
||||
} else {
|
||||
return javaImages.findImage(img, template, weakThreshold, threshold, null, maxLevel);
|
||||
}
|
||||
}
|
||||
|
||||
images.findImageInRegion = function (img, template, x, y, width, height, threshold) {
|
||||
return images.findImage(img, template, {
|
||||
region: [x, y, width, height],
|
||||
threshold: threshold
|
||||
});
|
||||
}
|
||||
|
||||
images.fromBase64 = function (base64) {
|
||||
return javaImages.fromBase64(base64);
|
||||
}
|
||||
|
||||
images.toBase64 = function (img, format, quality) {
|
||||
format = format || "png";
|
||||
quality = quality == undefined ? 100 : quality;
|
||||
return javaImages.toBase64(img, format, quality);
|
||||
}
|
||||
|
||||
images.fromBytes = function (bytes) {
|
||||
return javaImages.fromBytes(bytes);
|
||||
}
|
||||
|
||||
images.toBytes = function (img, format, quality) {
|
||||
format = format || "png";
|
||||
quality = quality == undefined ? 100 : quality;
|
||||
return javaImages.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
|
||||
};
|
||||
}
|
||||
|
||||
images.matToImage = function(img){
|
||||
return Image.ofMat(img);
|
||||
}
|
||||
|
||||
function getColorDetector(color, algorithm, threshold) {
|
||||
switch (algorithm) {
|
||||
case "rgb":
|
||||
return new com.stardust.autojs.core.image.ColorDetector.RGBDistanceDetector(color, threshold);
|
||||
case "equal":
|
||||
return new com.stardust.autojs.core.image.ColorDetector.EqualityDetector(color);
|
||||
case "diff":
|
||||
return new com.stardust.autojs.core.image.ColorDetector.DifferenceDetector(color, threshold);
|
||||
case "rgb+":
|
||||
return new com.stardust.autojs.core.image.ColorDetector.WeightedRGBDistanceDetector(color, threshold);
|
||||
case "hs":
|
||||
return new com.stardust.autojs.core.image.ColorDetector.HSDistanceDetector(color, threshold);
|
||||
}
|
||||
throw new Error("Unknown algorithm: " + algorithm);
|
||||
}
|
||||
|
||||
|
||||
function toPointArray(points) {
|
||||
var arr = [];
|
||||
for (var i = 0; i < points.length; i++) {
|
||||
arr.push(points[i]);
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
function buildRegion(region, img) {
|
||||
var x = region[0] === undefined ? 0 : region[0];
|
||||
var y = region[1] === undefined ? 0 : region[1];
|
||||
var width = region[2] === undefined ? img.getWidth() - x : region[2];
|
||||
var height = region[3] === undefined ? (img.getHeight() - y) : region[3];
|
||||
var r = new org.opencv.core.Rect(x, y, width, height);
|
||||
return r;
|
||||
}
|
||||
|
||||
function parseColor(color) {
|
||||
if (typeof (color) == 'string') {
|
||||
color = colors.parseColor(color);
|
||||
}
|
||||
return color;
|
||||
}
|
||||
|
||||
function newSize(size) {
|
||||
if (!Array.isArray(size)) {
|
||||
size = [size, size];
|
||||
}
|
||||
if (size.length == 1) {
|
||||
size = [size[0], size[0]];
|
||||
}
|
||||
return new Size(size[0], size[1]);
|
||||
}
|
||||
|
||||
scope.__asGlobal__(images, ['requestScreenCapture', 'captureScreen', 'findImage', 'findImageInRegion', 'findColor', 'findColorInRegion', 'findColorEquals', 'findMultiColors']);
|
||||
|
||||
scope.colors = colors;
|
||||
|
||||
return images;
|
||||
}
|
||||
|
||||
var rtImages = __runtime__.getImages();
|
||||
|
||||
var colorFinder = rtImages.colorFinder;
|
||||
|
||||
images.requestScreenCapture = rtImages.requestScreenCapture.bind(rtImages);
|
||||
|
||||
images.captureScreen = rtImages.captureScreen.bind(rtImages);
|
||||
|
||||
images.read = rtImages.read.bind(rtImages);
|
||||
|
||||
images.copy = rtImages.copy.bind(rtImages);
|
||||
|
||||
images.load = rtImages.load.bind(rtImages);
|
||||
|
||||
images.clip = rtImages.clip.bind(rtImages);
|
||||
|
||||
images.save = function (img, path, format, quality) {
|
||||
format = format || "png";
|
||||
quality = quality == undefined ? 100 : quality;
|
||||
return rtImages.save(img, path, format, quality);
|
||||
}
|
||||
|
||||
images.saveImage = images.save;
|
||||
|
||||
images.pixel = rtImages.pixel;
|
||||
|
||||
images.grayscale = function (img, dstCn) {
|
||||
return images.cvtColor(img, "BGR2GRAY", dstCn);
|
||||
}
|
||||
|
||||
images.threshold = function (img, threshold, maxVal, type) {
|
||||
var mat = newMat();
|
||||
type = type || "BINARY";
|
||||
type = Imgproc["THRESH_" + type];
|
||||
Imgproc.threshold(img.mat, mat, threshold, maxVal, type);
|
||||
return matToImage(mat);
|
||||
}
|
||||
|
||||
images.adaptiveThreshold = function(img, maxValue, adaptiveMethod, thresholdType, blockSize, C){
|
||||
var mat = newMat();
|
||||
adaptiveMethod = Imgproc["ADAPTIVE_THRESH_" + adaptiveMethod];
|
||||
thresholdType = Imgproc["THRESH_" + thresholdType];
|
||||
Imgproc.adaptiveThreshold(img.mat, mat, maxValue, adaptiveMethod, thresholdType, blockSize, C);
|
||||
return matToImage(mat);
|
||||
|
||||
}
|
||||
images.blur = function (img, size, point, type) {
|
||||
var mat = newMat();
|
||||
size = newSize(size);
|
||||
type = Imgproc["BORDER_" + (type || "CONSTANT")];
|
||||
if (point == undefined) {
|
||||
Imgproc.blur(img.mat, mat, size);
|
||||
} else {
|
||||
Imgproc.blur(img.mat, mat, size, new Point(point[0], point[1]), type);
|
||||
}
|
||||
return matToImage(mat);
|
||||
}
|
||||
|
||||
images.medianBlur = function (img, size) {
|
||||
var mat = newMat();
|
||||
Imgproc.medianBlur(img.mat, mat, size);
|
||||
return matToImage(mat);
|
||||
}
|
||||
|
||||
|
||||
images.gaussianBlur = function (img, size, sigmaX, sigmaY, type) {
|
||||
var mat = newMat();
|
||||
size = newSize(size);
|
||||
sigmaX = sigmaX == undefined ? 0 : sigmaX;
|
||||
sigmaY = sigmaY == undefined ? 0 : sigmaY;
|
||||
type = Imgproc["BORDER_" + (type || "DEFAULT")];
|
||||
Imgproc.GaussianBlur(img.mat, mat, size, sigmaX, sigmaY, type);
|
||||
return matToImage(mat);
|
||||
}
|
||||
|
||||
images.cvtColor = function (img, code, dstCn) {
|
||||
var mat = newMat();
|
||||
code = Imgproc["COLOR_" + code];
|
||||
if (dstCn == undefined) {
|
||||
Imgproc.cvtColor(img.mat, mat, code);
|
||||
} else {
|
||||
Imgproc.cvtColor(img.mat, mat, code, dstCn);
|
||||
}
|
||||
return matToImage(mat);
|
||||
}
|
||||
|
||||
images.resize = function(img, size, interpolation) {
|
||||
var mat = newMat();
|
||||
interpolation = Imgproc["INTER_" + (interpolation || "LINEAR")];
|
||||
Imgproc.resize(img.mat, mat, newSize(size), 0, 0, interpolation);
|
||||
return matToImage(mat);
|
||||
}
|
||||
|
||||
images.scale = function(img, fx, fy, interpolation) {
|
||||
var mat = newMat();
|
||||
interpolation = Imgproc["INTER_" + (interpolation || "LINEAR")];
|
||||
Imgproc.resize(img.mat, mat, newSize([0, 0]), fx, fy, interpolation);
|
||||
return matToImage(mat);
|
||||
}
|
||||
|
||||
images.detectsColor = function (img, color, x, y, threshold, algorithm) {
|
||||
color = parseColor(color);
|
||||
algorithm = algorithm || "diff";
|
||||
threshold = threshold || defaultColorThreshold;
|
||||
var colorDetector = getColorDetector(color, algorithm, threshold);
|
||||
var pixel = images.pixel(img, x, y);
|
||||
return colorDetector.detectsColor(colors.red(pixel), colors.green(pixel), colors.blue(pixel));
|
||||
}
|
||||
|
||||
images.findColor = function (img, color, options) {
|
||||
color = parseColor(color);
|
||||
options = options || {};
|
||||
var region = options.region || [];
|
||||
if (options.similarity) {
|
||||
var threshold = parseInt(255 * (1 - options.similarity));
|
||||
} else {
|
||||
var threshold = options.threshold || defaultColorThreshold;
|
||||
}
|
||||
if (options.region) {
|
||||
return colorFinder.findColor(img, color, threshold, buildRegion(options.region, img));
|
||||
} else {
|
||||
return colorFinder.findColor(img, color, threshold, null);
|
||||
}
|
||||
}
|
||||
|
||||
images.findColorInRegion = function (img, color, x, y, width, height, threshold) {
|
||||
return findColor(img, color, {
|
||||
region: [x, y, width, height],
|
||||
threshold: threshold
|
||||
});
|
||||
}
|
||||
|
||||
images.findColorEquals = function (img, color, x, y, width, height) {
|
||||
return findColor(img, color, {
|
||||
region: [x, y, width, height],
|
||||
threshold: 0
|
||||
});
|
||||
}
|
||||
|
||||
images.findAllPointsForColor = function (img, color, options) {
|
||||
color = parseColor(color);
|
||||
options = options || {};
|
||||
if (options.similarity) {
|
||||
var threshold = parseInt(255 * (1 - options.similarity));
|
||||
} else {
|
||||
var threshold = options.threshold || defaultColorThreshold;
|
||||
}
|
||||
if (options.region) {
|
||||
return toPointArray(colorFinder.findAllPointsForColor(img, color, threshold, buildRegion(options.region, img)));
|
||||
} else {
|
||||
return toPointArray(colorFinder.findAllPointsForColor(img, color, threshold, null));
|
||||
}
|
||||
}
|
||||
|
||||
images.findMultiColors = function (img, firstColor, paths, options) {
|
||||
options = options || {};
|
||||
firstColor = parseColor(firstColor);
|
||||
var list = java.lang.reflect.Array.newInstance(java.lang.Integer.TYPE, paths.length * 3);
|
||||
for (var i = 0; i < paths.length; i++) {
|
||||
var p = paths[i];
|
||||
list[i * 3] = p[0];
|
||||
list[i * 3 + 1] = p[1];
|
||||
list[i * 3 + 2] = parseColor(p[2]);
|
||||
}
|
||||
var region = options.region ? buildRegion(options.region, img) : null;
|
||||
var threshold = options.threshold === undefined ? defaultColorThreshold : options.threshold;
|
||||
return colorFinder.findMultiColors(img, firstColor, threshold, region, list);
|
||||
}
|
||||
|
||||
images.findImage = function (img, template, options) {
|
||||
options = options || {};
|
||||
var threshold = options.threshold || 0.9;
|
||||
var maxLevel = -1;
|
||||
if (typeof (options.level) == 'number') {
|
||||
maxLevel = options.level;
|
||||
}
|
||||
var weakThreshold = options.weakThreshold || 0.7;
|
||||
if (options.region) {
|
||||
return rtImages.findImage(img, template, weakThreshold, threshold, buildRegion(options.region, img), maxLevel);
|
||||
} else {
|
||||
return rtImages.findImage(img, template, weakThreshold, threshold, null, maxLevel);
|
||||
}
|
||||
}
|
||||
|
||||
images.findImageInRegion = function (img, template, x, y, width, height, threshold) {
|
||||
return images.findImage(img, template, {
|
||||
region: [x, y, width, height],
|
||||
threshold: threshold
|
||||
});
|
||||
}
|
||||
|
||||
images.fromBase64 = function (base64) {
|
||||
return rtImages.fromBase64(base64);
|
||||
}
|
||||
|
||||
images.toBase64 = function (img, format, quality) {
|
||||
format = format || "png";
|
||||
quality = quality == undefined ? 100 : quality;
|
||||
return rtImages.toBase64(img, format, quality);
|
||||
}
|
||||
|
||||
images.fromBytes = function (bytes) {
|
||||
return rtImages.fromBytes(bytes);
|
||||
}
|
||||
|
||||
images.toBytes = function (img, format, quality) {
|
||||
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) {
|
||||
case "rgb":
|
||||
return new com.stardust.autojs.core.image.ColorDetector.RGBDistanceDetector(color, threshold);
|
||||
case "equal":
|
||||
return new com.stardust.autojs.core.image.ColorDetector.EqualityDetector(color);
|
||||
case "diff":
|
||||
return new com.stardust.autojs.core.image.ColorDetector.DifferenceDetector(color, threshold);
|
||||
case "rgb+":
|
||||
return new com.stardust.autojs.core.image.ColorDetector.WeightedRGBDistanceDetector(color, threshold);
|
||||
case "hs":
|
||||
return new com.stardust.autojs.core.image.ColorDetector.HSDistanceDetector(color, threshold);
|
||||
}
|
||||
throw new Error("Unknown algorithm: " + algorithm);
|
||||
}
|
||||
|
||||
|
||||
function toPointArray(points) {
|
||||
var arr = [];
|
||||
for (var i = 0; i < points.length; i++) {
|
||||
arr.push(points[i]);
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
function buildRegion(region, img) {
|
||||
var x = region[0] === undefined ? 0 : region[0];
|
||||
var y = region[1] === undefined ? 0 : region[1];
|
||||
var width = region[2] === undefined ? img.getWidth() - x : region[2];
|
||||
var height = region[3] === undefined ? (img.getHeight() - y) : region[3];
|
||||
var r = new org.opencv.core.Rect(x, y, width, height);
|
||||
return r;
|
||||
}
|
||||
|
||||
function parseColor(color) {
|
||||
if (typeof (color) == 'string') {
|
||||
color = colors.parseColor(color);
|
||||
}
|
||||
return color;
|
||||
}
|
||||
|
||||
function newMat() {
|
||||
return new com.stardust.autojs.core.opencv.Mat();
|
||||
}
|
||||
|
||||
function matToImage(mat) {
|
||||
return com.stardust.autojs.core.image.ImageWrapper.ofMat(mat);
|
||||
}
|
||||
|
||||
function newSize(size) {
|
||||
if (!Array.isArray(size)) {
|
||||
size = [size, size];
|
||||
}
|
||||
if (size.length == 1) {
|
||||
size = [size[0], size[0]];
|
||||
}
|
||||
return new Size(size[0], size[1]);
|
||||
}
|
||||
|
||||
scope.__asGlobal__(images, ['requestScreenCapture', 'captureScreen', 'findImage', 'findImageInRegion', 'findColor', 'findColorInRegion', 'findColorEquals', 'findMultiColors']);
|
||||
|
||||
scope.colors = colors;
|
||||
|
||||
return images;
|
||||
}
|
||||
@@ -19,7 +19,6 @@
|
||||
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
||||
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
var formatRegExp = /%[sdj%]/g;
|
||||
exports.extend = (function () {
|
||||
var extendStatics = Object.setPrototypeOf ||
|
||||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
||||
@@ -30,7 +29,16 @@ exports.extend = (function () {
|
||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||
};
|
||||
})();
|
||||
|
||||
exports.java = require("__java_util__");
|
||||
|
||||
exports.__assignFunctions__ = function(src, target, functions) {
|
||||
for(let f of functions){
|
||||
target[f] = src[f].bind(src);
|
||||
}
|
||||
}
|
||||
|
||||
var formatRegExp = /%[sdj%]/g;
|
||||
exports.format = function(f) {
|
||||
if (!isString(f)) {
|
||||
var objects = [];
|
||||
|
||||
@@ -30,7 +30,7 @@ module = (typeof module === 'undefined') ? {} : module;
|
||||
NativeRequire.require = require;
|
||||
}
|
||||
|
||||
function Module (id, parent, core) {
|
||||
function Module(id, parent, core) {
|
||||
this.id = id;
|
||||
this.core = core;
|
||||
this.parent = parent;
|
||||
@@ -56,7 +56,8 @@ module = (typeof module === 'undefined') ? {} : module;
|
||||
}.bind(this);
|
||||
}
|
||||
|
||||
Module._load = function _load (file, parent, core, main) {
|
||||
Module._load = function _load(file, parent, core, main) {
|
||||
return NativeRequire.require(file);
|
||||
var module = new Module(file, parent, core);
|
||||
var body = readFile(module.filename, module.core);
|
||||
var dir = new File(module.filename).getParent();
|
||||
@@ -68,21 +69,21 @@ module = (typeof module === 'undefined') ? {} : module;
|
||||
return module.exports;
|
||||
};
|
||||
|
||||
Module.runMain = function runMain (main) {
|
||||
Module.runMain = function runMain(main) {
|
||||
var file = Require.resolve(main);
|
||||
Module._load(file, undefined, false, true);
|
||||
};
|
||||
|
||||
function Require (id, parent) {
|
||||
function Require(id, parent) {
|
||||
var normalizePath = normalizeName(id);
|
||||
if(builtInModules.indexOf(normalizePath) >= 0 && !files.exists(normalizePath)){
|
||||
return NativeRequire.require(normalizePath);
|
||||
if (builtInModules.indexOf(normalizePath) >= 0 && !files.exists(normalizePath)) {
|
||||
return NativeRequire.require(normalizePath);
|
||||
}
|
||||
if(id === "events"){
|
||||
return events;
|
||||
if (id === "events") {
|
||||
return events;
|
||||
}
|
||||
if(id.startsWith("http://") || id.startsWith("https://")){
|
||||
return NativeRequire.require(id);
|
||||
if (id.startsWith("http://") || id.startsWith("https://")) {
|
||||
return NativeRequire.require(id);
|
||||
}
|
||||
|
||||
var core;
|
||||
@@ -105,21 +106,12 @@ module = (typeof module === 'undefined') ? {} : module;
|
||||
file = file.path;
|
||||
core = true;
|
||||
}
|
||||
try {
|
||||
if (Require.cache[file]) {
|
||||
return Require.cache[file];
|
||||
} else if (file.endsWith('.js')) {
|
||||
return Module._load(file, parent, core);
|
||||
} else if (file.endsWith('.json')) {
|
||||
return loadJSON(file);
|
||||
}
|
||||
} catch (ex) {
|
||||
if (ex instanceof java.lang.Exception) {
|
||||
throw new ModuleError('Cannot load module ' + id, 'LOAD_ERROR', ex);
|
||||
} else {
|
||||
System.out.println('Cannot load module ' + id + ' LOAD_ERROR');
|
||||
throw ex;
|
||||
}
|
||||
if (Require.cache[file]) {
|
||||
return Require.cache[file];
|
||||
} else if (file.endsWith('.js')) {
|
||||
return Module._load(file, parent, core);
|
||||
} else if (file.endsWith('.json')) {
|
||||
return loadJSON(file);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,10 +120,10 @@ module = (typeof module === 'undefined') ? {} : module;
|
||||
for (var i = 0; i < roots.length; ++i) {
|
||||
var root = roots[i];
|
||||
var result = resolveCoreModule(id, root) ||
|
||||
resolveAsFile(id, root, '.js') ||
|
||||
resolveAsFile(id, root, '.json') ||
|
||||
resolveAsDirectory(id, root) ||
|
||||
resolveAsNodeModule(id, root);
|
||||
resolveAsFile(id, root, '.js') ||
|
||||
resolveAsFile(id, root, '.json') ||
|
||||
resolveAsDirectory(id, root) ||
|
||||
resolveAsNodeModule(id, root);
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
@@ -142,13 +134,13 @@ module = (typeof module === 'undefined') ? {} : module;
|
||||
Require.root = files.cwd();//System.getProperty('user.dir');
|
||||
Require.NODE_PATH = undefined;
|
||||
|
||||
function findRoots (parent) {
|
||||
function findRoots(parent) {
|
||||
var r = [];
|
||||
r.push(findRoot(parent));
|
||||
return r.concat(Require.paths());
|
||||
}
|
||||
|
||||
function parsePaths (paths) {
|
||||
function parsePaths(paths) {
|
||||
if (!paths) {
|
||||
return [];
|
||||
}
|
||||
@@ -184,7 +176,7 @@ module = (typeof module === 'undefined') ? {} : module;
|
||||
return r;
|
||||
};
|
||||
|
||||
function findRoot (parent) {
|
||||
function findRoot(parent) {
|
||||
if (!parent || !parent.id) { return Require.root; }
|
||||
var pathParts = parent.id.split(/[\/|\\,]+/g);
|
||||
pathParts.pop();
|
||||
@@ -199,20 +191,20 @@ module = (typeof module === 'undefined') ? {} : module;
|
||||
Module.require = require;
|
||||
module.exports = Module;
|
||||
|
||||
function loadJSON (file) {
|
||||
function loadJSON(file) {
|
||||
var json = JSON.parse(readFile(file));
|
||||
Require.cache[file] = json;
|
||||
return json;
|
||||
}
|
||||
|
||||
function resolveAsNodeModule (id, root) {
|
||||
function resolveAsNodeModule(id, root) {
|
||||
var base = [root, 'node_modules'].join('/');
|
||||
return resolveAsFile(id, base) ||
|
||||
resolveAsDirectory(id, base) ||
|
||||
(root ? resolveAsNodeModule(id, new File(root).getParent()) : false);
|
||||
}
|
||||
|
||||
function resolveAsDirectory (id, root) {
|
||||
function resolveAsDirectory(id, root) {
|
||||
var base = [root, id].join('/');
|
||||
var file = new File([base, 'package.json'].join('/'));
|
||||
if (file.exists()) {
|
||||
@@ -232,7 +224,7 @@ module = (typeof module === 'undefined') ? {} : module;
|
||||
return resolveAsFile('index.js', base);
|
||||
}
|
||||
|
||||
function resolveAsFile (id, root, ext) {
|
||||
function resolveAsFile(id, root, ext) {
|
||||
var file;
|
||||
if (id.length > 0 && id[0] === '/') {
|
||||
file = new File(normalizeName(id, ext));
|
||||
@@ -247,7 +239,7 @@ module = (typeof module === 'undefined') ? {} : module;
|
||||
}
|
||||
}
|
||||
|
||||
function resolveCoreModule (id, root) {
|
||||
function resolveCoreModule(id, root) {
|
||||
var name = normalizeName(id);
|
||||
var classloader = java.lang.Thread.currentThread().getContextClassLoader();
|
||||
if (classloader.getResource(name)) {
|
||||
@@ -255,9 +247,9 @@ module = (typeof module === 'undefined') ? {} : module;
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeName (fileName, ext) {
|
||||
if(fileName.endsWith('.json')){
|
||||
return fileName;
|
||||
function normalizeName(fileName, ext) {
|
||||
if (fileName.endsWith('.json')) {
|
||||
return fileName;
|
||||
}
|
||||
var extension = ext || '.js';
|
||||
if (fileName.endsWith(extension)) {
|
||||
@@ -266,7 +258,7 @@ module = (typeof module === 'undefined') ? {} : module;
|
||||
return fileName + extension;
|
||||
}
|
||||
|
||||
function readFile (filename, core) {
|
||||
function readFile(filename, core) {
|
||||
var input;
|
||||
try {
|
||||
if (core) {
|
||||
@@ -282,7 +274,7 @@ module = (typeof module === 'undefined') ? {} : module;
|
||||
}
|
||||
}
|
||||
|
||||
function ModuleError (message, code, cause) {
|
||||
function ModuleError(message, code, cause) {
|
||||
this.code = code || 'UNDEFINED';
|
||||
this.message = message || 'Error loading module';
|
||||
this.cause = cause;
|
||||
|
||||
@@ -97,6 +97,7 @@ public class Mat extends org.opencv.core.Mat implements ResourceMonitor.Resource
|
||||
if (!mReleased) {
|
||||
ResourceMonitor.onFinalize(this);
|
||||
super.release();
|
||||
mReleased = true;
|
||||
}
|
||||
super.finalize();
|
||||
}
|
||||
|
||||
@@ -9,8 +9,6 @@ import com.afollestad.materialdialogs.MaterialDialog;
|
||||
import org.opencv.android.InstallCallbackInterface;
|
||||
import org.opencv.android.LoaderCallbackInterface;
|
||||
import org.opencv.android.OpenCVLoader;
|
||||
import org.opencv.core.Core;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
|
||||
/**
|
||||
@@ -33,6 +31,8 @@ public class OpenCVHelper {
|
||||
public static void release(@Nullable MatOfPoint mat) {
|
||||
if (mat == null)
|
||||
return;
|
||||
|
||||
|
||||
mat.release();
|
||||
}
|
||||
|
||||
|
||||
@@ -452,7 +452,8 @@ public class ScriptRuntime {
|
||||
}
|
||||
|
||||
public static String getStackTrace(Throwable e, boolean printJavaStackTrace) {
|
||||
StringBuilder scriptTrace = new StringBuilder();
|
||||
String message = e.getMessage();
|
||||
StringBuilder scriptTrace = new StringBuilder(message == null ? "" : message + "\n");
|
||||
if (e instanceof RhinoException) {
|
||||
RhinoException rhinoException = (RhinoException) e;
|
||||
scriptTrace.append(rhinoException.details()).append("\n");
|
||||
@@ -479,7 +480,7 @@ public class ScriptRuntime {
|
||||
return scriptTrace.toString();
|
||||
} catch (IOException e1) {
|
||||
e1.printStackTrace();
|
||||
return e.getMessage();
|
||||
return message;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import android.graphics.Matrix;
|
||||
import android.media.Image;
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.os.SystemClock;
|
||||
import android.support.annotation.RequiresApi;
|
||||
import android.util.Base64;
|
||||
import android.view.Display;
|
||||
@@ -29,6 +30,7 @@ import com.stardust.pio.UncheckedIOException;
|
||||
import com.stardust.util.ScreenMetrics;
|
||||
|
||||
import com.stardust.autojs.core.opencv.Mat;
|
||||
|
||||
import org.opencv.core.Point;
|
||||
import org.opencv.core.Rect;
|
||||
|
||||
@@ -270,4 +272,12 @@ public class Images {
|
||||
return point;
|
||||
}
|
||||
|
||||
public Mat newMat() {
|
||||
return new Mat();
|
||||
}
|
||||
|
||||
public Mat newMat(Mat mat, Rect roi) {
|
||||
return new Mat(mat, roi);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user