add: images.findImage; change images.findColor implementation to opencv

This commit is contained in:
hyb1996
2017-11-26 17:47:17 +08:00
parent 35955faba9
commit 328ab6efa7
25 changed files with 551 additions and 811 deletions

View File

@@ -37,7 +37,7 @@ module.exports = function(runtime, scope){
}
call.enqueue(new Callback({
onResponse: function(call, res){
callback(res);
callback(wrapResponse(res));
},
onFailure: function(call, ex){
callback(null, ex);
@@ -103,6 +103,7 @@ module.exports = function(runtime, scope){
function wrapResponse(res){
var r = {};
r.statusCode = res.code();
r.statusMessage = res.message();
var headers = res.headers();
r.headers = {};
for(var i = 0; i < headers.size(); i++){
@@ -112,6 +113,7 @@ module.exports = function(runtime, scope){
r.body.json = function(){
return JSON.parse(r.body.string());
}
r.body.contentType = r.body.contentType();
r.request = res.request();
r.url = r.request.url();
r.method = r.request.method();

View File

@@ -17,13 +17,15 @@ module.exports = function(__runtime__, scope){
images.captureScreen = rtImages.captureScreen.bind(rtImages);
images.read = rtImages.read.bind(rtImages);
images.saveImage = rtImages.saveImage.bind(rtImages);
images.pixel = rtImages.pixel;
images.detectsColor = function(img, color, x, y, threshold, algorithm){
color = parseColor(color);
algorithm = algorithm || "rgb";
algorithm = algorithm || "diff";
threshold = threshold || 16;
var colorDetector = getColorDetector(color, algorithm, threshold);
var pixel = images.pixel(img, x, y);
@@ -34,54 +36,80 @@ module.exports = function(__runtime__, scope){
color = parseColor(color);
options = options || {};
var region = options.region || [];
var x = region[0] || 0;
var y = region[1] || 0;
var width = region[2] || (img.getWidth() - x);
var height = region[3] || (img.getHeight() - y);
var threads = options.threads || 2;
if(options.similarity){
var threshold = parseInt(255 * (1 - options.similarity));
}else{
var threshold = options.threshold || 16;
}
algorithm = options.algorithm || "rgb";
var rect = new android.graphics.Rect(x, y, width + x, height + y);
var colorDetector = getColorDetector(color, algorithm, threshold);
return colorFinder.findColorConcurrently(img, colorDetector, rect, threads);
if(options.region){
return rtImages.toAndroidPoint(colorFinder.findColor(img, color, threshold, buildRegion(options.region, img)));
}else{
return rtImages.toAndroidPoint(colorFinder.findColor(img, color, threshold, null));
}
}
images.findColorInRegion = function(img, color, x, y, width, height, threads, algorithm, threshold){
images.findColorInRegion = function(img, color, x, y, width, height, threshold){
return findColor(img, color, {
region: [x, y, width, height],
algorithm: algorithm,
threshold: threshold,
threads: threads
threshold: threshold
});
}
images.findColorEquals = function(img, color, x, y, width, height, threads){
images.findColorEquals = function(img, color, x, y, width, height){
return findColor(img, color, {
region: [x, y, width, height],
algorithm: "equal",
threads: threads
threshold: 0
});
}
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);
}
images.findColors = function(img, color, options){
color = parseColor(color);
options = options || {};
if(options.similarity){
var threshold = parseInt(255 * (1 - options.similarity));
}else{
var threshold = options.threshold || 16;
}
if(options.region){
return toPointArray(colorFinder.findAllColors(img, color, threshold, buildRegion(options.region, img)));
}else{
return toPointArray(colorFinder.findAllColors(img, color, threshold, null));
}
}
images.findImage = function(img, template, options){
options = options || {};
var threshold = options.threshold || 0.9;
var maxLevel = options.level || -1;
if(options.region){
return rtImages.findImage(img, template, threshold, buildRegion(options, img), maxLevel);
}else{
return rtImages.findImage(img, template, 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
});
}
function toPointArray(points){
var arr = [];
for(var i = 0; i < points.length; i++){
arr.push(rtImages.toAndroidPoint(points[i]));
}
return arr;
}
function buildRegion(region, img){
var x = region[0] || 0;
var y = region[1] || 0;
var width = region[2] || (img.getWidth() - x);
var height = region[3] || (img.getHeight() - y);
return new org.opencv.core.Rect(x, y, width, height);
}
function parseColor(color){
if(typeof(color) == 'string'){
@@ -94,7 +122,7 @@ module.exports = function(__runtime__, scope){
return color;
}
scope.__asGlobal__(images, ['requestScreenCapture', 'captureScreen', 'findColor', 'findColorInRegion', 'findColorEquals']);
scope.__asGlobal__(images, ['requestScreenCapture', 'captureScreen', 'findImage', 'findImageInRegion', 'findColor', 'findColorInRegion', 'findColorEquals']);
scope.colors = colors;

View File

@@ -24,8 +24,8 @@ exports.format = function(f) {
if (!isString(f)) {
var objects = [];
for (var i = 0; i < arguments.length; i++) {
var v = isJavaObject(arguments[i]) ? arguments[i].toString() :
arguments[i];
var v = (arguments[i] && isJavaObject(arguments[i])) ? arguments[i].toString() :
arguments[i];
objects.push(inspect(v));
}
return objects.join(' ');