From 3ce51e4a012c2ecffe0efbf3928ef0bec35cc1ee Mon Sep 17 00:00:00 2001 From: SuperMonster003 <30370009+SuperMonster003@users.noreply.github.com> Date: Sat, 1 Jan 2022 23:25:27 +0800 Subject: [PATCH] =?UTF-8?q?6.0.1=20-=20Fix=20-=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E4=B8=8E=20VSCode=20=E4=BF=9D=E6=8C=81=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E6=97=B6=E6=8E=A7=E5=88=B6=E5=8F=B0=E6=89=93=E5=8D=B0=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/autojs/autojs/autojs/AutoJs.java | 2 +- autojs/src/main/assets/modules/__images__.js | 876 +++++++++--------- project-versions.json | 2 +- 3 files changed, 438 insertions(+), 442 deletions(-) diff --git a/app/src/main/java/org/autojs/autojs/autojs/AutoJs.java b/app/src/main/java/org/autojs/autojs/autojs/AutoJs.java index ec3dd525..15e56062 100644 --- a/app/src/main/java/org/autojs/autojs/autojs/AutoJs.java +++ b/app/src/main/java/org/autojs/autojs/autojs/AutoJs.java @@ -112,7 +112,7 @@ public class AutoJs extends com.stardust.autojs.AutoJs { @Override public String println(int level, CharSequence charSequence) { String log = super.println(level, charSequence); - DevPluginService.getInstance().print(log); + new Thread(() -> DevPluginService.getInstance().print(log)).start(); return log; } }; diff --git a/autojs/src/main/assets/modules/__images__.js b/autojs/src/main/assets/modules/__images__.js index 7ed90868..a1951631 100644 --- a/autojs/src/main/assets/modules/__images__.js +++ b/autojs/src/main/assets/modules/__images__.js @@ -1,8 +1,21 @@ +// noinspection NpmUsedModulesInstalled + +const Point = org.opencv.core.Point; +const Rect = org.opencv.core.Rect; +const Scalar = org.opencv.core.Scalar; +const Size = org.opencv.core.Size; +const Core = org.opencv.core.Core; +const Imgproc = org.opencv.imgproc.Imgproc; +const Mat = com.stardust.autojs.core.opencv.Mat; +const Images = com.stardust.autojs.runtime.api.Images; + +const DEF_COLOR_THRESHOLD = 4; module.exports = function (runtime, scope) { - const ResultAdapter = require("result_adapter"); - const MatchingResult = (function () { + const ResultAdapter = require('result_adapter'); + + const MatchingResult = (function $iiFe() { let comparators = { 'left': (l, r) => l.point.x - r.point.x, 'top': (l, r) => l.point.y - r.point.y, @@ -14,38 +27,30 @@ module.exports = function (runtime, scope) { if (Array.isArray(list)) { this.matches = list; } else { - this.matches = runtime.bridges.bridges.toArray(list); + this.matches = runtime.bridges.getBridges().toArray(list); } - this.__defineGetter__('points', () => { - if (typeof (this.__points__) == 'undefined') { - this.__points__ = this.matches.map(m => m.point); - } - return this.__points__; + Object.defineProperty(this, 'points', { + get() { + if (typeof this.__points__ === 'undefined') { + this.__points__ = this.matches.map(m => m.point); + } + return this.__points__; + }, }); } MatchingResult.prototype.first = function () { - if (this.matches.length == 0) { - return null; - } - return this.matches[0]; + return this.matches.length ? this.matches[0] : null; }; MatchingResult.prototype.last = function () { - if (this.matches.length == 0) { - return null; - } - return this.matches[this.matches.length - 1]; + return this.matches.length ? this.matches[this.matches.length - 1] : null; }; MatchingResult.prototype.findMax = function (cmp) { - if (this.matches.length == 0) { + if (!this.matches.length) { return null; } - var target = this.matches[0]; - this.matches.forEach(m => { - if (cmp(target, m) > 0) { - target = m; - } - }); + let target = this.matches[0]; + this.matches.forEach(m => target = cmp(target, m) > 0 ? m : target); return target; }; MatchingResult.prototype.leftmost = function () { @@ -67,24 +72,21 @@ module.exports = function (runtime, scope) { return this.findMax((l, r) => r.similarity - l.similarity); }; MatchingResult.prototype.sortBy = function (cmp) { - var comparatorFn = null; - if (typeof (cmp) == 'string') { - cmp.split('-').forEach(direction => { - var buildInFn = comparators[direction]; + let comparatorFn = null; + if (typeof cmp === 'string') { + cmp.split('-').forEach((direction) => { + let buildInFn = comparators[direction]; if (!buildInFn) { throw new Error('unknown direction \'' + direction + '\' in \'' + cmp + '\''); } (function (fn) { - if (comparatorFn == null) { + if (comparatorFn === null) { comparatorFn = fn; } else { comparatorFn = (function (comparatorFn, fn) { return function (l, r) { - var cmpValue = comparatorFn(l, r); - if (cmpValue == 0) { - return fn(l, r); - } - return cmpValue; + let cmpValue = comparatorFn(l, r); + return cmpValue === 0 ? fn(l, r) : cmpValue; }; })(comparatorFn, fn); } @@ -93,465 +95,459 @@ module.exports = function (runtime, scope) { } else { comparatorFn = cmp; } - var clone = this.matches.slice(); + let clone = this.matches.slice(); clone.sort(comparatorFn); return new MatchingResult(clone); }; + return MatchingResult; })(); - function images() { + const rtImages = runtime.getImages(); + + const colorFinder = rtImages.colorFinder; + + 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); } - if (android.os.Build.VERSION.SDK_INT >= 21) { - util.__assignFunctions__(runtime.images, images, ['captureScreen', 'read', 'copy', 'load', 'clip', 'pixel']) + + function toPointArray(points) { + let arr = []; + for (let i = 0; i < points.length; i++) { + arr.push(points[i]); + } + return arr; } - 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 colors = Object.create(runtime.colors); - colors.alpha = function (color) { - color = parseColor(color); - return color >>> 24; + function buildRegion(region, img) { + if (region === undefined) { + region = []; } - 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; + let x = region[0] === undefined ? 0 : region[0]; + let y = region[1] === undefined ? 0 : region[1]; + let width = region[2] === undefined ? img.getWidth() - x : region[2]; + let height = region[3] === undefined ? (img.getHeight() - y) : region[3]; + let r = new Rect(x, y, width, height); + if (x < 0 || y < 0 || x + width > img.width || y + height > img.height) { + throw new Error('out of region: region = [' + [x, y, width, height] + '], image.size = [' + [img.width, img.height] + ']'); } + return r; + } - 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)); + function parseColor(color) { + if (typeof color === 'string') { + color = colors.parseColor(color); } + return color; + } - var javaImages = runtime.getImages(); + 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]); + } - var colorFinder = javaImages.colorFinder; + function initIfNeeded() { + rtImages.initOpenCvIfNeeded(); + } - images.requestScreenCapture = function (landscape) { - let ScreenCapturer = com.stardust.autojs.core.image.capture.ScreenCapturer; - var orientation = ScreenCapturer.ORIENTATION_AUTO; - if (landscape === true) { - orientation = ScreenCapturer.ORIENTATION_LANDSCAPE; + const colors = Object.create(runtime.colors, { + alpha: { + value(color) { + color = parseColor(color); + return color >>> 24; + }, + enumerable: true, + }, + red: { + value(color) { + color = parseColor(color); + return (color >> 16) & 0xFF; + }, + enumerable: true, + }, + green: { + value(color) { + color = parseColor(color); + return (color >> 8) & 0xFF; + }, + enumerable: true, + }, + blue: { + value(color) { + color = parseColor(color); + return color & 0xFF; + }, + enumerable: true, + }, + isSimilar: { + value(c1, c2, threshold, algorithm) { + c1 = parseColor(c1); + c2 = parseColor(c2); + threshold = threshold === undefined ? 4 : threshold; + algorithm = algorithm === undefined ? 'diff' : algorithm; + let colorDetector = getColorDetector(c1, algorithm, threshold); + return colorDetector.detectsColor(colors.red(c2), colors.green(c2), colors.blue(c2)); + }, + enumerable: true, + }, + }); + + const images = () => void 0; + + images.requestScreenCapture = function (landscape) { + let ScreenCapturer = com.stardust.autojs.core.image.capture.ScreenCapturer; + let orientation = ScreenCapturer.ORIENTATION_AUTO; + if (landscape === true) { + orientation = ScreenCapturer.ORIENTATION_LANDSCAPE; + } + if (landscape === false) { + orientation = ScreenCapturer.ORIENTATION_PORTRAIT; + } + return ResultAdapter.wait(rtImages.requestScreenCapture(orientation)); + }; + + images.save = function (img, path, format, quality) { + format = format || 'png'; + quality = quality === undefined ? 100 : quality; + return rtImages.save(img, path, format, quality); + }; + + images.saveImage = function (img, path, format, quality) { + return images.save(img, path, format, quality); + }; + + images.grayscale = function (img, dstCn) { + return images.cvtColor(img, 'BGR2GRAY', dstCn); + }; + + images.threshold = function (img, threshold, maxVal, type) { + initIfNeeded(); + let 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) { + initIfNeeded(); + let lb = new Scalar(colors.red(lowerBound), colors.green(lowerBound), + colors.blue(lowerBound), colors.alpha(lowerBound)); + let ub = new Scalar(colors.red(upperBound), colors.green(upperBound), + colors.blue(upperBound), colors.alpha(lowerBound)); + let bi = new Mat(); + Core.inRange(img.mat, lb, ub, bi); + return images.matToImage(bi); + }; + + images.interval = function (img, color, threshold) { + initIfNeeded(); + let lb = new Scalar(colors.red(color) - threshold, colors.green(color) - threshold, + colors.blue(color) - threshold, colors.alpha(color)); + let ub = new Scalar(colors.red(color) + threshold, colors.green(color) + threshold, + colors.blue(color) + threshold, colors.alpha(color)); + let bi = new Mat(); + Core.inRange(img.mat, lb, ub, bi); + return images.matToImage(bi); + }; + + images.adaptiveThreshold = function (img, maxValue, adaptiveMethod, thresholdType, blockSize, C) { + initIfNeeded(); + let 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) { + initIfNeeded(); + let mat = new Mat(); + size = newSize(size); + type = Core['BORDER_' + (type || 'DEFAULT')]; + 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) { + initIfNeeded(); + let mat = new Mat(); + Imgproc.medianBlur(img.mat, mat, size); + return images.matToImage(mat); + }; + + images.gaussianBlur = function (img, size, sigmaX, sigmaY, type) { + initIfNeeded(); + let mat = new Mat(); + size = newSize(size); + sigmaX = sigmaX === undefined ? 0 : sigmaX; + sigmaY = sigmaY === undefined ? 0 : sigmaY; + type = Core['BORDER_' + (type || 'DEFAULT')]; + Imgproc.GaussianBlur(img.mat, mat, size, sigmaX, sigmaY, type); + return images.matToImage(mat); + }; + + images.cvtColor = function (img, code, dstCn) { + initIfNeeded(); + let 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) { + initIfNeeded(); + options = options || {}; + let mat = options.region === undefined ? grayImg.mat : new Mat(grayImg.mat, buildRegion(options.region, grayImg)); + let resultMat = new Mat(); + let dp = options.dp === undefined ? 1 : options.dp; + let minDst = options.minDst === undefined ? grayImg.height / 8 : options.minDst; + let param1 = options.param1 === undefined ? 100 : options.param1; + let param2 = options.param2 === undefined ? 100 : options.param2; + let minRadius = options.minRadius === undefined ? 0 : options.minRadius; + let maxRadius = options.maxRadius === undefined ? 0 : options.maxRadius; + Imgproc.HoughCircles(mat, resultMat, Imgproc.CV_HOUGH_GRADIENT, dp, minDst, param1, param2, minRadius, maxRadius); + let result = []; + for (let i = 0; i < resultMat.rows(); i++) { + for (let j = 0; j < resultMat.cols(); j++) { + let d = resultMat.get(i, j); + result.push({ + x: d[0], + y: d[1], + radius: d[2], + }); } - if (landscape === false) { - orientation = ScreenCapturer.ORIENTATION_PORTRAIT; - } - return ResultAdapter.wait(javaImages.requestScreenCapture(orientation)); } - - images.save = function (img, path, format, quality) { - format = format || "png"; - quality = quality == undefined ? 100 : quality; - return javaImages.save(img, path, format, quality); + if (options.region !== undefined) { + mat.release(); } + resultMat.release(); + return result; + }; - images.saveImage = images.save; + images.resize = function (img, size, interpolation) { + initIfNeeded(); + let mat = new Mat(); + interpolation = Imgproc['INTER_' + (interpolation || 'LINEAR')]; + Imgproc.resize(img.mat, mat, newSize(size), 0, 0, interpolation); + return images.matToImage(mat); + }; - images.grayscale = function (img, dstCn) { - return images.cvtColor(img, "BGR2GRAY", dstCn); + images.scale = function (img, fx, fy, interpolation) { + initIfNeeded(); + let mat = new Mat(); + interpolation = Imgproc['INTER_' + (interpolation || 'LINEAR')]; + Imgproc.resize(img.mat, mat, newSize([0, 0]), fx, fy, interpolation); + return images.matToImage(mat); + }; + + images.rotate = function (img, degree, x, y) { + initIfNeeded(); + if (x === undefined) { + x = img.width / 2; } - - images.threshold = function (img, threshold, maxVal, type) { - initIfNeeded(); - var mat = new Mat(); - type = type || "BINARY"; - type = Imgproc["THRESH_" + type]; - Imgproc.threshold(img.mat, mat, threshold, maxVal, type); - return images.matToImage(mat); + if (y === undefined) { + y = img.height / 2; } + return rtImages.rotate(img, x, y, degree); + }; - images.inRange = function (img, lowerBound, upperBound) { - initIfNeeded(); - var lb = new Scalar(colors.red(lowerBound), colors.green(lowerBound), - colors.blue(lowerBound), colors.alpha(lowerBound)); - var ub = new Scalar(colors.red(upperBound), colors.green(upperBound), - colors.blue(upperBound), colors.alpha(lowerBound)) - var bi = new Mat(); - Core.inRange(img.mat, lb, ub, bi); - return images.matToImage(bi); - } + images.concat = function (img1, img2, direction) { + initIfNeeded(); + direction = direction || 'right'; + return Images.concat(img1, img2, android.view.Gravity[direction.toUpperCase()]); + }; - images.interval = function (img, color, threshold) { - initIfNeeded(); - var lb = new Scalar(colors.red(color) - threshold, colors.green(color) - threshold, - colors.blue(color) - threshold, colors.alpha(color)); - var ub = new Scalar(colors.red(color) + threshold, colors.green(color) + threshold, - colors.blue(color) + threshold, colors.alpha(color)); - var bi = new Mat(); - Core.inRange(img.mat, lb, ub, bi); - return images.matToImage(bi); - } + images.detectsColor = function (img, color, x, y, threshold, algorithm) { + initIfNeeded(); + color = parseColor(color); + algorithm = algorithm || 'diff'; + threshold = threshold || DEF_COLOR_THRESHOLD; + let colorDetector = getColorDetector(color, algorithm, threshold); + let pixel = images.pixel(img, x, y); + return colorDetector.detectsColor(colors.red(pixel), colors.green(pixel), colors.blue(pixel)); + }; - images.adaptiveThreshold = function (img, maxValue, adaptiveMethod, thresholdType, blockSize, C) { - initIfNeeded(); - 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) { - initIfNeeded(); - var mat = new Mat(); - size = newSize(size); - type = Core["BORDER_" + (type || "DEFAULT")]; - 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) { - initIfNeeded(); - var mat = new Mat(); - Imgproc.medianBlur(img.mat, mat, size); - return images.matToImage(mat); - } - - - images.gaussianBlur = function (img, size, sigmaX, sigmaY, type) { - initIfNeeded(); - var mat = new Mat(); - size = newSize(size); - sigmaX = sigmaX == undefined ? 0 : sigmaX; - sigmaY = sigmaY == undefined ? 0 : sigmaY; - type = Core["BORDER_" + (type || "DEFAULT")]; - Imgproc.GaussianBlur(img.mat, mat, size, sigmaX, sigmaY, type); - return images.matToImage(mat); - } - - images.cvtColor = function (img, code, dstCn) { - initIfNeeded(); - 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) { - initIfNeeded(); - 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) { - initIfNeeded(); - 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) { - initIfNeeded(); - 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.rotate = function (img, degree, x, y) { - initIfNeeded(); - if (x == undefined) { - x = img.width / 2; - } - if (y == undefined) { - y = img.height / 2; - } - return javaImages.rotate(img, x, y, degree); - } - - images.concat = function (img1, img2, direction) { - initIfNeeded(); - direction = direction || "right"; - return javaImages.concat(img1, img2, android.view.Gravity[direction.toUpperCase()]); - } - - images.detectsColor = function (img, color, x, y, threshold, algorithm) { - initIfNeeded(); - 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) { - initIfNeeded(); - color = parseColor(color); - options = options || {}; - var region = options.region || []; + images.findColor = function (img, color, options) { + initIfNeeded(); + color = parseColor(color); + options = options || {}; + let region = options.region || []; + let threshold = function $iiFe() { if (options.similarity) { - var threshold = parseInt(255 * (1 - options.similarity)); - } else { - var threshold = options.threshold || defaultColorThreshold; + return parseInt(255 * (1 - options.similarity)); } - if (options.region) { - return colorFinder.findColor(img, color, threshold, buildRegion(options.region, img)); - } else { - return colorFinder.findColor(img, color, threshold, null); - } - } + return options.threshold || DEF_COLOR_THRESHOLD; + }(); - images.findColorInRegion = function (img, color, x, y, width, height, threshold) { - return findColor(img, color, { - region: [x, y, width, height], - threshold: threshold - }); + if (options.region) { + return colorFinder.findColor(img, color, threshold, buildRegion(region, img)); + } else { + return colorFinder.findColor(img, color, threshold, null); } + }; - images.findColorEquals = function (img, color, x, y, width, height) { - return findColor(img, color, { - region: [x, y, width, height], - threshold: 0 - }); - } + images.findColorInRegion = function (img, color, x, y, width, height, threshold) { + return findColor(img, color, { + region: [x, y, width, height], + threshold: threshold, + }); + }; - images.findAllPointsForColor = function (img, color, options) { - initIfNeeded(); - color = parseColor(color); - options = options || {}; + 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) { + initIfNeeded(); + color = parseColor(color); + options = options || {}; + let threshold = function $iiFe() { 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)); + return parseInt(255 * (1 - options.similarity)); } + return options.threshold || DEF_COLOR_THRESHOLD; + }(); + 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) { - initIfNeeded(); - 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.findMultiColors = function (img, firstColor, paths, options) { + initIfNeeded(); + options = options || {}; + firstColor = parseColor(firstColor); + let list = java.lang.reflect.Array.newInstance(java.lang.Integer.TYPE, paths.length * 3); + for (let i = 0; i < paths.length; i++) { + let p = paths[i]; + list[i * 3] = p[0]; + list[i * 3 + 1] = p[1]; + list[i * 3 + 2] = parseColor(p[2]); } + let region = options.region ? buildRegion(options.region, img) : null; + let threshold = options.threshold === undefined ? DEF_COLOR_THRESHOLD : options.threshold; + return colorFinder.findMultiColors(img, firstColor, threshold, region, list); + }; - images.findImage = function (img, template, options) { - initIfNeeded(); - options = options || {}; - var threshold = options.threshold || 0.9; - var maxLevel = -1; - if (typeof (options.level) == 'number') { - maxLevel = options.level; - } - var weakThreshold = options.weakThreshold || 0.6; - 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.findImage = function (img, template, options) { + initIfNeeded(); + options = options || {}; + let threshold = options.threshold || 0.9; + let maxLevel = -1; + if (typeof options.level === 'number') { + maxLevel = options.level; } - - images.matchTemplate = function (img, template, options) { - initIfNeeded(); - options = options || {}; - var threshold = options.threshold || 0.9; - var maxLevel = -1; - if (typeof (options.level) == 'number') { - maxLevel = options.level; - } - var max = options.max || 5; - var weakThreshold = options.weakThreshold || 0.6; - var result; - if (options.region) { - result = javaImages.matchTemplate(img, template, weakThreshold, threshold, buildRegion(options.region, img), maxLevel, max); - } else { - result = javaImages.matchTemplate(img, template, weakThreshold, threshold, null, maxLevel, max); - } - return new MatchingResult(result); + let weakThreshold = options.weakThreshold || 0.6; + 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.matchTemplate = function (img, template, options) { + initIfNeeded(); + options = options || {}; + let threshold = options.threshold || 0.9; + let maxLevel = -1; + if (typeof options.level === 'number') { + maxLevel = options.level; } - - images.fromBase64 = function (base64) { - return javaImages.fromBase64(base64); + let max = options.max || 5; + let weakThreshold = options.weakThreshold || 0.6; + let result; + if (options.region) { + result = rtImages.matchTemplate(img, template, weakThreshold, threshold, buildRegion(options.region, img), maxLevel, max); + } else { + result = rtImages.matchTemplate(img, template, weakThreshold, threshold, null, maxLevel, max); } + return new MatchingResult(result); + }; - images.toBase64 = function (img, format, quality) { - format = format || "png"; - quality = quality == undefined ? 100 : quality; - return javaImages.toBase64(img, format, quality); - } + images.findImageInRegion = function (img, template, x, y, width, height, threshold) { + return images.findImage(img, template, { + region: [x, y, width, height], + threshold: threshold, + }); + }; - images.fromBytes = function (bytes) { - return javaImages.fromBytes(bytes); - } + images.fromBase64 = function (base64) { + return rtImages.fromBase64(base64); + }; - images.toBytes = function (img, format, quality) { - format = format || "png"; - quality = quality == undefined ? 100 : quality; - return javaImages.toBytes(img, format, quality); - } + images.toBase64 = function (img, format, quality) { + format = format || 'png'; + quality = quality === undefined ? 100 : quality; + return rtImages.toBase64(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.fromBytes = function (bytes) { + return rtImages.fromBytes(bytes); + }; - images.matToImage = function (img) { - initIfNeeded(); - return Image.ofMat(img); - } + images.toBytes = function (img, format, quality) { + format = format || 'png'; + quality = quality === undefined ? 100 : quality; + return rtImages.toBytes(img, format, quality); + }; + images.readPixels = function (path) { + let img = images.read(path); + let bitmap = img.getBitmap(); + let w = bitmap.getWidth(); + let h = bitmap.getHeight(); + let 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) { + initIfNeeded(); + return Image.ofMat(img); + }; + util.__assignFunctions__(rtImages, images, ['captureScreen', 'read', 'copy', 'load', 'clip', 'pixel']); + scope.__asGlobal__(images, ['requestScreenCapture', 'captureScreen', 'findImage', 'findImageInRegion', 'findColor', 'findColorInRegion', 'findColorEquals', 'findMultiColors']); - 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); - } + scope.colors = colors; + return images; - function toPointArray(points) { - var arr = []; - for (var i = 0; i < points.length; i++) { - arr.push(points[i]); - } - return arr; - } - - function buildRegion(region, img) { - if (region == undefined) { - region = []; - } - 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); - if (x < 0 || y < 0 || x + width > img.width || y + height > img.height) { - throw new Error("out of region: region = [" + [x, y, width, height] + "], image.size = [" + [img.width, img.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]); - } - - function initIfNeeded() { - javaImages.initOpenCvIfNeeded(); - } - - scope.__asGlobal__(images, ['requestScreenCapture', 'captureScreen', 'findImage', 'findImageInRegion', 'findColor', 'findColorInRegion', 'findColorEquals', 'findMultiColors']); - - scope.colors = colors; - - return images; - } -} \ No newline at end of file +}; \ No newline at end of file diff --git a/project-versions.json b/project-versions.json index 2daa05bb..0cc6375c 100644 --- a/project-versions.json +++ b/project-versions.json @@ -1,5 +1,5 @@ { - "appVersionCode": 693, + "appVersionCode": 694, "appVersionName": "6.0.1", "appSinceDate": "Jan 1, 2022", "target": 28,